1 /// <summary> 2 /// 根据面要素的ID获取面,判断点是否在面内 3 /// </summary> 4 /// <param name="point">要判断的点,射线的起点</param> 5 /// <param name="ID">面的ID</param> 6 /// <param name="pFeatureLayer">面要素所在的图层</param> 7 /// <returns></returns> 8 private bool PointIsInPolygon(IPoint point, int ID, IFeatureLayer pFeatureLayer) 9 { 10 IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; 11 IPolygon pPolygon = (pFeatureClass.GetFeature(ID)).Shape as IPolygon; 12 //与多边形的最大最小XY值比较 13 IEnvelope pEnvelope = new EnvelopeClass(); 14 pEnvelope = pPolygon.Envelope; 15 if(point.X<pEnvelope.XMin||point.X>pEnvelope.XMax||point.Y<pEnvelope.YMin||point.Y>pEnvelope.YMax) 16 return false; 17 //如果在这个范围内,向左作射线,根据线与面的拓扑判断交点个数 18 try { 19 IPoint toPoint = new PointClass(); 20 toPoint.PutCoords(getXminValue(pPolygon) - 20.00000000, point.Y); 21 IPolyline pLine = new PolylineClass();//定义射线 22 pLine.ToPoint = toPoint; 23 pLine.FromPoint = point; 24 25 ITopologicalOperator pTopo = pPolygon as ITopologicalOperator; 26 IGeometryCollection pGeoCol = pTopo.Intersect((IGeometry)pLine, esriGeometryDimension.esriGeometry0Dimension) as IGeometryCollection; 27 IPointCollection pPointCol = pGeoCol as IPointCollection; 28 if ((pPointCol.PointCount) % 2 == 0) 29 { 30 return false; 31 } 32 else 33 { 34 return true; 35 } 36 37 } 38 catch (Exception ee) 39 { 40 MessageBox.Show(ee.Message); 41 return false; 42 } 43 } 44 /// <summary> 45 /// 获取polygon的X最小值 46 /// </summary> 47 /// <param name="pPolygon"></param> 48 /// <returns></returns> 49 private double getXminValue(IPolygon pPolygon) 50 { 51 IPointCollection pPointCollection = pPolygon as IPointCollection; 52 int n = pPointCollection.PointCount; 53 double[] coordx = new double[n]; 54 for (int i = 0; i < n; i++) 55 { 56 IPoint point = pPointCollection.get_Point(i); 57 coordx[i] = point.X; 58 } 59 System.Array.Sort(coordx); 60 return coordx[0]; 61 }
时间: 2024-10-25 04:43:51