protected IPolygon getPolygon(IPoint inPoint) { bool found = false; IRelationalOperator relOp = (IRelationalOperator)inPoint; IFeature f = null; IPolygon searchPoly = null; mLog.Debug("inside getPolygon"); try { for (int i = 0; i < this.myPolygons.Count && !found; i++) { f = (IFeature)myPolygons[i]; searchPoly = (IPolygon)f.ShapeCopy; if (relOp.Within(searchPoly)) { found = true; } } } catch (System.Exception ex) { #if (DEBUG) System.Windows.Forms.MessageBox.Show(ex.Message); #endif eLog.Debug(ex); } if (found) { return(searchPoly); } else { return(null); } }
protected double getArea(IPoint inPoint) { double area = 0; IPolygon searchPoly = null; IRelationalOperator relOp = null; IFeatureCursor searchCurr = null; IFeature feat = null; mLog.Debug("inside get area of the home range finder class for point X = " + inPoint.X.ToString() + " and Y = " + inPoint.Y.ToString()); try { relOp = (IRelationalOperator)inPoint; searchCurr = this.myMapManager.SocialMap.mySelf.Search(null, true); while ((feat = searchCurr.NextFeature()) != null) { searchPoly = feat.Shape as IPolygon; if (relOp.Within(searchPoly)) { area = this.getArea(searchPoly); break; } } } catch (System.Exception ex) { eLog.Debug(ex); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(searchPoly); System.Runtime.InteropServices.Marshal.ReleaseComObject(searchCurr); } mLog.Debug("leaving with an area of " + area.ToString()); return(area); }
protected double getArea(IPoint inPoint) { double area = 0; IPolygon searchPoly = null; IRelationalOperator relOp = null; IFeatureCursor searchCurr = null; fw.writeLine("inside get area of the home range finder class for point X = " + inPoint.X.ToString() + " and Y = " + inPoint.Y.ToString()); try { relOp = (IRelationalOperator)inPoint; searchCurr = myAvailableAreas.Search(null, true); IFeature feat = searchCurr.NextFeature(); while (feat != null) { searchPoly = feat.Shape as IPolygon; if (relOp.Within(searchPoly)) { area = this.getArea(searchPoly); break; } } } catch (System.Exception ex) { FileWriter.FileWriter.WriteErrorFile(ex); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(searchPoly); System.Runtime.InteropServices.Marshal.ReleaseComObject(searchCurr); } fw.writeLine("leaving with an area of " + area.ToString()); return(area); }
private void AddVertexNode(IPoint pPnt) { try { IFeatureLayer pFeaturelayer = m_EngineEditLayers.TargetLayer; IActiveView pActiveView = m_activeView; IPoint pPoint = pPnt; if (pFeaturelayer.FeatureClass == null) { return; } //如果不是面状地物则退出 if (pFeaturelayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryEnvelope && pFeaturelayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeaturelayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryLine && pFeaturelayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline) { return; } IGeometry pGeo = null; IFeature pSelFeature = null; pSelFeature = EditVertexClass.GetSelectedFeature(pFeaturelayer); //是否有选中的几何体 if (pSelFeature == null) { return; } //解决不带Z值的要素的编辑和Z值为空的要素的编辑问题 IZAware pZAware = pPoint as IZAware; pZAware.ZAware = true; pPoint.Z = 0; bool pInLine = false; ITopologicalOperator pTopoOpt = default(ITopologicalOperator); IPolyline pBoundaryLine = default(IPolyline); //最小的距离 double pMindis = 0; IProximityOperator pProxOpt = default(IProximityOperator); //得到多边形的边界 if (pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon || pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryEnvelope) { //获取边界线 pBoundaryLine = EditVertexClass.GetBoundary(pFeaturelayer); } else if (pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline || pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryLine) { pBoundaryLine = pSelFeature.ShapeCopy as IPolyline; } pTopoOpt = pPoint as ITopologicalOperator; IRelationalOperator pRelationalOperator = default(IRelationalOperator); pRelationalOperator = pPoint as IRelationalOperator; //判断点是否在边界上 pInLine = pRelationalOperator.Within(pBoundaryLine); //如果不在边界上,判断是否小于容忍距离,如果大于容忍距离则退出程序 if (pInLine == false) { pProxOpt = pPoint as IProximityOperator; pMindis = pProxOpt.ReturnDistance(pBoundaryLine); if (pMindis > THE_POINT_TO_POINT_TOLERANCE) { return; } } //判断是否增加的点刚好为节点 IPointCollection pPolylinePointCol = pBoundaryLine as IPointCollection; IHitTest pHitTest = default(IHitTest); double pHitDis = 0; int pSegIndex = 0; int pVerIndex = 0; IPoint pHitPoint = null; bool bRightSide = true; pHitTest = pBoundaryLine as IHitTest; //增加的点为已有的节点则退出程序 if (pHitTest.HitTest(pPoint, THE_POINT_TO_POINT_TOLERANCE * 10, esriGeometryHitPartType.esriGeometryPartVertex, pHitPoint, ref pHitDis, ref pSegIndex, ref pVerIndex, ref bRightSide) == true) { if (pHitDis < THE_POINT_TO_POINT_TOLERANCE) { return; } } EditVertexClass.pHitPnt = pHitPoint; //为多边形增加节点 ISegmentCollection pSegmentCollection = pBoundaryLine as ISegmentCollection; IPolyline pSegPolyline = null; int pPointIndex = 0; ILine pLine = default(ILine); double pDis1 = 0; double pDis2 = 0; IPoint pVerTex = default(IPoint); pMindis = 100; pProxOpt = pPoint as IProximityOperator; for (int i = 0; i <= pSegmentCollection.SegmentCount - 1; i++) { //判断选中点是否在这个Segment上 pLine = pSegmentCollection.get_Segment(i) as ILine; pDis1 = pProxOpt.ReturnDistance(pLine.FromPoint); pDis2 = pProxOpt.ReturnDistance(pLine.ToPoint); if (Math.Abs(pDis1 + pDis2 - pLine.Length) <= pMindis) { pMindis = Math.Abs(pDis1 + pDis2 - pLine.Length); pVerTex = pLine.ToPoint; } } //获取选中的几何特征 pGeo = pSelFeature.Shape; //得到索引 pPointIndex = EditVertexClass.GetVertexIndex(pVerTex, pGeo); //如果是首点,则设置为最后一个点 if (pPointIndex == 0) { pPointIndex = pSegmentCollection.SegmentCount; } IPointCollection pPolygonPointCol = null; pPolygonPointCol = pSelFeature.ShapeCopy as IPointCollection; pPolygonPointCol.InsertPoints(pPointIndex, 1, ref pPoint); m_EngineEditor.StartOperation(); //拓扑操作 IPolygon pPolygon = null; IPolyline pPlyline = null; if (pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon || pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryEnvelope) { pPolygon = pPolygonPointCol as IPolygon; pPolygon.Close(); pTopoOpt = pPolygon as ITopologicalOperator; pTopoOpt.Simplify(); pSelFeature.Shape = pPolygon; pSelFeature.Store(); } else if (pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline || pFeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryLine) { pPlyline = pPolygonPointCol as IPolyline; pTopoOpt = pPlyline as ITopologicalOperator; pTopoOpt.Simplify(); pSelFeature.Shape = pPlyline; pSelFeature.Store(); } //停止编辑 m_EngineEditor.StopOperation("AddVertexTool"); //显示顶点 EditVertexClass.ShowAllVertex(pFeaturelayer); } catch (Exception ex) { } }
public bool HitTestSelectionElement(IActiveView pActiveView, long x, long y, bool bMapUnit) { try { if (pActiveView != null) { IGraphicsContainerSelect select = pActiveView as IGraphicsContainerSelect; if (select.ElementSelectionCount <= 0) { return(false); } IPoint point = null; if (bMapUnit) { point = new PointClass { X = x, Y = y }; } else { point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint((int)x, (int)y); } IEnvelope bounds = new EnvelopeClass(); IBoundsProperties properties = null; IGeometry other = null; IRelationalOperator @operator = point as IRelationalOperator; IEnumElement selectedElements = null; selectedElements = select.SelectedElements; if (selectedElements == null) { return(false); } IElement element2 = null; selectedElements.Reset(); for (element2 = selectedElements.Next(); element2 != null; element2 = selectedElements.Next()) { if (!element2.Locked) { properties = element2 as IBoundsProperties; if (properties.FixedSize) { element2.QueryBounds(pActiveView.ScreenDisplay, bounds); } else { bounds = element2.Geometry.Envelope; } other = bounds; if (@operator.Within(other)) { return(true); } } } } return(false); } catch (Exception exception) { this.mErrOpt.ErrorOperate(this.mSubSysName, "FunFactory.ElementFun", "HitTestSelectionElement", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); return(false); } }
/// <summary> /// 根据查询图形在目标要素集中查找图形相同的要素 /// 返回查询到的第一个要素id /// </summary> /// <param name="queryGeo">查询图形</param> /// <param name="targetClass">目标要素集</param> /// <param name="resultOIds">查询结果oid列表</param> /// <returns>查询到的</returns> public IList <int> QuerySameGeoFetaures(IGeometry queryGeo, IFeatureClass targetClass, string whereclause = null) { IList <int> result = new List <int>(); if (queryGeo == null || targetClass == null) { return(result); } IPoint queryPoint = null; try { ISpatialFilter spatialFilter = new SpatialFilter() { GeometryField = targetClass.ShapeFieldName, SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects, Geometry = queryPoint ?? queryGeo }; if (!String.IsNullOrEmpty(whereclause)) { spatialFilter.WhereClause = whereclause; } if (queryGeo.GeometryType == esriGeometryType.esriGeometryPoint) { queryPoint = queryGeo as IPoint; } else if (queryGeo.GeometryType == esriGeometryType.esriGeometryPolyline) { queryPoint = (queryGeo as IPolyline).FromPoint; } else if (queryGeo.GeometryType == esriGeometryType.esriGeometryPolygon) { queryPoint = (queryGeo as IPolygon).FromPoint; } //进行空间查询 using (ComReleaser comreleaser = new ComReleaser()) { IRelationalOperator relationalOperator = queryGeo as IRelationalOperator; IFeatureCursor featureCursor = targetClass.Search(spatialFilter, true); IFeature target = featureCursor.NextFeature(); comreleaser.ManageLifetime(featureCursor); comreleaser.ManageLifetime(target); while (target != null && target.Shape != null) { if (relationalOperator != null) { //既包含又在内,说明完全相同 if (relationalOperator.Contains(target.Shape) && relationalOperator.Within(target.Shape)) { result.Add(target.OID); } } target = featureCursor.NextFeature(); } featureCursor = null; } } catch (Exception ex) { ErrorMessage = ex.ToString(); } return(result); }