ProGuide Feature Selection - kataya/arcgis-pro-sdk GitHub Wiki
Language: C#
Subject: Map Exploration
Contributor: ArcGIS Pro SDK Team <[email protected]>
Organization: Esri, http://www.esri.com
Date: 11/24/2020
ArcGIS Pro: 2.7
Visual Studio: 2017, 2019
This guide demonstrates how to create a simple map tool for feature selection. See ProConcepts Map Exploration, MapTool for more information on the MapTool pattern.
Prerequisites
- Download and install the sample data required for this guide as instructed in Arcgis Pro SDK Community Samples Releases.
- Create a new ArcGIS Pro Module Add-in, and name the project MapToolSelect. If you are not familiar with the ArcGIS Pro SDK, you can follow the steps in the ProGuide Build your first add-in to get started.
- Add a new ArcGIS Pro Add-ins | ArcGIS Pro Map Tool item to the add-in project, and name the item MapToolSelect.
Step 1
Modify the Config.daml file tool item as follows:
- Change the caption to "Select Features".
- Change the tool heading to "Select Features" and the ToolTip text to "Select features on the current map using a rectangular sketch."
...
<tool id="MapToolSelect_MapToolSelect" caption="Select Features" className="MapToolSelect"
loadOnClick="true" smallImage="Images\GenericButtonRed16.png"
largeImage="Images\GenericButtonRed32.png" condition="esri_mapping_mapPane">
<tooltip heading="Select Features">
Select features on the current map using a rectangular sketch.<disabledText /></tooltip>
</tool>
...
Build the sample and validate the UI on the ArcGIS Pro ribbon.
Step 2
The required feature selection functions must be implemented in the MapToolSelect class.
First, SketchOutputMode in the constructor is changed to SketchOutputMode.Screen. MapView.SelectFeatures and MapView.GetFeatures throw a System.ArgumentException if a 3D query is attempted in SketchOutputMode.Map, so you need to change the sketch projection to screen coordinates.
public MapToolSelect()
{
IsSketchTool = true;
SketchType = SketchGeometryType.Rectangle;
SketchOutputMode = SketchOutputMode.Screen;
}
Note: If IsSketchTool is not set to true, the map view will not switch to sketching mode when the tool is activated, and a sketch will not be produced.
The OnSketchCompleteAsync method can be overwritten to implement the required selection functionality. The MapTool base class provides an ActiveMapView property that provides a link to the current active map view. The MapView, in turn, provides a method that can be used to select features that intersect a given geometry.
protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// Using the active map view to select
// the features that intersect the sketch geometry
ActiveMapView.SelectFeatures(geometry);
return true;
});
}
Note: ActiveMapView is a property provided by the MapTool base class; however, the same reference can also be obtained from the MapView class as shown below.
// Get the active map view
var mapView = MapView.Active;
if (mapView != null) { // Use map view here }
Step 3
Rebuild the add-in. Fix any compilation errors.
Step 4
Debug the add-in. Run the debugger and start ArcGIS Pro. Open the C:\Data\Interacting with Maps\Interacting with Maps.aprx project that contains a 3D map with feature data. Try the selection tool on the 3D scene as shown here:
Verify the updated feature selection.