Get First Feature from Point Search in GeoFeatureLayer Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Get First Feature from Point Search in GeoFeatureLayer Snippet

Finds the first feature in a GeoFeature layer by supplying an point. The point could come from a mouse click in the map.

[C#]
///<summary>Finds the first feature in a GeoFeature layer by supplying an point.  The point could come from a mouse click in the map.</summary>
///
///<param name="searchTolerance">A System.Double that is the number of map units to search. Example: 25</param>
///<param name="point">An IPoint interface in map units where the user clicked on the map</param>
///<param name="geoFeatureLayer">An ILayer interface to search upon</param>
///<param name="activeView">An IActiveView interface</param>
/// 
///<returns>An IFeature interface that is the first feature found in the GeoFeatureLayer.</returns>
/// 
///<remarks></remarks>
public ESRI.ArcGIS.Geodatabase.IFeature GetFirstFeatureFromPointSearchInGeoFeatureLayer(System.Double searchTolerance, ESRI.ArcGIS.Geometry.IPoint point, ESRI.ArcGIS.Carto.IGeoFeatureLayer geoFeatureLayer, ESRI.ArcGIS.Carto.IActiveView activeView)
{
  if (searchTolerance < 0 || point == null || geoFeatureLayer == null || activeView == null)
  {
    return null;
  }

  ESRI.ArcGIS.Carto.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=featureClass.Search(spatialFilter, false);

  // Get the first feature
  ESRI.ArcGIS.Geodatabase.IFeature feature=featureCursor.NextFeature();
  if (!(feature == null))
  {
    return feature;
  }
  else
  {
      return null;
  }
}
[Visual Basic .NET]
'''<summary>Finds the first feature in a GeoFeature layer by supplying an point.  The point could come from a mouse click in the map.</summary>
'''
'''<param name="searchTolerance">A System.Double that is the number of map units to search. Example: 25</param>
'''<param name="point">An IPoint interface in map units where the user clicked on the map</param>
'''<param name="geoFeatureLayer">An ILayer interface to search upon</param>
'''<param name="activeView">An IActiveView interface</param>
''' 
'''<returns>An IFeature interface that is the first feature found in the GeoFeatureLayer.</returns>
''' 
'''<remarks></remarks>
Public Function GetFirstFeatureFromPointSearchInGeoFeatureLayer(ByVal searchTolerance As System.Double, ByVal point As ESRI.ArcGIS.Geometry.IPoint, ByVal geoFeatureLayer As ESRI.ArcGIS.Carto.IGeoFeatureLayer, ByVal activeView As ESRI.ArcGIS.Carto.IActiveView) As ESRI.ArcGIS.Geodatabase.IFeature

  If searchTolerance < 0 OrElse point Is Nothing OrElse geoFeatureLayer Is Nothing OrElse activeView Is Nothing Then

    Return Nothing

  End If

  Dim map As ESRI.ArcGIS.Carto.IMap=activeView.FocusMap

  ' Expand the points envelope to give better search results    
  Dim envelope As ESRI.ArcGIS.Geometry.IEnvelope=point.Envelope
  envelope.Expand(searchTolerance, searchTolerance, False)

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

  ' Create a new spatial filter and use the new envelope as the geometry    
  Dim spatialFilter As ESRI.ArcGIS.Geodatabase.ISpatialFilter=New ESRI.ArcGIS.Geodatabase.SpatialFilterClass
  spatialFilter.Geometry=envelope
  spatialFilter.SpatialRel=ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects
  spatialFilter.OutputSpatialReference(shapeFieldName)=map.SpatialReference
  spatialFilter.GeometryField=shapeFieldName

  ' Do the search
  Dim featureCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor=featureClass.Search(spatialFilter, False)

  ' Get the first feature
  Dim feature As ESRI.ArcGIS.Geodatabase.IFeature=featureCursor.NextFeature

  If Not (feature Is Nothing) Then

    Return feature

  Else

    Return Nothing

  End If

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System