ProGuide Identify - Esri/arcgis-pro-sdk GitHub Wiki
Language: C#
Subject: Map Exploration
Contributor: ArcGIS Pro SDK Team <[email protected]>
Organization: Esri, http://www.esri.com
Date: 10/06/2024
ArcGIS Pro: 3.4
Visual Studio: 2022
This guide demonstrates how to create a map tool for feature identify. The sample tool allows you to view the total number of features by feature layer on a map by drawing a circular selection area over a map. 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 MapToolIdentify. 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 MapToolIdentify.
Step 1
Modify the Config.daml file tool item as follows:
- Change the caption to "Identify Features."
- Change the tool heading to "Identify Features" and the ToolTip text to "Identify features on the current map using a circular sketch."
...
<tool id="MapToolIdentify_MapToolIdentify" caption="Identify Features"
className="MapToolIdentify" loadOnClick="true" keytip="TI"
smallImage="GenericButtonRed16" largeImage="GenericButtonRed32"
condition="esri_mapping_mapPane">
<tooltip heading="Identify Features">
Identify features on the current map using a circular sketch.
<disabledText />
</tooltip>
</tool>
...
Build the sample and validate the UI on the ArcGIS Pro ribbon:
Step 2
The required feature identify functions must be implemented in the MapToolIdentify class.
First, the sketch type set in the constructor is changed to SketchGeometryType.Circle.
Next, 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 MapToolIdentify()
{
IsSketchTool = true;
SketchType = SketchGeometryType.Circle;
SketchOutputMode = SketchOutputMode.Screen;
}
Note: If IsSketchTool is not set to true, a sketch is not produced.
The OnSketchCompleteAsync method can be overwritten to implement the required identify functionality. Since you "await" functions within your identify implementation, you have to add the "async" keyword to the OnSketchCompleteAsync method declaration. The MapTool base class provides an ActiveMapView property that provides the link to the current active map view. The MapView, in turn, provides a method that can be used to get all features that intersect a given geometry. After the awaited Run method completes, the resulting identify string is displayed using a simple MessageBox.
protected override async Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
var mv = ActiveMapView;
if (mv == null)
return false;
// Get all layer definitions.
var lyrs = mv.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
var identifyResult = await QueuedTask.Run(() =>
{
var sb = new StringBuilder();
// Get the features that intersect the sketch geometry.
var features = mv.GetFeatures(geometry);
var dictFeatures = features.ToDictionary();
foreach (var lyr in lyrs)
{
var fCnt = dictFeatures.ContainsKey(lyr) ? features[lyr].Count : 0;
sb.AppendLine($@"{fCnt} {(fCnt == 1 ? "record" : "records")} for {lyr.Name}");
}
return sb.ToString();
});
MessageBox.Show(identifyResult);
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 2D and 3D maps with feature data. Try the Identify tool on the 2D map as shown here:
Verify the Identify result.
Try the Identify tool on the 3D scene as shown here: