/// <summary> /// 获取指定空间范围下相交的要素集合 /// </summary> /// <param name="featureClass">被查询要素类</param> /// <param name="queryClause">查询条件</param> /// <param name="queryGeometry">指定空间范围</param> /// <param name="intersectFeatureDTOs">输出相交要素信息</param> /// <param name="sumIntersectArea">输出相交部分的总面积</param> /// <param name="geometryField">geometry字段名称,默认为Shape</param> public static void GetIntersectFeaturesByQuery(IFeatureClass featureClass, string queryClause, IGeometry queryGeometry, out IList <IntersectFeatureDTO> intersectFeatureDTOs, out double sumIntersectArea, string geometryField = "Shape") { List <IFeature> featureList = new List <IFeature>(); intersectFeatureDTOs = new List <IntersectFeatureDTO>(); sumIntersectArea = 0d; try { ISpatialFilter spatialFilter = new SpatialFilter(); if (queryClause != null) { spatialFilter.WhereClause = queryClause; } if (queryGeometry != null) { spatialFilter.Geometry = queryGeometry; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; } if (geometryField != null) { spatialFilter.GeometryField = geometryField; } IFeatureCursor featureCursor = featureClass.Search(spatialFilter, false); IFeature feature = featureCursor.NextFeature(); IntersectFeatureDTO intersectFeatureDTOTemp = null; while (feature != null) { IGeometry intersectGeometry = OverlayCommonTool.GetIntersectGeometry(queryGeometry as IPolygon, feature.Shape as IPolygon); if (null == intersectGeometry || intersectGeometry.IsEmpty) { continue; } intersectFeatureDTOTemp = new IntersectFeatureDTO { Geometry = feature.Shape, OID = feature.OID, Coordinates = GeometryUtil.ConvertGeometryToJson(intersectGeometry, out string message), Area = GeometryUtil.GetArea(intersectGeometry), QueryGeometry = queryGeometry, IntersectGeometry = intersectGeometry, GeoType = EnumUtil.GetEnumName <esriGeometryType>((int)intersectGeometry.GeometryType) }; intersectFeatureDTOs.Add(intersectFeatureDTOTemp); IArea area = intersectGeometry as IArea; double intersectArea = area.Area; sumIntersectArea += intersectArea; featureList.Add(feature); feature = featureCursor.NextFeature(); } WorkspaceUtil.ReleaseComObject(featureCursor); } catch (Exception e) { LOG.Error(e); } }
public override Result Execute() { if (!base.InitComplete) { throw new BusinessException((int)EnumSystemStatusCode.DME_FAIL_INIT, "初始化工作未完成"); } DMEWorkspaceBridge <IWorkspace, IFeatureClass> dmeWorkspaceBridge = new DMEWorkspaceBridge <IWorkspace, IFeatureClass>(); dmeWorkspaceBridge.SetWorkspace(new EsriWorkspace()); // 目前先以ORACLE和MDB为数据源类型 // 获取source feature class this.SourceFeatureClass = dmeWorkspaceBridge.GetFeatureClass(this.sourceFeatureClassDTO); // 获取target feature class this.TargetFeatureClass = dmeWorkspaceBridge.GetFeatureClass(this.targetFeatureClassDTO); if (AnalysisType.COVER == this.AnalysisType) { OverlayCommonTool.GetTopounionGeometryByQuery(this.SourceFeatureClass, null, null, esriSpatialRelEnum.esriSpatialRelUndefined, out IGeometry sourceGeom, out IList <string> oidList); //找出touch的要素oid,排除掉touch的图形 OverlayCommonTool.GetTopounionGeometryByQuery(this.TargetFeatureClass, null, sourceGeom, esriSpatialRelEnum.esriSpatialRelTouches, out IGeometry touchGeom, out IList <string> touchOIDList); //通过intersect查找压盖部分的要素 string queryClause = null; if (touchOIDList != null && touchOIDList.Count > 0) { queryClause = $"OBJECTID NOT in ({string.Join(",", ((List<string>)touchOIDList).ToArray())})"; } // 求取空间相交的部分 OverlayCommonTool.GetIntersectFeaturesByQuery(this.TargetFeatureClass, queryClause, sourceGeom, out IList <IntersectFeatureDTO> intersectFeatureDTOs, out double sumIntersectArea); if (intersectFeatureDTOs?.Count > 0) { OverlayRespDTO overlayRespDTO = new OverlayRespDTO { SumArea = sumIntersectArea }; foreach (var item in intersectFeatureDTOs) { FeatureRespDTO intersectFeatureRespDTO = new FeatureRespDTO { OID = item.OID, Area = item.Area, Coordinates = item.Coordinates, GeoType = item.GeoType }; overlayRespDTO.Features.Add(intersectFeatureRespDTO); } Property resultProp = base.OutputParametersMeta[nameof(Result)]; resultProp.Value = overlayRespDTO; return(new Result(EnumSystemStatusCode.DME_SUCCESS, "运行完成", EnumSystemStatusCode.DME_SUCCESS, null)); } } else if (AnalysisType.OVERTOP == this.AnalysisType) { // 先拷贝一份mdb模板 string sTemplate = System.AppDomain.CurrentDomain.BaseDirectory + GlobalSystemConfig.PATH_TEMPLATE_PGDB;// @"\template\pgdb.mdb"; string sPath = System.AppDomain.CurrentDomain.BaseDirectory + GlobalSystemConfig.DIR_TEMP + "/" + GuidUtil.NewGuid() + ".mdb"; File.Copy(sTemplate, sPath); IWorkspace mdbWorkspace = WorkspaceUtil.OpenMdbWorspace(sPath); if (FeatureClassUtil.ExportToWorkspace(this.SourceFeatureClass, mdbWorkspace)) { // 获取导出的临时要素类信息 IFeatureClass tempExpFeatureClass = WorkspaceUtil.GetFeatureClass(mdbWorkspace, this.sourceFeatureClassDTO.Name); // 进行擦除操作 OverlayCommonTool.Erase(tempExpFeatureClass, this.TargetFeatureClass); // 计算完保存结果,如何保存? if (tempExpFeatureClass != null) { OverlayRespDTO overlayRespDTO = new OverlayRespDTO(); // 总面积 double sumArea = 0; IFeatureCursor pFeatureCursor = tempExpFeatureClass.Search(null, false); IFeature pFeature = null; //获得“Area”字段 //IFields fields = pFeatureCursor.Fields; //int areaIndex = fields.FindField("Area"); while ((pFeature = pFeatureCursor.NextFeature()) != null) { double area = ((IArea)pFeature.Shape).Area;// (double)pFeature.get_Value(areaIndex); sumArea += area; FeatureRespDTO featureRespDTO = new FeatureRespDTO { OID = pFeature.OID, Area = area, Coordinates = GeometryUtil.ConvertGeometryToJson(pFeature.Shape, out string msg) }; overlayRespDTO.Features.Add(featureRespDTO); pFeature = pFeatureCursor.NextFeature(); } overlayRespDTO.SumArea = sumArea; Property resultProp = base.OutputParametersMeta[nameof(Result)]; resultProp.Value = overlayRespDTO; return(new Result(EnumSystemStatusCode.DME_SUCCESS, "运行完成", EnumSystemStatusCode.DME_SUCCESS, null)); } } // 删除临时文件 if (IsClearTemp && File.Exists(sPath)) { File.Delete(sPath); } } else { throw new BusinessException((int)EnumSystemStatusCode.DME_FAIL, "分析类型不匹配"); } return(new Result(EnumSystemStatusCode.DME_SUCCESS, "运行完成,但没有运算结果", EnumSystemStatusCode.DME_SUCCESS, null)); }