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

 

    public class PolygonGapChecker : CheckProgressMessageSender, IChecker, ICheckProgressChangeEvent
    {
        private IFeatureLayer featureLayer;
        /// <summary>
        /// 待检查的面要素类图层
        /// </summary>
        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;
        /// <summary>
        /// 受检要素类的要素个数
        /// </summary>
        public int FeatureCount { get { return featureCount; } }

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

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

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

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

       /// <summary>
       /// 检查方法
       /// </summary>
       /// <returns>检查结果要素类</returns>
        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;
        /// <summary>
        /// 进度改变事件
        /// </summary>
        public  event CheckProgressChangeEventHandler CheckProgressChangeEvent
        {
            add
            {
                this.checkProgressChangeEventHandler += value;
            }

            remove
            {
                this.checkProgressChangeEventHandler-=value;
            }
        }

    }

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

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

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

上一篇:梵高数字时钟电脑屏保「windows电脑屏幕保护」


下一篇:win 10 禁用后置摄像头