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:
[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 licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine