arcengine要素轉點文件

有一個將標註文件(Annotation)以重心轉換爲點文件的任務,方法一:要素轉點spa

public void FeatureToPointCollection(IMap pMap)
        {
            IFeatureLayer pFeatureLayer = pMap.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCursor.NextFeature();
            IPointCollection pPointCollection = new MultipointClass();
            object o = Type.Missing;
            while (pFeature != null)
            {
                IGeometry pGeometry = pFeature.Shape;
                IArea pArea = pGeometry as IArea;
                IPoint pPoint = pArea.Centroid;
                pPointCollection.AddPoint(pPoint, ref o, ref o);
                pFeature = pFeatureCursor.NextFeature();
            }
            IGeoDataset pGeoDataset = pFeatureLayer as IGeoDataset;
            ISpatialReferenceFactory pSRFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
            PointCollToShpFile(pPointCollection, "C:/Users/Administrator/Desktop/testfolder/test.shp", pSpatialReference);
        }
public IFeatureLayer PointCollToShpFile(IPointCollection pPointCollection, string filePath, ISpatialReference pReference)
        {
            //利用IWorkspaceFactory打開工做空間
            int index = filePath.LastIndexOf('/');
            string folder = filePath.Substring(0, index);
            string shapeName = filePath.Substring(index + 1);
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pFWS = (IFeatureWorkspace)pWSF.OpenFromFile(folder, 0);

            //建立字段編輯所須要的接口
            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit;
            pFieldsEdit = (IFieldsEdit)pFields;

            //給字段屬性、類型賦值
            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef pGeometryDef = new GeometryDefClass();
            IGeometryDefEdit pGDefEdit = (IGeometryDefEdit)pGeometryDef;
            pGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            //設置座標系
            pGDefEdit.SpatialReference_2 = pReference;

            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pFieldsEdit.AddField(pField);

            IFeatureClass pFeatureClass;
            pFeatureClass = pFWS.CreateFeatureClass(shapeName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

            IPoint pPoint = new PointClass();
            for (int j = 0; j < pPointCollection.PointCount; j++)
            {
                pPoint = pPointCollection.get_Point(j);
                IFeature pFeature = pFeatureClass.CreateFeature();
                pFeature.Shape = pPoint;
                pFeature.Store();
            }
        }

還有一種思路應該比這個簡單,正在~~~~~,記錄一下code

相關文章
相關標籤/搜索