How to perform map selection


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 examples demonstrate how to achieve several of the selection types previously mentioned:
  • SelectFeaturesScreenPoint shows how to select features in the map based on a screen coordinate like the coordinate obtained from a tool.
  • SelectLayersFeatures shows how to select features in a layer based on a Structured Query Language (SQL) WhereClause.
  • SelectFeaturesPolygon shows how to perform a map selection based on a shape.
  • ClearMapSelection shows how to clear the map selection.
  • ClearLayerSelection shows how to clear a layer selection.
[Java]
static void selectFeaturesScreenPoint(IMap map, int x, int y, int pixelTol)throws
    Exception{
    tagRECT r = new 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.
    IEnvelope envelope = new Envelope();
    IActiveView activeView = (IActiveView)map;
    IDisplayTransformation displayXform = activeView.getScreenDisplay()
        .getDisplayTransformation();
    displayXform.transformRect(envelope, r, 5); 
        // 5 =  esriTransformPosition + esriTransformToMap.

    envelope.setSpatialReferenceByRef(map.getSpatialReference());

    ISelectionEnvironment selectionEnv = new SelectionEnvironment();
    selectionEnv.setCombinationMethod(esriSelectionResultEnum.esriSelectionResultNew)
        ;
    map.selectByShape(envelope, selectionEnv, false);
}



static void selectLayersFeatures(IFeatureLayer featureLayer, String whereClause)
    throws Exception{
    IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
    if (featureSelection != null){
        IQueryFilter filter = new QueryFilter();
        filter.setWhereClause(whereClause);
        featureSelection.selectFeatures(filter,
            esriSelectionResultEnum.esriSelectionResultNew, false);
    }
}


static void selectFeaturesPolygon(IMap map, IPolygon polygon)throws Exception{
    ISelectionEnvironment selectionEnv = new SelectionEnvironment();
    selectionEnv.setCombinationMethod(esriSelectionResultEnum.esriSelectionResultNew)
        ;
    map.selectByShape(polygon, selectionEnv, false);
}


static void clearMapSelection(IMap map)throws Exception{
    map.clearSelection();
}


static void clearLayerSelection(IFeatureLayer featureLayer)throws Exception{
    IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
    if (featureSelection != null)
        featureSelection.clear();
}






Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime