This document is archived and information here might be outdated.  Recommended version.


Get All Features from Point Search in GeoFeatureLayer Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Get All Features from Point Search in GeoFeatureLayer Snippet

Finds all the features in a GeoFeature layer by supplying a point. The point could come from a mouse click on the map.

[C#]
///<summary>Finds all the features in a GeoFeature layer by supplying a point. The point could come from a mouse click on 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 IFeatureCursor interface is returned containing all the selected features found in the GeoFeatureLayer.</returns>
///   
///<remarks></remarks>
public ESRI.ArcGIS.Geodatabase.IFeatureCursor GetAllFeaturesFromPointSearchInGeoFeatureLayer(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);

  return featureCursor;
}
[Visual Basic .NET]
'''<summary>Finds all the features in a GeoFeature layer by supplying a point. The point could come from a mouse click on 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 IFeatureCursor interface is returned containing all the selected features found in the GeoFeatureLayer.</returns>
'''   
'''<remarks></remarks>
Public Function GetAllFeaturesFromPointSearchInGeoFeatureLayer(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.IFeatureCursor

  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)

  Return featureCursor

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