| Development licensing | Deployment licensing |
|---|---|
| ArcView | ArcView |
| ArcEditor | ArcEditor |
| ArcInfo | ArcInfo |
| Engine Developer Kit | Engine Runtime |
// Get the feature and its geometry given an Object ID.
IFeature stateFeature = stateFeatureClass.GetFeature(14);
IGeometry queryGeometry = stateFeature.Shape;
// Create the spatial filter. "highwayFeatureClass" is the feature class containing
// the highway data. Set the SubFields property to "FULL_NAME", as only that field is
// going to be displayed.
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = queryGeometry;
spatialFilter.GeometryField = highwayFeatureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
spatialFilter.SubFields = "FULL_NAME";
// Find the position of the "FULL_NAME" field in the highway feature class.
int nameFieldPosition = highwayFeatureClass.FindField("FULL_NAME");
// Execute the query and iterate through the cursor's results.
IFeatureCursor highwayCursor = highwayFeatureClass.Search(spatialFilter, false);
IFeature highwayFeature = null;
while ((highwayFeature = highwayCursor.NextFeature()) != null)
{
String name = Convert.ToString(highwayFeature.get_Value(nameFieldPosition));
Console.WriteLine("Highway found: {0}", name);
}
// The cursors is no longer needed, so dispose of it.
Marshal.ReleaseComObject(highwayCursor);
' Get the feature and its geometry given an Object ID.
Dim stateFeature As IFeature = stateFeatureClass.GetFeature(14)
Dim queryGeometry As IGeometry = stateFeature.Shape
' Create the spatial filter. "highwayFeatureClass" is the feature class containing
' the highway data. Set the SubFields property to "FULL_NAME", as only that field is
' going to be displayed.
Dim spatialFilter As ISpatialFilter = New SpatialFilterClass()
spatialFilter.Geometry = queryGeometry
spatialFilter.GeometryField = highwayFeatureClass.ShapeFieldName
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
spatialFilter.SubFields = "FULL_NAME"
' Find the position of the "FULL_NAME" field in the highway feature class.
Dim nameFieldPosition As Integer = highwayFeatureClass.FindField("FULL_NAME")
' Execute the query and iterate through the cursor's results.
Dim highwayCursor As IFeatureCursor = highwayFeatureClass.Search(spatialFilter, False)
Dim highwayFeature As IFeature = highwayCursor.NextFeature()
While Not highwayFeature Is Nothing
Dim Name As String = Convert.ToString(highwayFeature.Value(nameFieldPosition))
Console.WriteLine("Highway found: {0}", Name)
highwayFeature = highwayCursor.NextFeature()
End While
' The cursors is no longer needed, so dispose of it.
Marshal.ReleaseComObject(highwayCursor)
// Create a new geometry bag and give it the same spatial reference as the
// blocks feature class.
IGeometryBag geometryBag = new GeometryBagClass();
IGeometryCollection geometryCollection = (IGeometryCollection)geometryBag;
IGeoDataset geoDataset = (IGeoDataset)blocksFeatureClass;
ISpatialReference spatialReference = geoDataset.SpatialReference;
geometryBag.SpatialReference = spatialReference;
// Get a feature cursor for the three blocks and put their geometries into the geometry bag.
// Note that a non-recycling cursor is used, as the features' geometries are being stored
// for later use.
int[] blockObjectIDs =
{
11043, 11049, 11057
};
IFeatureCursor blocksCursor = blocksFeatureClass.GetFeatures(blockObjectIDs,
false);
IFeature blockFeature = null;
object missingType = Type.Missing;
while ((blockFeature = blocksCursor.NextFeature()) != null)
{
geometryCollection.AddGeometry(blockFeature.Shape, ref missingType, ref
missingType);
}
// The cursor is no longer needed, so dispose of it.
Marshal.ReleaseComObject(blocksCursor);
' Create a new geometry bag and give it the same spatial reference as the
' blocks feature class.
Dim geometryBag As IGeometryBag = New GeometryBagClass()
Dim geometryCollection As IGeometryCollection = CType(geometryBag, IGeometryCollection)
Dim geoDataset As IGeoDataset = CType(blocksFeatureClass, IGeoDataset)
Dim spatialReference As ISpatialReference = geoDataset.SpatialReference
geometryBag.SpatialReference = spatialReference
' Get a feature cursor for the three blocks and put their geometries into the geometry bag.
' Note that a non-recycling cursor is used, as the features' geometries are being stored
' for later use.
Dim blockObjectIDs As Integer() = {11043, 11049, 11057}
Dim blocksCursor As IFeatureCursor = blocksFeatureClass.GetFeatures(blockObjectIDs, False)
Dim blockFeature As IFeature = blocksCursor.NextFeature()
Dim missingType As Object = Type.Missing
While Not blockFeature Is Nothing
geometryCollection.AddGeometry(blockFeature.Shape, missingType, missingType)
blockFeature = blocksCursor.NextFeature()
End While
' The cursor is no longer needed, so dispose of it.
Marshal.ReleaseComObject(blocksCursor)
// Cast the geometry bag to the ISpatialIndex interface and call the Invalidate method
// to generate a new spatial index.
ISpatialIndex spatialIndex = (ISpatialIndex)geometryBag;
spatialIndex.AllowIndexing = true;
spatialIndex.Invalidate();
' Cast the geometry bag to the ISpatialIndex interface and call the Invalidate method
' to generate a new spatial index.
Dim spatialIndex As ISpatialIndex = CType(geometryBag, ISpatialIndex)
spatialIndex.AllowIndexing = True
spatialIndex.Invalidate()
// Create the spatial filter. Note that the SubFields property specifies that only
// the Shape field is retrieved, since the features' attributes aren't being inspected.
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = geometryBag;
spatialFilter.GeometryField = parcelsFeatureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
spatialFilter.SubFields = "Shape";
// Use IFeatureClass.FeatureCount to get a parcel count.
int parcelCount = parcelsFeatureClass.FeatureCount(spatialFilter);
Console.WriteLine("Parcels in the three blocks: {0}", parcelCount);
' Create the spatial filter. Note that the SubFields property specifies that only
' the Shape field is retrieved, since the features' attributes aren't being inspected.
Dim spatialFilter As ISpatialFilter = New SpatialFilterClass()
spatialFilter.Geometry = geometryBag
spatialFilter.GeometryField = parcelsFeatureClass.ShapeFieldName
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains
spatialFilter.SubFields = "Shape"
' Use IFeatureClass.FeatureCount to get a parcel count.
Dim parcelCount As Integer = parcelsFeatureClass.FeatureCount(spatialFilter)
Console.WriteLine("Parcels in the three blocks: {0}", parcelCount)
// Find the feature for Osaka and get its geometry.
IFeature osakaFeature = citiesFeatureClass.GetFeature(2263);
IGeometry osakaGeometry = osakaFeature.Shape;
// Use the ITopologicalOperator interface to create a buffer.
ITopologicalOperator topoOperator = (ITopologicalOperator)osakaGeometry;
IGeometry buffer = topoOperator.Buffer(500000);
' Find the feature for Osaka and get its geometry.
Dim osakaFeature As IFeature = citiesFeatureClass.GetFeature(2263)
Dim osakaGeometry As IGeometry = osakaFeature.Shape
' Use the ITopologicalOperator interface to create a buffer.
Dim topoOperator As ITopologicalOperator = CType(osakaGeometry, ITopologicalOperator)
Dim buffer As IGeometry = topoOperator.Buffer(500000)
// Create the spatial filter.
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = buffer;
spatialFilter.GeometryField = citiesFeatureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
spatialFilter.SubFields = "CITY_NAME, POP_CLASS";
spatialFilter.WhereClause = "POP_RANK < 4";
// Find the position of the CITY_NAME and POP_CLASS fields in the feature class.
int cityNamePosition = citiesFeatureClass.FindField("CITY_NAME");
int popClassPosition = citiesFeatureClass.FindField("POP_CLASS");
// Execute the query.
IFeatureCursor featureCursor = citiesFeatureClass.Search(spatialFilter, true);
IFeature feature = null;
while ((feature = featureCursor.NextFeature()) != null)
{
String cityName = Convert.ToString(feature.get_Value(cityNamePosition));
String popClass = Convert.ToString(feature.get_Value(popClassPosition));
Console.WriteLine("{0} --- Population: {1}", cityName, popClass);
}
' Create the spatial filter.
Dim spatialFilter As ISpatialFilter = New SpatialFilterClass()
spatialFilter.Geometry = buffer
spatialFilter.GeometryField = citiesFeatureClass.ShapeFieldName
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains
spatialFilter.SubFields = "CITY_NAME, POP_CLASS"
spatialFilter.WhereClause = "POP_RANK < 4"
' Find the position of the CITY_NAME and POP_CLASS fields in the feature class.
Dim cityNamePosition As Integer = citiesFeatureClass.FindField("CITY_NAME")
Dim popClassPosition As Integer = citiesFeatureClass.FindField("POP_CLASS")
' Execute the query.
Dim featureCursor As IFeatureCursor = citiesFeatureClass.Search(spatialFilter, True)
Dim feature As IFeature = featureCursor.NextFeature()
While Not feature Is Nothing
Dim cityName As String = Convert.ToString(feature.Value(cityNamePosition))
Dim popClass As String = Convert.ToString(feature.Value(popClassPosition))
Console.WriteLine("{0} --- Population: {1}", cityName, popClass)
End While
|
|
Requested Geometry
|
|||
|
Interior
|
Boundary
|
Exterior
|
||
|
Query Geometry
|
Interior
|
1
|
2
|
3
|
|
Boundary
|
4
|
5
|
6
|
|
|
Exterior
|
9
|
8
|
7
|
|
|
Illustration
|
String
|
Description
|
|
T********
|
The interiors of the geometries must intersect.
|
|
T***T****
|
The boundaries of the geometries must intersect and their interiors must intersect.
|
|
F***T****
|
The boundaries of the geometries must intersect and their interiors must not intersect.
|
|
FF*FF****
|
The geometries must be completely disjoint.
|
// Get the feature with the known Object ID (California).
IFeature caFeature = featureClass.GetFeature(4);
IGeometry caGeometry = caFeature.Shape;
// Create a spatial filter with a SCL spatial relationship string.
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = caGeometry;
spatialFilter.GeometryField = featureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelRelation;
spatialFilter.SpatialRelDescription = "TFFFTFFF*";
spatialFilter.SubFields = "Start_Date, Population";
// Find the positions of the Start_Date and Population fields in the class.
int startDatePosition = featureClass.FindField("Start_Date");
int populationPosition = featureClass.FindField("Population");
// Execute the query.
ISelectionSet selectionSet = featureClass.Select(spatialFilter,
esriSelectionType.esriSelectionTypeSnapshot,
esriSelectionOption.esriSelectionOptionNormal, null);
' Get the feature with the known Object ID (California).
Dim caFeature As IFeature = featureClass.GetFeature(4)
Dim caGeometry As IGeometry = caFeature.Shape
' Create a spatial filter with a 9IM spatial relationship.
Dim spatialFilter As ISpatialFilter = New SpatialFilterClass()
spatialFilter.Geometry = caGeometry
spatialFilter.GeometryField = featureClass.ShapeFieldName
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelRelation
spatialFilter.SpatialRelDescription = "TFFFTFFF*"
spatialFilter.SubFields = "Start_Date, Population"
' Find the positions of the Start_Date and Population fields in the class.
Dim startDatePosition As Integer = featureClass.FindField("Start_Date")
Dim populationPosition As Integer = featureClass.FindField("Population")
' Execute the query.
Dim selectionSet As ISelectionSet = featureClass.Select(spatialFilter, esriSelectionType.esriSelectionTypeSnapshot, _
esriSelectionOption.esriSelectionOptionNormal, Nothing)
// Get the marsh and highway features from their respective classes.
IFeature marshFeature = vegFeatureClass.GetFeature(518);
IFeature highwayFeature = roadsFeatureClass.GetFeature(39);
// Get the geometries of the two features.
IGeometry marshGeometry = marshFeature.Shape;
IGeometry highwayGeometry = highwayFeature.Shape;
// Cast the highway's geometry to IRelationalOperator and determine whether or
// not it crosses the marsh's geometry.
IRelationalOperator relationalOperator = (IRelationalOperator)highwayGeometry;
Boolean crosses = relationalOperator.Crosses(marshGeometry);
Console.WriteLine("Highway crosses marsh: {0}", crosses);
' Get the marsh and highway features from their respective classes.
Dim marshFeature As IFeature = vegFeatureClass.GetFeature(518)
Dim highwayFeature As IFeature = roadsFeatureClass.GetFeature(39)
' Get the geometries of the two features.
Dim marshGeometry As IGeometry = marshFeature.Shape
Dim highwayGeometry As IGeometry = highwayFeature.Shape
' Cast the highway's geometry to IRelationalOperator and determine whether or
' not it crosses the marsh's geometry.
Dim relationalOperator As IRelationalOperator = CType(highwayGeometry, IRelationalOperator)
Dim crosses As Boolean = relationalOperator.Crosses(marshGeometry)
Console.WriteLine("Highway crosses marsh: {0}", crosses)
// Open the feature classes used by the queries.
IFeatureClass blocksFeatureClass = featureWorkspace.OpenFeatureClass("Blocks");
IFeatureClass parcelsFeatureClass = featureWorkspace.OpenFeatureClass("Parcels")
;
// Fill the spatial cache.
ISpatialCacheManager spatialCacheManager = (ISpatialCacheManager)
featureWorkspace;
// Check if the cache has been filled.
if (!spatialCacheManager.CacheIsFull)
{
// If not full, fill the cache.
spatialCacheManager.FillCache(cacheExtent);
}
// Execute spatial queries.
// Empty the cache.
spatialCacheManager.EmptyCache();
' Open the feature classes used by the queries.
Dim blocksFeatureClass As IFeatureClass = featureWorkspace.OpenFeatureClass("Blocks")
Dim parcelsFeatureClass As IFeatureClass = featureWorkspace.OpenFeatureClass("Parcels")
' Fill the spatial cache.
Dim spatialCacheManager As ISpatialCacheManager = CType(featureWorkspace, ISpatialCacheManager)
' Check if the cache has been filled.
If spatialCacheManager.CacheIsFull Then
' If not full, fill the cache.
spatialCacheManager.FillCache(cacheExtent)
End If
' Execute spatial queries.
' Empty the cache.
spatialCacheManager.EmptyCache()