How to determine  which polygon contains the point x,y

1755
4
Jump to solution
06-13-2012 11:59 PM
AnatoliiTerentiev
Occasional Contributor III
Dear Gurus!
I get point coordinates x,y by clicking on the axMapControl. How to determine  which polygon of the feature layer contains the point ?
Thanks in advance!
(arcengine 2010 c# vs 2010)
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
Do a spatial query using ISpatialFilter and IFeatureLayer.Search.

View solution in original post

0 Kudos
4 Replies
NeilClemmons
Regular Contributor III
Do a spatial query using ISpatialFilter and IFeatureLayer.Search.
0 Kudos
AnatoliiTerentiev
Occasional Contributor III
Thank you very much!
I tried the following code:
       

 public IFeature getFeature(Point point, IGeoFeatureLayer geoFeatureLayer, IActiveView activeView, System.Double searchTolerance)
        {
            if (searchTolerance < 0 || point == null || geoFeatureLayer == null || activeView == null){return null;}
            IMap map = activeView.FocusMap;

            // Expand the points envelope to give better search results    
            ESRI.ArcGIS.Geometry.IEnvelope envelope = point.Envelope;
            envelope.Expand(searchTolerance, searchTolerance, false);

            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = geoFeatureLayer.FeatureClass;
            System.String shapeFieldName = featureClass.ShapeFieldName;

            // Create a new spatial filter and use the new envelope as the geometry    
            ESRI.ArcGIS.Geodatabase.ISpatialFilter spatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
            spatialFilter.Geometry = envelope;
            spatialFilter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
            spatialFilter.set_OutputSpatialReference(shapeFieldName, map.SpatialReference);
            spatialFilter.GeometryField = shapeFieldName;

            // Do the search
            ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = geoFeatureLayer.Search(spatialFilter, false);

            feature = featureCursor.NextFeature();

            return feature;
        }

But sometimes I getting the wrong result. In particular, when the point does not belong to any polygon - I get a certain polygon.
I need to get feature (region of republic - polygon) which contains the point.
0 Kudos
JoeHershman
MVP Regular Contributor
You may want to try esriSpatialRelEnum.esriSpatialRelWithin instead of intersects

Good Luck
Thanks,
-Joe
0 Kudos
AnatoliiTerentiev
Occasional Contributor III
Thanks!
This has helped!
0 Kudos