/// <summary> /// 合并几何体 /// </summary> /// <returns></returns> private IGeometry GetMergeGeometry(string str) { IGeometryBag pGeometryBag = new GeometryBag() as IGeometryBag; pGeometryBag.SpatialReference = GetSpatialReference(); IGeometryCollection pGeometryCollection = pGeometryBag as IGeometryCollection; // 属性过滤 IQueryFilter pQueryFilter = new QueryFilter(); pQueryFilter.WhereClause = str; IFeatureLayer pFeatureLayer = mLayer as IFeatureLayer; IFeatureClass in_FeatureClass = pFeatureLayer.FeatureClass; // 要素游标 IFeatureCursor pFeatureCursor = in_FeatureClass.Search(pQueryFilter, true); IFeature pFeature = pFeatureCursor.NextFeature(); if (pFeature == null) { return null; } // 遍历游标 object missing = Type.Missing; while (pFeature != null) { pGeometryCollection.AddGeometry(pFeature.ShapeCopy, ref missing, ref missing); pFeature = pFeatureCursor.NextFeature(); } Marshal.ReleaseComObject(pFeatureCursor); // 合并要素 ITopologicalOperator pTopologicalOperator = null; if (in_FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint) { pTopologicalOperator = new Multipoint() as ITopologicalOperator; pTopologicalOperator.ConstructUnion(pGeometryCollection as IEnumGeometry); } else if (in_FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline) { pTopologicalOperator = new Polyline() as ITopologicalOperator; pTopologicalOperator.ConstructUnion(pGeometryCollection as IEnumGeometry); } else { pTopologicalOperator = new Polygon() as ITopologicalOperator; pTopologicalOperator.ConstructUnion(pGeometryCollection as IEnumGeometry); } return pTopologicalOperator as IGeometry; }
/// <summary> /// 缝隙检查。融合后去内环。 /// </summary> /// <param name="pFeature"></param> /// <returns></returns> public List <Dictionary <string, IGeometry> > CheckFeatureGap(IFeatureClass pFClass, string inputtext) { List <Dictionary <string, IGeometry> > listGeo = new List <Dictionary <string, IGeometry> >(); if (pFClass == null) { return(null); } //Stopwatch watch = new Stopwatch(); //watch.Start(); //获取空间参考 IGeometry geometryBag = new GeometryBagClass(); IGeoDataset geoDataset = pFClass as IGeoDataset; geometryBag.SpatialReference = geoDataset.SpatialReference; ////属性过滤 //ISpatialFilter queryFilter = new SpatialFilterClass(); //queryFilter.SubFields = "Shape"; IFeatureCursor featureCursor = pFClass.Search(null, false); // 遍历游标 IFeature currentFeature = featureCursor.NextFeature(); if (currentFeature == null) { return(null); } 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; if (pFClass.ShapeType == esriGeometryType.esriGeometryPoint) { unionedPolygon = new Multipoint() as ITopologicalOperator; unionedPolygon.ConstructUnion(geometryCollection as IEnumGeometry); } else if (pFClass.ShapeType == esriGeometryType.esriGeometryPolyline) { unionedPolygon = new Polyline() as ITopologicalOperator; unionedPolygon.ConstructUnion(geometryCollection as IEnumGeometry); } else { 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) { Console.WriteLine(feature.OID); 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)) { Dictionary <string, IGeometry> itemDic = new Dictionary <string, IGeometry>(); itemDic.Add(feature.OID.ToString(), inRing); listGeo.Add(itemDic); flag = false; } } feature = o.NextFeature(); } Marshal.ReleaseComObject(o); } } } if (listGeo.Count > 0) { return(listGeo); } else { return(null); } }