代码改变世界

ArcGIS Server中通过C#添加或修改空间数据

2011-06-02 16:42  何朝阳  阅读(1730)  评论(0编辑  收藏  举报

最近在项目中需要动态添加或修改图层中的边坡点,经过差不多一天的努力,终于实现了这个功能,其中:

①如果数据源中使用了Group Layer,则得到的图层不是需要编辑的目标层,而是Group Layer层,如下图,得到的图层为“业务数据”,而我需要编辑的是“边坡”图层,所以这个时候就必须要对获得的图层进行判断,看是否为IGroupLayerICompositeLayer

//如果得到的layer是Group Layer
if (layer is ESRI.ArcGIS.Carto.IGroupLayer || layer is ESRI.ArcGIS.Carto.ICompositeLayer)
{
    ESRI.ArcGIS.Carto.ICompositeLayer Igrouplayer = (ESRI.ArcGIS.Carto.ICompositeLayer)layer;
    layer = Igrouplayer.get_Layer(layer_index);
}

②判断图层中是否存在字段以及添加字段的时候总是出错,后来才发现,在添加字段的时候,必须先停止编辑(wse.StopEditOperation(); wse.StopEditing(true);),或在开始编辑之前进行添加字段的操作;

操作结果如下图,速度还是比较令人满意:

全部代码如下:

        #region 在地图上修改或添加边坡点
        /// <summary>
        /// 在地图上修改或添加边坡点。
        /// </summary>
        /// <param name="MapResourceManager1">要编辑的地图资源</param>
        /// <param name="mapResourceName">MapResourceItem的名称</param>
        /// <param name="targetLayerName">要编辑的图层的名称</param>
        /// <param name="bh">边坡编号</param>
        /// <param name="name">边坡名称</param>
        /// <param name="X">边坡点的经度</param>
        /// <param name="Y">边坡点的纬度</param>
        public static bool AddPoint(MapResourceManager MapResourceManager1, string mapResourceName, string targetLayerName, string bh, string name, double X, double Y)
        {
            try
            {
                if (!MapResourceManager1.Initialized) MapResourceManager1.Initialize();
                IGISResource gisresource = (IGISResource)MapResourceManager1.GetResource(mapResourceName);
                bool supportquery = gisresource.SupportsFunctionality(typeof(IQueryFunctionality));
                if (supportquery)
                {
                    IQueryFunctionality qfunc = (IQueryFunctionality)gisresource.CreateFunctionality(typeof(IQueryFunctionality), null);
                    string[] lids;
                    string[] lnames;
                    qfunc.GetQueryableLayers(null, out lids, out lnames);

                    //获取图层ID
                    int layer_index = 0;
                    for (int i = 0; i < lnames.Length; i++)
                    {
                        if (lnames[i] == targetLayerName)
                        {
                            layer_index = i;
                            break;
                        }
                    }
                    ESRI.ArcGIS.ADF.Web.QueryFilter myQueryFilter = new ESRI.ArcGIS.ADF.Web.QueryFilter();
                    myQueryFilter.ReturnADFGeometries = true;
                    myQueryFilter.WhereClause = string.Format("BH='{0}'", bh);
                    DataTable dt = qfunc.Query(null, lids[layer_index], myQueryFilter);

                    if (qfunc.Resource is ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
                    {
                        MapResourceLocal mr = (MapResourceLocal)qfunc.Resource;
                        ESRI.ArcGIS.Server.IServerContext serverContext = null;
                        serverContext = mr.ServerContextInfo.ServerContext;
                        ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass();
                        point.X = X;
                        point.Y = Y;
                        ESRI.ArcGIS.Carto.IMapServerObjects mso = (ESRI.ArcGIS.Carto.IMapServerObjects)mr.MapServer;
                        ESRI.ArcGIS.Carto.ILayer layer = mso.get_Layer(mr.MapServer.DefaultMapName, layer_index);

                        //如果得到的layer是Group Layer
                        if (layer is ESRI.ArcGIS.Carto.IGroupLayer || layer is ESRI.ArcGIS.Carto.ICompositeLayer)
                        {
                            ESRI.ArcGIS.Carto.ICompositeLayer Igrouplayer = (ESRI.ArcGIS.Carto.ICompositeLayer)layer;
                            layer = Igrouplayer.get_Layer(layer_index);
                        }

                        ESRI.ArcGIS.Carto.IFeatureLayer fl = (ESRI.ArcGIS.Carto.IFeatureLayer)layer;
                        IDataset dataset = fl.FeatureClass as IDataset;
                        IWorkspace workspace = dataset.Workspace;
                        IWorkspaceEdit wse = dataset.Workspace as IWorkspaceEdit;
                        IFeatureClass featureclass = fl.FeatureClass as IFeatureClass;

                        if (featureclass.FindField("BH") == -1)
                        {
                            IField pField = new FieldClass();
                            IFieldEdit pFieldEdit = pField as IFieldEdit;
                            pFieldEdit.Length_2 = 100;
                            pFieldEdit.Name_2 = "BH";
                            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                            featureclass.AddField(pField);
                        }
                        if (featureclass.FindField("Name") == -1)
                        {
                            IField pField = new FieldClass();
                            IFieldEdit pFieldEdit = pField as IFieldEdit;
                            pFieldEdit.Length_2 = 254;
                            pFieldEdit.Name_2 = "Name";
                            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                            featureclass.AddField(pField);
                        }

                        IFeature feature;
                        wse.StartEditing(false);
                        wse.StartEditOperation();

                        if (dt != null && dt.Rows.Count > 0)
                        {
                            //修改
                            int fid = (int)(dt.Rows[0][0]);
                            feature = featureclass.GetFeature(fid);
                        }
                        else
                        {
                            //添加
                            feature = featureclass.CreateFeature();
                        }
                        feature.Shape = point as IGeometry;
                        feature.set_Value(feature.Fields.FindField("BH"), bh);
                        feature.set_Value(feature.Fields.FindField("Name"), name);
                        feature.Store();
                        wse.StopEditOperation();
                        wse.StopEditing(true);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion