protected void Button1_Click1(object sender, EventArgs e)
{
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)
{
IServerObjectManager som = agsConnection.ServerObjectManager;
IServerContext pServerContext = som.CreateServerContext("earth", "MapServer");
IWorkspaceFactory wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
IFeatureWorkspace fws = wsf.OpenFromFile("C://GISData", 0) as IFeatureWorkspace;
IFeatureClass fc = fws.OpenFeatureClass("continent.shp");
addLayerToMxd(pServerContext, fc, 0, "D://gis data//earth.mxd);
pServerContext.ReleaseContext();
}
public string addLayerToMxd(IServerContext serverContext, IFeatureClass nfc, int groupIndex, string mxdPath)
{
IMapServer pMapServer = serverContext.ServerObject as IMapServer;
IMapServerObjects pMapServerObjects = pMapServer as IMapServerObjects;
IMap pMap = pMapServerObjects.get_Map(pMapServer.DefaultMapName);
IMapLayers mapLayer = pMap as IMapLayers;
IFeatureLayer fl = serverContext.CreateObject("esriCarto.FeatureLayer") as IFeatureLayer;
fl.FeatureClass = nfc;
fl.Name = nfc.AliasName;
ILayer pLayer = fl as ILayer;
IGeoFeatureLayer pGeoLayer = fl as IGeoFeatureLayer;
//设置样式
ISimpleRenderer pSimpleRenderer = serverContext.CreateObject("esriCarto.SimpleRenderer") as ISimpleRenderer;
IRgbColor color = new RgbColorClass();
color = serverContext.CreateObject("esriDisplay.RgbColor") as IRgbColor;
color.Red = 255;
color.Blue = 0;
color.Green = 0;
ISymbol pSymbol;
SimpleFillSymbol fillSymbol = serverContext.CreateObject("esriDisplay.SimpleFillSymbol") as SimpleFillSymbol;
fillSymbol.Color = color;
fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
pSymbol = fillSymbol as ISymbol;
pSimpleRenderer.Symbol = pSymbol;
pGeoLayer.Renderer = (IFeatureRenderer)pSimpleRenderer;
mapLayer.AddLayer(pLayer);
IMxdContents pMxdC = pMap as IMxdContents;
IMapDocument pMapDocument = serverContext.CreateObject("esriCarto.MapDocument") as IMapDocument;
pMapDocument.Open(mxdPath, "");
pMapDocument.ReplaceContents(pMxdC);
if (pMapDocument == null)
return "文档为空";
if (pMapDocument.get_IsReadOnly(mxdPath) == true)
{
return "文档为只读,操作终止";
}
IObjectCopy lip_ObjCopy = new ObjectCopyClass();
lip_ObjCopy = null;
pMapDocument.SaveAs("D://gis data//earth1_super.mxd", true, true);
return "操作成功";
}