This document is archived and information here might be outdated. Recommended version. |
ArcObjects Help for .NET developers > ESRI.ArcGIS.Snippets > Snippets > Perform Spatial Query Snippet (ArcObjects .NET 10.4 SDK) |
Creates a spatial query which performs a spatial search for features in the supplied feature class and has the option to also apply an attribute query via a where clause.
///<summary>Creates a spatial query which performs a spatial search for features in the supplied feature class and has the option to also apply an attribute query via a where clause.</summary> /// ///<param name="featureClass">An ESRI.ArcGIS.Geodatabase.IFeatureClass</param> ///<param name="searchGeometry">An ESRI.ArcGIS.Geometry.IGeometry (Only high-level geometries can be used)</param> ///<param name="spatialRelation">An ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum (e.g., esriSpatialRelIntersects)</param> ///<param name="whereClause">A System.String, (e.g., "city_name='Redlands'").</param> /// ///<returns>An IFeatureCursor holding the results of the query will be returned.</returns> /// ///<remarks>Call the SpatialQuery method by passing in a reference to the Feature Class, a Geometry used for the search and the spatial operation to be preformed. An exmaple of a spatial opertaion would be intersects (e.g., esriSpatialRelEnum.esriSpatialRelContains). If you would like to return everything found by the spatial operation use "" for the where clause. Optionally a whereclause (e.g. "income > 1000") maybe applied if desired. The SQL syntax used to specify the where clause is the same as that of the underlying database holding the data.</remarks> public ESRI.ArcGIS.Geodatabase.IFeatureCursor PerformSpatialQuery(ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass, ESRI.ArcGIS.Geometry.IGeometry searchGeometry, ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum spatialRelation, System.String whereClause) { // create a spatial query filter ESRI.ArcGIS.Geodatabase.ISpatialFilter spatialFilter=new ESRI.ArcGIS.Geodatabase.SpatialFilterClass(); // specify the geometry to query with spatialFilter.Geometry=searchGeometry; // specify what the geometry field is called on the Feature Class that we will be querying against System.String nameOfShapeField=featureClass.ShapeFieldName; spatialFilter.GeometryField=nameOfShapeField; // specify the type of spatial operation to use spatialFilter.SpatialRel=spatialRelation; // create the where statement spatialFilter.WhereClause=whereClause; // perform the query and use a cursor to hold the results ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter=new ESRI.ArcGIS.Geodatabase.QueryFilterClass(); queryFilter=(ESRI.ArcGIS.Geodatabase.IQueryFilter)spatialFilter; ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor=featureClass.Search(queryFilter, false); return featureCursor; }
'''<summary>Creates a spatial query which performs a spatial search for features in the supplied feature class and has the option to also apply an attribute query via a where clause.</summary> ''' '''<param name="featureClass">An ESRI.ArcGIS.Geodatabase.IFeatureClass</param> '''<param name="searchGeometry">An ESRI.ArcGIS.Geometry.IGeometry (Only high-level geometries can be used)</param> '''<param name="spatialRelation">An ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum (e.g., esriSpatialRelIntersects)</param> '''<param name="whereClause">A System.String, (e.g., "city_name='Redlands'").</param> ''' '''<returns>An IFeatureCursor holding the results of the query will be returned.</returns> ''' '''<remarks>Call the SpatialQuery method by passing in a reference to the Feature Class, a Geometry used for the search and the spatial operation to be preformed. An exmaple of a spatial opertaion would be intersects (e.g., esriSpatialRelEnum.esriSpatialRelContains). If you would like to return everything found by the spatial operation use "" for the where clause. Optionally a whereclause (e.g. "income > 1000") maybe applied if desired. The SQL syntax used to specify the where clause is the same as that of the underlying database holding the data.</remarks> Public Function PerformSpatialQuery(ByVal featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal searchGeometry As ESRI.ArcGIS.Geometry.IGeometry, ByVal spatialRelation As ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum, ByVal whereClause As System.String) As ESRI.ArcGIS.Geodatabase.IFeatureCursor ' create a spatial query filter Dim spatialFilter As ESRI.ArcGIS.Geodatabase.ISpatialFilter=New ESRI.ArcGIS.Geodatabase.SpatialFilterClass() ' specify the geometry to query with spatialFilter.Geometry=searchGeometry ' specify what the geometry field is called on the Feature Class that we will be querying against Dim nameOfShapeField As System.String=featureClass.ShapeFieldName spatialFilter.GeometryField=nameOfShapeField ' specify the type of spatial operation to use spatialFilter.SpatialRel=spatialRelation ' create the where statement spatialFilter.WhereClause=whereClause ' perform the query and use a cursor to hold the results Dim queryFilter As ESRI.ArcGIS.Geodatabase.IQueryFilter=New ESRI.ArcGIS.Geodatabase.QueryFilterClass() queryFilter=CType(spatialFilter, ESRI.ArcGIS.Geodatabase.IQueryFilter) Dim featureCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor=featureClass.Search(queryFilter, False) Return featureCursor End Function