Sunday, December 21, 2008

How to spatially select a feature in Map – ESRI?

When we load map control with map (.Mxd) file, there could be many layers available.
So to select a specific feature from a layer, we need to know the feature class to which this feature belongs.

To get the feature class of a feature, we can say [feature object].Featureclass.
Ex: - argFeature.Featureclass.

Now we need to iterate through each layer available on the loaded map file, then we need to find the layer that loads features from the above feature class.

I would like to update such function which expects map and feature as input parameters to spatially select a feature in the given map.


public static void setSelected(IFeature pFeature,IMap pMap)
{
try
{
IFeatureLayer pFeaturelayer;
ICompositeLayer pCompositeLayer;
IFeatureClass pFeatureClass;
int layerCount;
int compositeLayerCount;

//Loop to iterate through each layer in the given map
for (layerCount = 0; layerCount <= pMap.LayerCount - 1; layerCount++)
{
//If retrieved layer is a group layer (layer having a group of layers in it)
if (pMap.get_Layer(layerCount) is IGroupLayer)
{
pCompositeLayer = (ICompositeLayer)pMap.get_Layer(layerCount);
//Looping through each layer in the group layer
for (compositeLayerCount = 0; compositeLayerCount <= pCompositeLayer.Count - 1; compositeLayerCount++)
{
//Checking if the layer is feature layer (a map layer that consists of features
if (pCompositeLayer.get_Layer(compositeLayerCount) is IFeatureLayer)
{
pFeaturelayer = (IFeatureLayer)pCompositeLayer.get_Layer(compositeLayerCount);
pFeatureClass = pFeaturelayer.FeatureClass;
//Checking with the feature class name asscoiated with the layer is same as
//given feature's feature class
if (pFeatureClass != null && pFeature != null)
{
if (((IDataset)pFeatureClass).Name == ((IDataset)(IFeatureClass)(pFeature.Class)).Name)
{
//If feature class name's are equal
pMap.SelectFeature((ILayer)pFeaturelayer, pFeature);
}

}
}
}
}
//Checking if the layer is feature layer (a map layer that consists of features
else if (pMap.get_Layer(layerCount) is IFeatureLayer)
{
pFeaturelayer = (IFeatureLayer)pMap.get_Layer(layerCount);
pFeatureClass = pFeaturelayer.FeatureClass;
//Checking with the feature class name asscoiated with the layer is same as
//given feature's feature class
if (pFeatureClass != null)
{
if (((IDataset)pFeatureClass).Name == ((IDataset)(IFeatureClass)(pFeature.Class)).Name)
{
//If feature class name's are equal
pMap.SelectFeature((ILayer)pFeaturelayer, pFeature);
}
}
}

}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message +" setSelected",Constants.DIALOG_TITLE,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
Please click in below image links to look at comments








No comments: