How to perform a spatial query


This sample will demonstrate how to formulate and execute a spatial query. The function requires a feature class to search, a geometry to define the spatial area, and a spatial relationship which determines the way features are selected.

How to use

  1. Paste the code into your VB6 or VBA Application.
  2. Call the function from within your application.
[VBA]
Public Function SpatialQuery(pFeatureClassIN As esriGeoDatabase.IFeatureClass, _
                             searchGeometry As esriGeometry.IGeometry, _
                             spatialRelation As esriGeoDatabase.esriSpatialRelEnum, _
                             Optional whereClause As String = "" _
                             ) As esriGeoDatabase.IFeatureCursor
    
    Dim pSpatialFilter As esriGeoDatabase.ISpatialFilter
    Dim strShpFld As String
    Dim pFeatCursor As esriGeoDatabase.IFeatureCursor
    Dim pQueryFilter As esriGeoDatabase.IQueryFilter
    
    ' create a spatial query filter
    Set pSpatialFilter = New esriGeoDatabase.SpatialFilter
    
    ' specify the geometry to query with
    Set pSpatialFilter.Geometry = searchGeometry
    
    ' specify what the geometry file is called on the Feature Class that we will be querying against
    strShpFld = pFeatureClassIN.ShapeFieldName
    pSpatialFilter.GeometryField = strShpFld
    
    ' specify the type of spatial operation to use
    pSpatialFilter.SpatialRel = spatialRelation
    
    ' create the where statement
    pSpatialFilter.whereClause = whereClause
    
    ' perform the query and use a cursor to hold the results
    Set pQueryFilter = pSpatialFilter
    Set pFeatCursor = pFeatureClassIN.Search(pQueryFilter, False)
    
    Set SpatialQuery = pFeatCursor
    
End Function