ArcGIS 面要素缝隙孔洞查询代码 C# GP

2021-01-27 00:13

阅读:513

标签:open   exist   存在   rtt   extent   查询   arc   send   ble   

 

    public class PolygonGapChecker : CheckProgressMessageSender, IChecker, ICheckProgressChangeEvent
    {
        private IFeatureLayer featureLayer;
        /// 
        /// 待检查的面要素类图层
        /// 
        public IFeatureLayer FeatureLayer
        {
            set
            {
                if (value != null)
                {
                    if (value.FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                        featureLayer = value;
                    else
                        throw new ArgumentException("要素类不是期望的类型,应是面要素类。");
                }
                else
                    throw new ArgumentException("要素类对象传入的是null");
            }
        }

        private int featureCount = -1;
        /// 
        /// 受检要素类的要素个数
        /// 
        public int FeatureCount { get { return featureCount; } }

        //private double distance = 0.2;
        /// 
        /// 距离值,一个大于零的值
        /// 
        public double Distance { set { } }

        private double tolerance = 0.001;
        /// 
        /// 容差值,一个大于零的值
        /// 
        public double Tolerance { set {
                if (value 0 )
                    throw new ArgumentException("容差应是一个大于0的小数。");
                else
                    tolerance = value;
            }
        }

        //private double angle = 10.0;
        /// 
        /// 角度值,一个大于零的值
        /// 
        public double Angle { set { } }
        
        private string workspacePath = string.Empty;
        /// 
        /// FGDB或文件夹的路径
        /// 
        public string WorkspacePath
        {
            set
            {
                if (System.IO.Directory.Exists(value))
                    workspacePath = value;
                else
                    throw new ArgumentException("给定FGDB或文件夹的路径不存在!");
            }
        }

        private readonly CheckerType checkerType = CheckerType.面缝隙或孔洞;
        /// 
        /// 检查类型
        /// 
        public CheckerType CheckerType { get { return checkerType; } }

       /// 
       /// 检查方法
       /// 
       /// 检查结果要素类
        public IFeatureClass Check()
        {

            IFeatureClass featureClass_cover;
            //IFeatureClass featureClass_erase;
            //IFeatureClass featureClass_erase_explode;
            IFeatureClass featureClass_result;
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();
            string featureClass_cover_Name=featureLayer.Name+"_cover.shp";
            string featureClass_erase_Name = $"{workspacePath}\\{featureLayer.Name}_erase.shp";
            string featureClass_result_Name = $"{workspacePath}\\{featureLayer.Name}_gap.shp";
            if (workspacePath.Split(.).Last().ToLower() == "gdb")
            {
                //确定要素类的名称,如workspace是文件地理数据库那么要素类名字最后没有有.shp。
                featureClass_cover_Name= featureClass_cover_Name.Replace(".shp", "");
                featureClass_erase_Name=featureClass_erase_Name.Replace(".shp", "");
                featureClass_result_Name=featureClass_result_Name.Replace(".shp", "");
                //若是文件地理数据库,应实例化FileGDBWorkspaceFactoryClass对象。
                workspaceFactory = new FileGDBWorkspaceFactoryClass();
            }
            IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(workspacePath, 0) as IFeatureWorkspace;

            #region 创建Cover要素类,用于被擦除
            FeatureClassCreator featureClassCreator = new FeatureClassCreator(featureLayer, featureClass_cover_Name, esriGeometryType.esriGeometryPolygon, workspacePath);
            featureClass_cover = featureClassCreator.Create();
            OnCheckProgresChange(checkProgressChangeEventHandler, ">>>成功创建Cover。");
            IPolygon polygon_cover = new PolygonClass();
            IPoint pointUpperLeft = new PointClass { X = (featureLayer.FeatureClass as IGeoDataset).Extent.UpperLeft.X - 100, Y = (featureLayer.FeatureClass as IGeoDataset).Extent.UpperLeft.Y-100 };
            IPoint pointUpperRight = new PointClass { X = (featureLayer.FeatureClass as IGeoDataset).Extent.UpperRight.X + 100, Y = (featureLayer.FeatureClass as IGeoDataset).Extent.UpperRight.Y-100 };
            IPoint pointLowerRight = new PointClass { X = (featureLayer.FeatureClass as IGeoDataset).Extent.LowerRight.X + 100, Y = (featureLayer.FeatureClass as IGeoDataset).Extent.LowerRight.Y+100 };
            IPoint pointLowerLeft = new PointClass { X = (featureLayer.FeatureClass as IGeoDataset).Extent.LowerLeft.X - 100, Y = (featureLayer.FeatureClass as IGeoDataset).Extent.LowerLeft.Y+100 };
            IPointCollection pointCollection_cover = polygon_cover as IPointCollection;
            pointCollection_cover.AddPoint(pointUpperLeft);
            pointCollection_cover.AddPoint(pointUpperRight);
            pointCollection_cover.AddPoint(pointLowerRight);
            pointCollection_cover.AddPoint(pointLowerLeft);
            pointCollection_cover.AddPoint(pointUpperLeft);
            IFeature feature_cover= featureClass_cover.CreateFeature();
            feature_cover.Shape = polygon_cover;
            feature_cover.Store();
            OnCheckProgresChange(checkProgressChangeEventHandler, ">>>成功创建Cover要素,准备执行Erase。");
            #endregion

            Geoprocessor.Geoprocessor gp = new Geoprocessor.Geoprocessor
            {
                AddOutputsToMap = false,
                OverwriteOutput = true
            };
            Erase erase = new Erase
            {
                in_features = $"{workspacePath}\\{featureClass_cover_Name}",
                erase_features = featureLayer,
                out_feature_class = featureClass_erase_Name,
                cluster_tolerance = this.tolerance,
            };
            gp.Execute(erase, null);
            OnCheckProgresChange(checkProgressChangeEventHandler, ">>>成功创建Erase,准备执行Explode。");
            MultipartToSinglepart multipartToSinglepart = new MultipartToSinglepart
            {
                in_features = featureClass_erase_Name,
                out_feature_class = featureClass_result_Name
            };
            gp.Execute(multipartToSinglepart, null);

            featureClass_result = featureWorkspace.OpenFeatureClass(featureClass_result_Name.Replace($"{workspacePath}\\", ""));
            CheckAuxiliaryHelper.SpatialSearchAndDeleteFeatures(featureClass_result, pointUpperLeft, esriSpatialRelEnum.esriSpatialRelIntersects);
            featureClass_result.DeleteField(featureClass_result.Fields.Field[featureClass_result.FindField("ORIG_FID")]);
            CheckAuxiliaryHelper.DeleteFeatureClass(featureClass_cover_Name, featureWorkspace);
            CheckAuxiliaryHelper.DeleteFeatureClass(featureClass_erase_Name, featureWorkspace);
            return featureClass_result;
        }
        private CheckProgressChangeEventHandler checkProgressChangeEventHandler;
        /// 
        /// 进度改变事件
        /// 
        public  event CheckProgressChangeEventHandler CheckProgressChangeEvent
        {
            add
            {
                this.checkProgressChangeEventHandler += value;
            }

            remove
            {
                this.checkProgressChangeEventHandler-=value;
            }
        }

    }

技术图片

技术图片

ArcGIS 面要素缝隙孔洞查询代码 C# GP

标签:open   exist   存在   rtt   extent   查询   arc   send   ble   

原文地址:https://www.cnblogs.com/yzhyingcool/p/11963660.html


评论


亲,登录后才可以留言!