Arcgis Server向MXD中添加图层

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 "操作成功";
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值