public bool CheckFeature(IFeature pFeature, ref object pErrFeatureInf) { IFieldEdit edit = this._layer.FeatureClass.Fields.get_Field(this._layer.FeatureClass.Fields.FindField(this._layer.FeatureClass.ShapeFieldName)) as IFieldEdit; ISpatialReference spatialReference = edit.GeometryDef.SpatialReference; bool flag = true; List <double[]> list = (List <double[]>)pErrFeatureInf; IGeometryCollection shape = pFeature.Shape as IGeometryCollection; for (int i = 0; i < shape.GeometryCount; i++) { esriNonSimpleReasonEnum enum2 = esriNonSimpleReasonEnum.esriNonSimpleOK; IPointCollection newPoints = shape.get_Geometry(i) as IPointCollection; PolylineClass o = new PolylineClass(); o.AddPointCollection(newPoints); o.SpatialReference = spatialReference; ITopologicalOperator3 ioperator = o as ITopologicalOperator3; ioperator.IsKnownSimple_2 = false; if (!ioperator.get_IsSimpleEx(out enum2)) { IRing ring = newPoints as IRing; int num2 = 0; if (ring.IsClosed) { num2 = 1; } flag = false; List <string> list2 = new List <string>(); List <string> list3 = new List <string>(); for (int j = num2; j < newPoints.PointCount; j++) { IPoint point = newPoints.get_Point(j); string item = point.X.ToString() + "," + point.Y.ToString(); if (list2.Contains(item)) { if (!list3.Contains(item)) { double[] numArray = new double[] { point.X, point.Y }; list.Add(numArray); list3.Add(item); } } else { list2.Add(item); } } } Marshal.ReleaseComObject(o); o = null; } return(flag); }
/// <summary> /// 判断图形是否存在几何错误, /// <para>True表示存在错误,False表示未检查出错误,</para> /// <para>esriNonSimpleReasonEnum参数表示检查出的错误类型(自相交、非闭合环、不正确环走向、不正确线段方向、短线段等)</para> /// </summary> /// <param name="geometry"></param> /// <param name="eNonSimpleReason">几何错误类型,参考:http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//001700000034000000 ,或参考<see cref="GeometryCheckInfo.GetGeometryCheckInfos"/></param> /// <returns>True表示存在指定几何错误,False表示不存在指定几何错误</returns> public static bool Check(this IGeometry geometry, out esriNonSimpleReasonEnum eNonSimpleReason) { ITopologicalOperator3 topologicalOperator = (ITopologicalOperator3)geometry; topologicalOperator.IsKnownSimple_2 = false;//布尔型,指示此几何图形是否是已知的(或假设)是拓扑正确。这里赋值false,就是非已知的几何图形 eNonSimpleReason = esriNonSimpleReasonEnum.esriNonSimpleOK; if (!topologicalOperator.get_IsSimpleEx(out var reason))//返回布尔值,指示该几何图形是否为简单的。如果返回的是false,则可以对输出的"reason"参数检查审查 { eNonSimpleReason = reason; } return(eNonSimpleReason != esriNonSimpleReasonEnum.esriNonSimpleOK); }
/// <summary> /// 判断是否自相交 /// </summary> /// <param name="geometry"></param> /// <returns>True表示自相交,False表示不自相交</returns> public static bool IsSelfCross(this IGeometry geometry) { ITopologicalOperator3 topologicalOperator = (ITopologicalOperator3)geometry; topologicalOperator.IsKnownSimple_2 = false;//布尔型,指示此几何图形是否是已知的(或假设)是拓扑正确。这里赋值false,就是非已知的几何图形 //reason枚举参考:http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//001700000034000000 //返回布尔值,指示该几何图形是否为简单的。如果返回的是false,则可以对输出的"reason"参数检查审查 if (!topologicalOperator.get_IsSimpleEx(out var reason)) { if (reason == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections) { return(true); } } return(false); }
public void OtherRule(string idname, string checkname, string IN_RuleType, string TABLENAME, string SUPTABLE, string inputtext, IHookHelper m_hookHelper) { CommonClass common = new CommonClass(); IFeatureClass IN_FeatureClass = common.GetLayerByName(TABLENAME).FeatureClass; IGeoDataset pGeoDataset = IN_FeatureClass as IGeoDataset; ISpatialReference spatialReference = pGeoDataset.SpatialReference; IFeatureClass IN_Sup_FeatureClass = null; if (SUPTABLE != null) { IN_Sup_FeatureClass = common.GetLayerByName(SUPTABLE).FeatureClass; } string ErrorFilePath = this.topoDir + "\\" + checkname + idname + ".shp"; if (IN_RuleType == "面多部件检查") { try { common.CreatShpFile(this.topoDir, spatialReference, esriGeometryType.esriGeometryPolygon, checkname + idname); List <ErrorEntity> list = new List <ErrorEntity>(); IFeatureCursor cursor = IN_FeatureClass.Search(null, false); int tempCount = 0; IFeature pFeature = cursor.NextFeature(); while (pFeature != null) { IGeometry pGeo = pFeature.ShapeCopy; ITopologicalOperator pTopoOperator = pGeo as ITopologicalOperator; int iCount = 0; if (IN_FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { iCount = (pGeo as IPolygon).ExteriorRingCount; } else if (IN_FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline) { iCount = ((pGeo as IPolyline) as IGeometryCollection).GeometryCount; } else if (IN_FeatureClass.ShapeType == esriGeometryType.esriGeometryMultipoint) { iCount = ((pGeo as IMultipoint) as IPointCollection).PointCount; } if (iCount > 1) { tempCount++; common.GenerateSHPFile(pFeature.ShapeCopy, ErrorFilePath); } pFeature = cursor.NextFeature(); } //new ErrorTable().AddErr(list, ErrType.MultiPart, idname); if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, tempCount); } else { DicTopoError[idname] = tempCount; } Marshal.ReleaseComObject(cursor); if (tempCount == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); } } else if (IN_RuleType == "面自相交检查") { int errorCount = 0; try { common.CreatShpFile(this.topoDir, spatialReference, esriGeometryType.esriGeometryPoint, checkname + idname); IFeatureLayer pLayer = new FeatureLayer(); pLayer.FeatureClass = IN_FeatureClass; TopoClassChecker topo = new TopoClassChecker(); IFeatureCursor cursor; IQueryFilter filter = new QueryFilterClass(); filter.SubFields = pLayer.FeatureClass.OIDFieldName + "," + pLayer.FeatureClass.ShapeFieldName; cursor = pLayer.FeatureClass.Search(filter, true); IFeature feature = null; object missing = Type.Missing; while ((feature = cursor.NextFeature()) != null) { IPoint tempPoint = null; StringBuilder builder = new StringBuilder(); IGeometryCollection shape = feature.Shape as IGeometryCollection; for (int i = 0; i < shape.GeometryCount; i++) { esriNonSimpleReasonEnum enum2; IPointCollection newPoints = shape.get_Geometry(i) as IPointCollection; IRing ring = newPoints as IRing; int num2 = 0; if (ring.IsClosed) { num2 = 1; } PolylineClass o = new PolylineClass(); o.AddPointCollection(newPoints); o.SpatialReference = spatialReference; ITopologicalOperator3 @operator = o; @operator.IsKnownSimple_2 = false; if ([email protected]_IsSimpleEx(out enum2) && (enum2 == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections)) { List <string> list2 = new List <string>(); List <string> list3 = new List <string>(); for (int j = num2; j < newPoints.PointCount; j++) { IPoint point = newPoints.get_Point(j); tempPoint = point; string item = point.X.ToString() + "," + point.Y.ToString(); if (list2.Contains(item)) { if (!list3.Contains(item)) { builder.Append(";"); builder.Append(item); list3.Add(item); } } else { list2.Add(item); } } } Marshal.ReleaseComObject(o); o = null; } if (builder.Length > 0) { errorCount++; string[] strArray = builder.ToString().Substring(1).Split(new char[] { ';' }); ESRI.ArcGIS.Geometry.IPointCollection pPointCollection1 = new ESRI.ArcGIS.Geometry.MultipointClass(); foreach (string str in strArray) { if (!string.IsNullOrEmpty(str)) { string[] strArray2 = str.Split(new char[] { ',' }); double pX = double.Parse(strArray2[0]); double pY = double.Parse(strArray2[1]); ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass(); point.X = pX; point.Y = pY; common.GenerateSHPFile(point, ErrorFilePath); } } } } Marshal.ReleaseComObject(cursor); if (errorCount == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, errorCount); } else { DicTopoError[idname] = errorCount; } } else if (IN_RuleType == "缝隙检查") { int errorCount = 0; try { common.CreatShpFile(this.topoDir, spatialReference, esriGeometryType.esriGeometryPolygon, checkname + idname); this.m_hookHelper = m_hookHelper; IFeatureClass pFClass = IN_FeatureClass; //获取空间参考 IGeometry geometryBag = new GeometryBagClass(); IGeoDataset geoDataset = pFClass as IGeoDataset; geometryBag.SpatialReference = geoDataset.SpatialReference; ////属性过滤 IFeatureCursor featureCursor = pFClass.Search(null, false); // 遍历游标 IFeature currentFeature = featureCursor.NextFeature(); IGeometryCollection geometryCollection = geometryBag as IGeometryCollection; object missing = Type.Missing; while (currentFeature != null) { geometryCollection.AddGeometry(currentFeature.Shape, ref missing, ref missing); currentFeature = featureCursor.NextFeature(); } // 合并要素 ITopologicalOperator unionedPolygon = null; unionedPolygon = new Polygon() as ITopologicalOperator; unionedPolygon.ConstructUnion(geometryCollection as IEnumGeometry); Marshal.ReleaseComObject(featureCursor); IPolygon4 pMergerPolygon = unionedPolygon as IPolygon4; IGeometryBag pOutGeometryBag = pMergerPolygon.ExteriorRingBag; //获取外部环 IGeometryCollection pOutGmtyCollection = pOutGeometryBag as IGeometryCollection; for (int i = 0; i < pOutGmtyCollection.GeometryCount; i++) //对外部环遍历 { IGeometry pOutRing = pOutGmtyCollection.get_Geometry(i); //外部环 //【此处可以对外部环进行操作】 IPointCollection pOutRingCollection = pOutRing as IPointCollection; for (int j = 0; j < pOutRingCollection.PointCount; j++) { IPoint pOutRingPoint = pOutRingCollection.get_Point(j);//获取外环上的点 } IGeometryBag pInteriotGeometryBag = pMergerPolygon.get_InteriorRingBag(pOutRing as IRing); //获取内部环 IGeometryCollection pInteriorGeometryCollection = pInteriotGeometryBag as IGeometryCollection; for (int j = 0; j < pInteriorGeometryCollection.GeometryCount; j++) { ISegmentCollection SegCol = pInteriorGeometryCollection.get_Geometry(j) as ISegmentCollection; IPolygon PPolygon = new PolygonClass(); ISegmentCollection newSegCol = PPolygon as ISegmentCollection; newSegCol.AddSegmentCollection(SegCol); //pInteriorGeometry即为多边形的内部环 IGeometry inRing = PPolygon as IGeometry; inRing.SpatialReference = geometryBag.SpatialReference; IArea area = inRing as IArea; Double getarea = System.Math.Abs(Convert.ToDouble(area.Area)); if (inputtext == null || inputtext == "" || getarea < Convert.ToDouble(inputtext)) { Boolean flag = true; ISpatialFilter filter = new SpatialFilterClass(); filter.Geometry = inRing; filter.SubFields = pFClass.OIDFieldName + "," + pFClass.ShapeFieldName; filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureCursor o = pFClass.Search(filter, true); IFeature feature = o.NextFeature(); while (feature != null && flag) { IPolygon4 pPolygon = feature.Shape as IPolygon4; IGeometryBag iOutGeometryBag = pPolygon.ExteriorRingBag; //获取外部环 IGeometryCollection iOutGmtyCollection = iOutGeometryBag as IGeometryCollection; for (int m = 0; m < iOutGmtyCollection.GeometryCount && flag; m++) //对外部环遍历 { IGeometry outGeo = iOutGmtyCollection.get_Geometry(m); IGeometryCollection polyGonGeo = new PolygonClass(); polyGonGeo.AddGeometry(outGeo); IPolygon iPolygon = polyGonGeo as IPolygon; iPolygon.SimplifyPreserveFromTo(); IRelationalOperator2 pRelationalOperator2 = iPolygon as IRelationalOperator2; if (!pRelationalOperator2.Contains(inRing)) { errorCount++; common.GenerateSHPFile(inRing, ErrorFilePath); flag = false; } } feature = o.NextFeature(); } Marshal.ReleaseComObject(o); } } if (errorCount == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, errorCount); } else { DicTopoError[idname] = errorCount; } } else if (IN_RuleType == "面重叠检查") { IFeatureClass outIFC = null; this.m_hookHelper = m_hookHelper; ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); string outString = this.topoDir + "\\" + checkname + idname + ".shp"; intersect.in_features = common.GetPathByName(TABLENAME); intersect.out_feature_class = outString; Geoprocessor geoProcessor = new Geoprocessor(); geoProcessor.OverwriteOutput = true; try { if (this.gp == null) { this.gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); } IGeoProcessorResult result = (IGeoProcessorResult)this.gp.Execute(intersect, null); if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded) { Console.WriteLine("gp工具执行错误"); } else { outIFC = this.gp.Open(result.ReturnValue) as IFeatureClass; } } catch (Exception ex) { LogHelper.WriteLog(typeof(TopoChecker), ex); Console.WriteLine(ex.Message); } if (outIFC.FeatureCount(null) == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, outIFC.FeatureCount(null)); } else { DicTopoError[idname] = outIFC.FeatureCount(null); } } else if (IN_RuleType == "面重叠检查(与其他图层)") { IFeatureClass outIFC = null; this.m_hookHelper = m_hookHelper; ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect(); string outString = this.topoDir + "\\" + checkname + idname + ".shp"; intersect.in_features = @common.GetPathByName(TABLENAME) + ";" + @common.GetPathByName(SUPTABLE); intersect.out_feature_class = outString; Geoprocessor geoProcessor = new Geoprocessor(); geoProcessor.OverwriteOutput = true; try { if (this.gp == null) { this.gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); } IGeoProcessorResult result = (IGeoProcessorResult)this.gp.Execute(intersect, null); if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded) { Console.WriteLine("gp工具执行错误"); } else { outIFC = this.gp.Open(result.ReturnValue) as IFeatureClass; } } catch (Exception ex) { Console.WriteLine(ex.Message); LogHelper.WriteLog(typeof(TopoChecker), ex); } if (outIFC.FeatureCount(null) == 0) { string shpFolder = System.IO.Path.GetDirectoryName(ErrorFilePath); IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass(); IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(ErrorFilePath, 0); IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace; if (System.IO.File.Exists(ErrorFilePath)) { IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(ErrorFilePath); if (pFCChecker != null) { IDataset pds = pFCChecker as IDataset; pds.Delete(); } } } if (!DicTopoError.ContainsKey(idname)) { DicTopoError.Add(idname, outIFC.FeatureCount(null)); } else { DicTopoError[idname] = outIFC.FeatureCount(null); } } Marshal.ReleaseComObject(IN_FeatureClass); if (SUPTABLE != null) { Marshal.ReleaseComObject(IN_Sup_FeatureClass); } }
public List <ErrorEntity> AreaSelfIntersect(string idname, IFeatureLayer pLayer) { IFeatureCursor cursor; IQueryFilter filter = new QueryFilterClass(); filter.SubFields = pLayer.FeatureClass.OIDFieldName + "," + pLayer.FeatureClass.ShapeFieldName; cursor = pLayer.FeatureClass.Search(filter, true); IFieldEdit edit = pLayer.FeatureClass.Fields.get_Field(pLayer.FeatureClass.Fields.FindField(pLayer.FeatureClass.ShapeFieldName)) as IFieldEdit; ISpatialReference spatialReference = edit.GeometryDef.SpatialReference; List <ErrorEntity> list = new List <ErrorEntity>(); IFeature feature = null; object missing = Type.Missing; while ((feature = cursor.NextFeature()) != null) { IPoint tempPoint = null; StringBuilder builder = new StringBuilder(); IGeometryCollection shape = feature.Shape as IGeometryCollection; for (int i = 0; i < shape.GeometryCount; i++) { esriNonSimpleReasonEnum enum2; IPointCollection newPoints = shape.get_Geometry(i) as IPointCollection; IRing ring = newPoints as IRing; int num2 = 0; if (ring.IsClosed) { num2 = 1; } PolylineClass o = new PolylineClass(); o.AddPointCollection(newPoints); o.SpatialReference = spatialReference; ITopologicalOperator3 @operator = o; @operator.IsKnownSimple_2 = false; if ([email protected]_IsSimpleEx(out enum2) && (enum2 == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections)) { List <string> list2 = new List <string>(); List <string> list3 = new List <string>(); for (int j = num2; j < newPoints.PointCount; j++) { IPoint point = newPoints.get_Point(j); tempPoint = point; string item = point.X.ToString() + "," + point.Y.ToString(); if (list2.Contains(item)) { if (!list3.Contains(item)) { builder.Append(";"); builder.Append(item); list3.Add(item); } } else { list2.Add(item); } } } Marshal.ReleaseComObject(o); o = null; } if (builder.Length > 0) { list.Add(new ErrorEntity(idname, feature.OID.ToString(), "自相交", builder.ToString().Substring(1), ErrType.SelfIntersect, tempPoint)); } } return(list); }