Executing spatial queries

空间查询是基于与某几何要素的空间关系的查询。


1.Finding features within a polygon

// Get the feature and its geometry given an ObjectID.
IFeature stateFeature = stateFeatureClass.GetFeature(14);
IGeometry queryGeometry = stateFeature.ShapeCopy;

// 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 shown.
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.
using(ComReleaser comReleaser = new ComReleaser())
{
    IFeatureCursor highwayCursor = highwayFeatureClass.Search(spatialFilter, false);
    comReleaser.ManageLifetime(highwayCursor);
    IFeature highwayFeature = null;
    while ((highwayFeature = highwayCursor.NextFeature()) != null)
    {
        String name = Convert.ToString(highwayFeature.get_Value(nameFieldPosition));
        Console.WriteLine("Highway found: {0}", name);
    }
}

2.Querying for features with relation to multiple geometries

这个场景展示了如何使用geometry bag进行几何查询geometry bag是一个单一的高层次的几何形状,存储的几何形状的集合。下列代码演示了如何在一组已知的block features中找到geometry bagblock features不相邻,但其ObjectIds是已知的。
// Create a 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.
// A non-recycling cursor is used, as the features' geometries are stored
// for later use.
int[] blockObjectIDs = 
{
    11043, 11049, 11057
};
using(ComReleaser comReleaser = new ComReleaser())
{
    IFeatureCursor blocksCursor = blocksFeatureClass.GetFeatures(blockObjectIDs,
        false);
    comReleaser.ManageLifetime(blocksCursor);
    IFeature blockFeature = null;
    object missingType = Type.Missing;
    while ((blockFeature = blocksCursor.NextFeature()) != null)
    {
        geometryCollection.AddGeometry(blockFeature.Shape, ref missingType, ref
            missingType);
    }
}

When using a geometry bag as the query geometry, create a spatial index on the geometry bag to allow rapid access to the contained geometries during the spatial query. See the following code example:
// 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();

现在的 geometry bag可以用作一个新的空间滤波器的几何 查询。下面的代码示例演示如何使用“包含”(包含在块中的完全的空间关系)的空间过滤器,以找到三个区块内的包裹数量:
// Create the spatial filter. 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);

3.Buffering and querying

// Find the feature for Osaka and get its geometry.
IFeature osakaFeature = citiesFeatureClass.GetFeature(2263);
IGeometry osakaGeometry = osakaFeature.ShapeCopy;

// Use the ITopologicalOperator interface to create a buffer.
ITopologicalOperator topoOperator = (ITopologicalOperator)osakaGeometry;
IGeometry buffer = topoOperator.Buffer(500000);

4.Evaluating a specific spatial relationship using IRelationalOperator

// 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 if
// it crosses the marsh's geometry.
IRelationalOperator relationalOperator = (IRelationalOperator)highwayGeometry;
Boolean crosses = relationalOperator.Crosses(marshGeometry);
Console.WriteLine("Highway crosses marsh: {0}", crosses);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值