示例#1
0
        /// <summary>
        /// 多边形转点,将多边形要素类每个图斑的中心(重心)点存入新的点要素类中
        /// </summary>
        /// <param name="sourceClass"></param>
        /// <param name="targeClassPath"></param>
        /// <returns></returns>
        public static IFeatureClass PolygonClassToPoint(this IFeatureClass sourceClass, string targeClassPath)
        {
            if (sourceClass.ShapeType != esriGeometryType.esriGeometryPolygon)
            {
                throw new Exception($"{sourceClass}不是面图层!");
            }

            var outClass         = sourceClass.CopyStruct(targeClassPath, esriGeometryType.esriGeometryPoint);
            var outFeatureCursor = outClass.Insert(true);
            var outFeatureBuffer = outClass.CreateFeatureBuffer();

            sourceClass.QueryFeatures(null, feature =>
            {
                var shape = feature.Shape;
                if (shape == null || shape.IsEmpty)
                {
                    throw new Exception($"{sourceClass}图层中存在几何为空的记录,请先进行修复几何!");
                }

                var dict = GetFieldIndexFromTwoClass(sourceClass, outClass, true);//其他字段值都从原图层复制过来
                foreach (var pair in dict)
                {
                    outFeatureBuffer.set_Value(pair.Value, feature.get_Value(pair.Key));
                }
                outFeatureBuffer.Shape = (shape as IPolygon).GetPolygonCenter();//获取多边形的中心点
                outFeatureCursor.InsertFeature(outFeatureBuffer);
            }, true);

            outFeatureCursor.Flush();

            Marshal.ReleaseComObject(sourceClass);
            Marshal.ReleaseComObject(outFeatureBuffer);
            Marshal.ReleaseComObject(outFeatureCursor);
            Marshal.ReleaseComObject(outClass);

            return(outClass);
        }