Tuesday, December 30, 2008

How to get features with in an Envelope – ESRI

When we load .mxd or map file onto map control, we can find features from different feature layers been spatially distributed.
So to find a set of features which belong to a specific feature class and enclosed with in an envelope, we can write a generic function that accepts the envelope and feature layer to find features in specific feature layer.

Step 1:-
Write a function Considering IEnvelope , IFeatureLayer, MapControl as Input Parameters.
Step 2:-
Run a spatial query using ISpatailFilter interface for the feature class of the given Input feature layer with geometry as given envelope.
Note:-For SpatialRelUse (Spatial relation)
Assign enumeration values from esriSpatialRelEnum
Ex:- esriSpatialRelEnum. esriSpatialRelIntersects for considering all featuers in the feature layer which intersect the given input envelope.
Step 3:-
Query feature class with the above Spatail filter object and retrieve feature cursor.

We can retrieve feature by feature from the returned feature cursor and perform specific required operations.

Function :-
private IFeatureCursor GetFeatures(IFeatureLayer argFeatureLayer, IEnvelope argEnvelope, IMap argMap)
{
if (argEnvelope != null && argFeatureLayer != null && argFeatureLayer.FeatureClass != null && argMap != null)
{
ISpatialFilter objSpatialFilter = new SpatialFilterClass();
objSpatialFilter = new SpatialFilter();
objSpatialFilter.Geometry = (IGeometry)argEnvelope;
objSpatialFilter.GeometryField = "Shape";
objSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
return argFeatureLayer.FeatureClass.Search(objSpatialFilter, false);
}
return null;
}

Example :-

To select a set features spatially intersecting the given envelope.

private void SelectFeatures(IFeatureLayer argFeatureLayer, IFeatureCursor argFeatCursor)
{
if (argFeatCursor != null)
{

IFeature pFeature = null;
while ((pFeature = argFeatCursor.NextFeature()) != null)
{
Map.SelectFeature(argFeatureLayer, pFeature);
}
}
}

O/p





No comments: