Solved! Go to Solution.
/// <summary> /// search the eid nearest from point /// </summary> /// <param name="searchTolerance">tolerance for search</param> /// <param name="point">point input</param> /// <param name="elementType">type of element found</param> /// <returns>return eid</returns> private int GetEIDFromPoint(double searchTolerance, IPoint point, out esriElementType elementType) { if (searchTolerance < 0 || point == null) { throw new GeometricNetworkException("Tolerance or source not valid!"); } IFeatureClassContainer featureClassContainer = this.geometricNetwork as IFeatureClassContainer; double distance = double.PositiveInfinity; int featureClassID = -1; IGeometry featureGeometry = null; for (int i = 0; i < featureClassContainer.ClassCount; i++) { IFeatureClass featureClass = featureClassContainer.get_Class(i); string shapeFieldName = featureClass.ShapeFieldName; ITopologicalOperator topologicalOperator = point as ITopologicalOperator; ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = topologicalOperator.Buffer(searchTolerance); spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; spatialFilter.GeometryField = shapeFieldName; using (ComReleaser comReleaser = new ComReleaser()) { IFeatureCursor featureCursor = featureClass.Search(spatialFilter, true); comReleaser.ManageLifetime(featureCursor); IFeature feature = featureCursor.NextFeature(); while (feature != null) { IProximityOperator proximityOperator = feature.ShapeCopy as IProximityOperator; double distanceCurrent = proximityOperator.ReturnDistance(point); if (distance > distanceCurrent) { distance = distanceCurrent; featureClassID = featureClass.FeatureClassID; featureGeometry = feature.ShapeCopy; } feature = featureCursor.NextFeature(); } } } if (featureClassID == -1) { throw new GeometricNetworkException("Element in network not found!"); } IProximityOperator proximityPoint = featureGeometry as IProximityOperator; IPoint p = proximityPoint.ReturnNearestPoint(point, esriSegmentExtension.esriNoExtension); int eid = this.geometricNetwork.get_EdgeElement(p); elementType = esriElementType.esriETEdge; if (eid < 1) { eid = this.geometricNetwork.get_JunctionElement(p); elementType = esriElementType.esriETJunction; } if (eid < 1) { throw new GeometricNetworkException("Element in network not found!"); } return eid; }
esriElementType elementType; int eid = this.GetEIDFromPoint(tolerance, point, out elementType); INetElements netElements = this.geometricNetwork.Network as INetElements; int featureClassID, featureID, subID; netElements.QueryIDs(eid, elementType, out featureClassID, out featureID, out subID);