This document is archived and information here might be outdated.  Recommended version.


Performing map selection (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with and configuring maps, layers, and graphics > Working with the map > Performing map selection

Performing map selection


About performing map selection

Selection is a common way to interact with subsets of features for analysis, editing, and other similar tasks. When working with a map, selection can be managed at both the map level and the layer level. At the layer level, it is common to select features based on queries. Map selection methods typically involve choosing selectable features based on an input geometry.
The following code example demonstrates how to achieve the following selection types:
  • SelectFeaturesScreenPoint - Selects features in the map based on a screen coordinate such as the coordinate obtained from a tool.
  • SelectLayersFeatures - Selects features in a layer based on a Structured Query Language (SQL) WhereClause.
  • SelectFeaturesPolygon - Performs a map selection based on a shape.
  • ClearMapSelection - Clears the map selection.
  • ClearLayerSelection - Clears a layer selection.
[C#]
static void SelectFeaturesScreenPoint(IMap pMap, int x, int y, int pixelTol)
{
    tagRECT r;
    //Construct a small rectangle out of the x,y coordinates' pixel tolerance.
    r.left=x - pixelTol; //Upper left x, top left is 0,0.  
    r.top=y - pixelTol; //Upper left y, top left is 0,0.
    r.right=x + pixelTol; //Lower right x, top left is 0,0. 
    r.bottom=y + pixelTol; //Lower right y, top left is 0,0.

    //Transform the device rectangle into a geographic rectangle via the display transformation.  
    IEnvelope pEnvelope=new EnvelopeClass();
    IActiveView pActiveView=pMap as IActiveView;
    IDisplayTransformation pDisplayTrans =
        pActiveView.ScreenDisplay.DisplayTransformation;
    pDisplayTrans.TransformRect(pEnvelope, ref r, 5); 
        //5=esriTransformPosition + esriTransformToMap.

    pEnvelope.SpatialReference=pMap.SpatialReference;

    ISelectionEnvironment pSelectionEnvironment=new SelectionEnvironmentClass();
    pSelectionEnvironment.CombinationMethod =
        esriSelectionResultEnum.esriSelectionResultNew;
    pMap.SelectByShape(pEnvelope, pSelectionEnvironment, false);
}

static void SelectLayersFeatures(IFeatureLayer pFeatureLayer, string WhereClause)
{
    IFeatureSelection pFeatureSelection=pFeatureLayer as IFeatureSelection;
    if (pFeatureSelection != null)
    {
        IQueryFilter pQueryFilter=new QueryFilterClass();
        pQueryFilter.WhereClause=WhereClause;
        pFeatureSelection.SelectFeatures(pQueryFilter,
            esriSelectionResultEnum.esriSelectionResultNew, false);
    }
}

static void SelectFeaturesPolygon(IMap pMap, IPolygon pPolygon)
{
    ISelectionEnvironment pSelectionEnvironment=new SelectionEnvironmentClass();
    pSelectionEnvironment.CombinationMethod =
        esriSelectionResultEnum.esriSelectionResultNew;
    pMap.SelectByShape(pPolygon, pSelectionEnvironment, false);
}

static void ClearMapSelection(IMap pMap)
{
    pMap.ClearSelection();
}

static void ClearLayerSelection(IFeatureLayer pFeatureLayer)
{
    IFeatureSelection pFeatureSelection=pFeatureLayer as IFeatureSelection;
    if (pFeatureSelection != null)
    {
        pFeatureSelection.Clear();
    }
}
[VB.NET]
Shared Sub SelectFeaturesScreenPoint(ByVal pMap As IMap, ByVal x As Integer, ByVal y As Integer, ByVal pixelTol As Integer)
Dim r As tagRECT
'Construct a small rectangle out of the x,y coordinates' pixel tolerance.
r.Left=x - pixelTol 'Upper left x, top left is 0,0.
r.top=y - pixelTol 'Upper left y, top left is 0,0.
r.Right=x + pixelTol 'Lower right x, top left is 0,0.
r.bottom=y + pixelTol 'Lower right y, top left is 0,0.

'Transform the device rectangle into a geographic rectangle via the display transformation.
Dim pEnvelope As IEnvelope=New EnvelopeClass()
Dim pActiveView As IActiveView=pMap
Dim pDisplayTrans As IDisplayTransformation=pActiveView.ScreenDisplay.DisplayTransformation
pDisplayTrans.TransformRect(pEnvelope, r, 5) '5=esriTransformPosition + esriTransformToMap.

pEnvelope.SpatialReference=pMap.SpatialReference

Dim pSelectionEnvironment As ISelectionEnvironment=New SelectionEnvironmentClass()
pSelectionEnvironment.CombinationMethod=esriSelectionResultEnum.esriSelectionResultNew
pMap.SelectByShape(pEnvelope, pSelectionEnvironment, False)
End Sub

Shared Sub SelectLayersFeatures(ByVal pFeatureLayer As IFeatureLayer, ByVal WhereClause As String)
Dim pFeatureSelection As IFeatureSelection=pFeatureLayer
If Not pFeatureSelection Is Nothing Then
    Dim pQueryFilter As IQueryFilter=New QueryFilterClass()
    pQueryFilter.WhereClause=WhereClause
    pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)
End If
End Sub

Shared Sub SelectFeaturesPolygon(ByVal pMap As IMap, ByVal pPolygon As IPolygon)
Dim pSelectionEnvironment As ISelectionEnvironment=New SelectionEnvironmentClass()
pSelectionEnvironment.CombinationMethod=esriSelectionResultEnum.esriSelectionResultNew
pMap.SelectByShape(pPolygon, pSelectionEnvironment, False)
End Sub

Shared Sub ClearMapSelection(ByVal pMap As IMap)
pMap.ClearSelection()
End Sub

Shared Sub ClearLayerSelection(ByVal pFeatureLayer As IFeatureLayer)
Dim pFeatureSelection As IFeatureSelection=pFeatureLayer
If Not pFeatureSelection Is Nothing Then
    pFeatureSelection.Clear()
End If
End Sub






Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine