您的位置:首页 > 其它

一篇对实际工作很有帮助的随笔:Server ADF 动态创建mapService

2011-02-25 13:38 316 查看
//added by zhangjun at 2011-02-25 通过这种方式可以弥补RIA开发的不足,充分发挥服务器的威力,需要什么都动态的在服务器生成相应的服务。

说明:加入地图包括图层渲染,标注等,数据从ArcSDE中读出!

代码中有两个数据表:mapinfo(存储地图信息,key:地图GUID),LayerInfo(存储图层信息,包括其在SDE中的名称,以及最主要的其属于哪个地图的地图GUID),代码中的MapGUID即为地图GUID。

代码中包括连接SDE以及常见sde工作空间的内容

代码如下:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using ESRI.ArcGIS.Server.WebControls;

using ESRI.ArcGIS.Server;

using ESRI.ArcGIS.Geodatabase;

using ESRI.ArcGIS.Carto;

using ESRI.ArcGIS.ADF.Web.Display.Symbol;

using ESRI.ArcGIS.Display;

using ESRI.ArcGIS.DataSourcesRaster;

using System.IO;

using ESRI.ArcGIS.esriSystem;

using System.Drawing;

public partial class ServerManager : System.Web.UI.Page

{

string HostName = "zhangye";

string MapServerUserName = "ArcGISSOM";

string MapserverPass = "123456";

string MxdPah = @"F:\BaseData";

private static string connectionString = "server=172.39.8.215;uid=sa;pwd=sa;database=gis_data";

protected void Page_Load(object sender, EventArgs e)

{

if (Request.QueryString["MapGUID"] == null || Request.QueryString["MapGUID"] == "")

{

Response.Write("创建失败");

return;

}

string MapName = Request.QueryString["MapGUID"];

if (isExistService(MapName))

{

Response.Write("创建成功");

return;

}

if (isExistMxd(MxdPah, MapName))

{

string MxdFullPath = MxdPah + "\\" + MapName + ".mxd";

CreateServices(MxdFullPath, MapName);

Response.Write("创建成功");

return;

}

else

{

if (CreateMxd(MxdPah, MapName))

{

string MxdFullPath = MxdPah + "\\" + MapName + ".mxd";

CreateServices(MxdFullPath, MapName);

Response.Write("创建成功");

return;

}

}

}

private DataTable GetMapLayersList(string MapGUID)

{

DataSet pDs = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, "select * from LayerInfo where MapID='"+MapGUID+"'");

DataTable dt = pDs.Tables[0];

return dt;

}

#region 创建服务、地图文档以及服务上下文

#region 创建地图服务

private bool CreateServices(string MapPath, string ServerName)

{

IGISServerConnection pGISServerConnection;

pGISServerConnection = new GISServerConnectionClass();

pGISServerConnection.Connect(HostName);

IServerObjectAdmin pServerObjectAdmin;

pServerObjectAdmin = pGISServerConnection.ServerObjectAdmin;

IServerObjectConfiguration2 configuration = (IServerObjectConfiguration2)pServerObjectAdmin.CreateConfiguration();

configuration.Name = ServerName;//发布Service的名称,必填

configuration.TypeName = "MapServer";//发布服务的类型,如:MapServer,GeocodeServer

IPropertySet props = configuration.Properties;

props.SetProperty("FilePath", MapPath);//设置MXD的路径

#region 一下的property并非必须,只要一个filepath就可以发布

props.SetProperty("OutputDir", "c:\\arcgisserver\\arcgisoutput");//图片的输出目录

props.SetProperty("VirtualOutPutDir", "http://zhangye/arcgisoutput");//图片输出的虚拟路径

props.SetProperty("SupportedImageReturnTypes", "URL");//支持的图片类型

props.SetProperty("MaxImageHeight", "2048");//图片的最大高度

props.SetProperty("MaxRecordCount", "500");//返回记录的最大条数

props.SetProperty("MaxBufferCount", "100");//缓冲区分析的最大数目

props.SetProperty("MaxImageWidth", "2048");//图片的最大宽度

props.SetProperty("IsCached", "false");//是否切片

props.SetProperty("CacheOnDemand", "false");//是否主动切片

props.SetProperty("IgnoreCache", "false");//是否忽略切片

props.SetProperty("ClientCachingAllowed", "true");//是否允许客户端缓冲

props.SetProperty("CacheDir", "c:\\arcgisserver\\arcgiscache\\NewService");//切片的输出路径

props.SetProperty("SOMCacheDir", "c:\\arcgisserver\\arcgiscache");//som的切片输出路径

//configuration.Description = "NewService";//Service的描述

configuration.IsolationLevel = esriServerIsolationLevel.esriServerIsolationHigh;//或者esriServerIsolationLow,esriServerIsolationAny

configuration.IsPooled = true;//是否池化

configuration.MaxInstances = 2;//最多的实例数

configuration.MinInstances = 1;//最少的实例数

////设置刷新

IPropertySet recycleProp = configuration.RecycleProperties;

recycleProp.SetProperty("StartTime", "00:00");//刷新开始时间

recycleProp.SetProperty("Interval", "3600");//刷新间隔

////设置是否开启REST服务

IPropertySet infoProp = configuration.Info;

infoProp.SetProperty("WebEnabled", "true");//是否提供REST服务

infoProp.SetProperty("WebCapabilities", "Map,Query,Data");//提供何种服务

//configuration.StartupType = esriStartupType.esriSTAutomatic;//或者esriSTManual

//configuration.UsageTimeout = 120;//客户端占用一个服务的最长时间

//configuration.WaitTimeout = 120;//客户端申请一个服务的最长等待时间

#endregion

//添加服务到Server

pServerObjectAdmin.AddConfiguration(configuration);

//启动服务

pServerObjectAdmin.StartConfiguration(ServerName, "MapServer");

return true;

}

#endregion

private bool CreateMxd(string MxdPath, string MxdName)

{

IMapDocument pMapDocument = CreateObject("esriCarto.MapDocument") as IMapDocument;

if (MxdPath.Substring(MxdPath.Length - 1) != @"\")

MxdPath += @"\";

pMapDocument.New(MxdPath + MxdName + ".mxd");

AddLayerToMxd(pMapDocument, MxdName);

if (pMapDocument == null)

return false;

if (pMapDocument.get_IsReadOnly(MxdPath + MxdName + ".mxd") == true)

{

return false;

}

pMapDocument.Save(true, false);

return true;

}

private bool AddLayerToMxd(IMapDocument pMapDocument,string MapGUID)

{

DataTable dt = GetMapLayersList(MapGUID);

IMap pMap = pMapDocument.get_Map(0);

IMapLayers mapLayer = pMap as IMapLayers;

mapLayer.ClearLayers();

for (int i = 0; i < dt.Rows.Count; i++)

{

DataRow dr = dt.Rows[i];

if ((int)dr["LayerType"] == 1)

{

IRasterDataset pRDataSet = GetRasterRasterDataset(dr["LayerStoreName"].ToString());

IRasterLayer pRLayer = CreateObject("esriCarto.RasterLayer") as IRasterLayer;

pRLayer.CreateFromDataset(pRDataSet);

RanderRaster(pRLayer);

mapLayer.AddLayer(pRLayer as ILayer);

}

else

{

IFeatureClass pFeatureClass = GetFeatureClass(dr["LayerStoreName"].ToString());

IFeatureLayer pFLayer = CreateObject("esriCarto.FeatureLayer") as IFeatureLayer;

pFLayer.FeatureClass = pFeatureClass;

IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pFLayer;

pGeoFeatureLayer.AnnotationProperties.Clear();

//IAnnotateLayerPropertiesCollection pAnnoLayerPropsColl = CreateObject("esriCarto.AnnotateLayerPropertiesCollection");

IAnnotateLayerPropertiesCollection pAnnoLayerPropsColl = pGeoFeatureLayer.AnnotationProperties;

ILabelEngineLayerProperties pLabelEngine = CreateObject("esriCarto.LabelEngineLayerProperties") as ILabelEngineLayerProperties;

pLabelEngine.Expression = "[CONTOUR]";

ITextSymbol pSym = CreateObject("esriDisplay.TextSymbol") as ITextSymbol;

pSym.Color = GetRGBColor(0, 200, 200);

pSym.Size = 12;

pLabelEngine.Symbol = pSym;

IAnnotateLayerProperties pAnnoLayerProps = (IAnnotateLayerProperties)pLabelEngine;

pAnnoLayerPropsColl.Add(pAnnoLayerProps);

pGeoFeatureLayer.DisplayAnnotation = true;

mapLayer.AddLayer(pFLayer as ILayer);

}

}

IMxdContents pMxdC = pMap as IMxdContents;

pMapDocument.ReplaceContents(pMxdC);

return true;

}

private IColor GetRGBColor(int R, int G, int B)

{

IColor pColor = CreateObject("esriDisplay.RgbColor") as IColor;

pColor.RGB = B * 65536 + G * 256 + R;

return pColor;

}

#region 栅格图层渲染

private void RanderRaster(IRasterLayer pNewLayer)

{

IRasterBandCollection pNewCol = (IRasterBandCollection)pNewLayer.Raster;

IRasterBand pNewBand = pNewCol.Item(0);

pNewBand.ComputeStatsAndHist();

IRasterStretchColorRampRenderer pSRender =CreateObject("esriCarto.RasterStretchColorRampRenderer") as IRasterStretchColorRampRenderer;

IRasterRenderer pRender = (IRasterRenderer)pSRender;

pRender.Raster = pNewLayer.Raster;

pRender.Update();

IMultiPartColorRamp mpcr =CreateObject("esriDisplay.MultiPartColorRamp") as IMultiPartColorRamp;

IAlgorithmicColorRamp Alg1 =CreateObject("esriDisplay.AlgorithmicColorRamp") as IAlgorithmicColorRamp;

IAlgorithmicColorRamp Alg2 =CreateObject("esriDisplay.AlgorithmicColorRamp") as IAlgorithmicColorRamp;

IRgbColor c1 =CreateObject("esriDisplay.RgbColor") as IRgbColor;

IRgbColor c2 =CreateObject("esriDisplay.RgbColor") as IRgbColor;

IRgbColor c3 =CreateObject("esriDisplay.RgbColor") as IRgbColor;

c3.Red = 255; c3.Green = 150; c3.Blue = 150;

c2.Red = 255; c2.Green = 255; c2.Blue = 150;

c1.Red = 150; c1.Green = 255; c1.Blue = 255;

Alg1.FromColor = c1; Alg1.ToColor = c2;

Alg2.FromColor = c2; Alg2.ToColor = c3;

mpcr.AddRamp(Alg1);

mpcr.AddRamp(Alg2);

mpcr.Size = 255;

bool IsOk;

mpcr.CreateRamp(out IsOk);

pSRender.ColorRamp = mpcr;

pNewLayer.Renderer = pRender;

}

#endregion

private IServerContext CreateServerContext(string ServerName, string UserName, string PassWord)

{

ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity(UserName, PassWord, "");

ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsConnection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection(ServerName, identity);

agsConnection.Connect();

if (agsConnection.IsConnected)

{

try

{

IServerObjectManager som = agsConnection.ServerObjectManager;

IServerContext pServerContext = som.CreateServerContext("zys", "MapServer");

return pServerContext;

}

catch (Exception e)

{

return null;

}

}

return null;

}

#region ServerContext CreateObject函数

private object CreateObject(string ObjectCLSID)

{

IServerContext pServerContext = CreateServerContext(HostName,MapServerUserName,MapserverPass);

if (pServerContext == null) return null;

try

{

return pServerContext.CreateObject(ObjectCLSID);

}

catch

{

return null;

}

finally

{

pServerContext.ReleaseContext();

}

}

#endregion

#endregion

#region 判断服务、文档是否存在

/// <summary>

/// 判断是否存在该服务

/// </summary>

/// <param name="serviceName"></param>

/// <returns></returns>

private bool isExistService(string serviceName)

{

ServerConnection pServerConnection = new ESRI.ArcGIS.Server.WebControls.ServerConnection(HostName);//地图服务机器名

pServerConnection.Connect();

IServerObjectAdmin pServerSOA = pServerConnection.ServerObjectAdmin;

try

{

IServerObjectConfiguration pConfig = pServerSOA.GetConfiguration(serviceName, "MapServer");

return true;

}

catch (Exception e)

{

return false;

}

}

private bool isExistMxd(string MxdPath, string MapName)

{

if (MxdPath.Substring(MxdPath.Length - 1) != @"\")

MxdPath += @"\";

if (File.Exists(MxdPath + MapName + ".mxd"))

{

return true;

}

return false;

}

#endregion

#region SDE连接以及数据获取

string SDEServer = "gisserver";

string Instance = "5151";

string DataBase = "gis_data";

string SDEUser = "sde";

string PassWord = "sde";

private IPropertySet GetProPerty()

{

IPropertySet propertySet = (IPropertySet)CreateObject("esriSystem.PropertySet");

propertySet.SetProperty("SERVER", SDEServer);

propertySet.SetProperty("INSTANCE", Instance);

propertySet.SetProperty("DATABASE", DataBase);

propertySet.SetProperty("USER", SDEUser);

propertySet.SetProperty("PASSWORD", PassWord);

propertySet.SetProperty("VERSION", "sde.DEFAULT");

return propertySet;

}

private IWorkspace OpenSDEWorkSpace(IPropertySet pPropSet)

{

IWorkspace pWorkSpace = null;

IWorkspaceFactory pSdeFact = (IWorkspaceFactory)CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");

try

{

pWorkSpace = pSdeFact.Open(pPropSet, 0);

}

catch (Exception e)

{

}

return pWorkSpace;

}

private IRasterWorkspaceEx OpenSdeRasterWsp()

{

try

{

IPropertySet pProPerty = GetProPerty();

IRasterWorkspaceEx pRasterWsp = OpenSDEWorkSpace(pProPerty) as IRasterWorkspaceEx;

return pRasterWsp;

}

catch (Exception e)

{

return null;

}

}

private IFeatureWorkspace OpenSdeFeatureWsp()

{

try

{

IPropertySet pProPerty = GetProPerty();

IFeatureWorkspace pRasterWsp = OpenSDEWorkSpace(pProPerty) as IFeatureWorkspace;

return pRasterWsp;

}

catch (Exception e)

{

return null;

}

}

private IRasterDataset GetRasterRasterDataset(string RasterName)

{

IRasterWorkspaceEx pRasterWspEx = OpenSdeRasterWsp();

if (pRasterWspEx == null) return null;

IRasterDataset pRasterDataset=pRasterWspEx.OpenRasterDataset(RasterName);

return pRasterDataset;

}

private IFeatureClass GetFeatureClass(string FeatureName)

{

IFeatureWorkspace pFeatureWspEx = OpenSdeFeatureWsp();

if (pFeatureWspEx == null) return null;

IFeatureClass pFeatureClass = pFeatureWspEx.OpenFeatureClass(FeatureName);

return pFeatureClass;

}

#endregion

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐