ArcEngine要素转点,要素节点转点

虽然有方便的GP工具FeatureVerticesToPoint。然而其需要最高权限,某些时候没法使用,可以用下面的函数作为替代。

        /// <summary>
        /// 要素节点转点
        /// </summary>
        /// <param name="IN_Featureclass">要转换的要素类</param>
        /// <param name="IN_FeatureDataset">要生成转换后要素类的要素数据集</param>
        /// <returns>生成的点要素类</returns>
        private IFeatureClass PRV_FeatureVerticesToPoints(IFeatureClass IN_Featureclass,IFeatureDataset IN_FeatureDataset)
        {
            //创建要素类
            IFeatureClass Temp_VPFeatureClass =IN_FeatureDataset.CreateFeatureClass(IN_Featureclass.AliasName + "_VPoints", PRV_SetFields(IN_Featureclass), null, null, esriFeatureType.esriFTSimple, "Shape", "");
            //提取所有要素
            IFeatureCursor Temp_GetEachFeature = IN_Featureclass.Search(null, false);
            IFeature Temp_EachFeature = Temp_GetEachFeature.NextFeature();
            if (Temp_EachFeature != null)
            {
                IWorkspaceEdit Temp_WorkspaceEdit = (IWorkspaceEdit)
                Temp_WorkspaceEdit.StartEditing(true);
                Temp_WorkspaceEdit.StartEditOperation();
                IFeatureBuffer Temp_FeatureBuffer = Temp_VPFeatureClass.CreateFeatureBuffer();
                IFeatureCursor Temp_FeatureCursor = Temp_VPFeatureClass.Insert(true);
                //将点存入要素类
                while (Temp_EachFeature != null)
                {
                    //获取要素的点集
                    IPointCollection Temp_Vertices = Temp_EachFeature.Shape as IPointCollection;
                    for (int i = 0; i < Temp_Vertices.PointCount - 1; i++)
                    {
                      Temp_FeatureBuffer.Shape = Temp_Vertices.Point[i];
                      Temp_FeatureCursor.InsertFeature(Temp_FeatureBuffer);
                    }
                    Temp_EachFeature = Temp_GetEachFeature.NextFeature();
                }
                Temp_FeatureCursor.Flush();
                Temp_WorkspaceEdit.StopEditOperation();
                Temp_WorkspaceEdit.StopEditing(true);
                //清理资源并返回
                System.Runtime.InteropServices.Marshal.ReleaseComObject(Temp_GetEachFeature);
            }
            return Temp_VPFeatureClass;
        }

该函数的辅助函数

  private IFields PRV_SetFields(IFeatureClass IN_Featureclass)//设字段的函数
        {
            //创建必要字段
            IFeatureClassDescription Temp_FeatureClassDescription = new FeatureClassDescriptionClass();
            IObjectClassDescription Temp_ObjectClassDescription = (IObjectClassDescription)Temp_FeatureClassDescription;
            IFields Temp_Fields = Temp_ObjectClassDescription.RequiredFields;
            int Temp_ShapeFieldIndex = Temp_Fields.FindField(Temp_FeatureClassDescription.ShapeFieldName);
            IField Temp_RequiredField = Temp_Fields.get_Field(Temp_ShapeFieldIndex);
            //设定几合字段
            IGeometryDef Temp_GeometryDef = Temp_RequiredField.GeometryDef;
            IGeometryDefEdit Temp_GeometryDefEdit = (IGeometryDefEdit)Temp_GeometryDef;
            Temp_GeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            ISpatialReferenceFactory Temp_SpatialRefFac = new SpatialReferenceEnvironmentClass();
            //以备用点为模板构建空间参考
            Temp_GeometryDefEdit.SpatialReference_2 = (IN_Featureclass as IGeoDataset).SpatialReference;
            //验证字段
            IFieldChecker Temp_FieldChecker = new FieldCheckerClass();
            IEnumFieldError Temp_FieldsError = null;
            IFields Temp_AllRightFields = null;//验证无误的字段集
            Temp_FieldChecker.ValidateWorkspace = (IWorkspace)FDS_MainFeatureDataset.Workspace;
            Temp_FieldChecker.Validate(Temp_Fields, out Temp_FieldsError, out Temp_AllRightFields);
            //添加"原图形的OID"字段
            IFieldsEdit Temp_FiledsEditor = Temp_AllRightFields as IFieldsEdit;
            IField Temp_OriginOIDFields = new FieldClass();
            IFieldEdit2 Temp_FiledEditor = Temp_OriginOIDFields as IFieldEdit2;
            Temp_FiledEditor.Type_2 = esriFieldType.esriFieldTypeInteger;
            Temp_FiledEditor.Name_2 = "OriginClassOID";
            Temp_FiledsEditor.AddField(Temp_OriginOIDFields);
            return Temp_AllRightFields;
        }

经测试,该方法只在 数据量小时起作用,数据量过大会出错(本人测试32000个面转到15000个面就报错了) 效率远远低于GP工具。估计GP工具采取了某些底层算法,处理60万点在160秒内(i5-6500,8G),本函数处理15000个面也需要超过10分钟时间。

如果有好方法也可以分享一下。

附GP工具的函数:

 //面转节点(GP法)
        private IFeatureClass PRV_FeatureVerticesToPoints_GP(IFeatureClass IN_Featureclass,IFeatureDataset FDS_MainFeatureDataset)
        {
            //要素数据集地址
            string Temp_FeaturedatasetPath = FDS_MainFeatureDataset.Workspace.PathName + "\\" + FDS_MainFeatureDataset.Name + "\\";
            //面转点
           Geoprocessor GP_Tool = new Geoprocessor();//GP运行工具
            FeatureVerticesToPoints GP_FeatureVerticesToPoint = new FeatureVerticesToPoints();
            GP_FeatureVerticesToPoint.in_features = Temp_FeaturedatasetPath + IN_Featureclass.AliasName;
            GP_FeatureVerticesToPoint.point_location = "All";
            GP_FeatureVerticesToPoint.out_feature_class = Temp_FeaturedatasetPath + IN_Featureclass.AliasName+"_VP";
            GP_Tool.Execute(GP_FeatureVerticesToPoint, null);
            //获取生成的要素类
            IFeatureClass Temp_FeatureClass = (FDS_MainFeatureDataset.Workspace as IFeatureWorkspace).OpenFeatureClass(IN_Featureclass.AliasName + "_VP");
            return Temp_FeatureClass;

        }


>

你可能感兴趣的:(arcgis,ArcEngine,ArcgisEngine)