How to convert selected feature from feature class to geometry

2098
13
Jump to solution
11-14-2018 02:02 PM
JoshuaO_Neil
New Contributor II

Hello,

I cannot figure out how to convert selected features from a feature dataset to geometry, so that I can zoom the map to the selected features (I want to zoom the map to only the selected state from the code below).

I have two layers in my map (project area and states). I was able to select the state from the states layer that intersects my project area polygon using the code below. However, I cannot figure out how to zoom the map to the selected state (the state that intersects with my project area boundary). I thought the last 2 lines of my code would do it, but that zooms the map to the entire states layer (not just the selected state). Does anyone know what I am doing wrong?

 

{
var layout = Project.Current.GetItems<LayoutProjectItem>()?.First().GetLayout();
if (layout == null) return;
MapFrame mf = layout.FindElement("Main Map") as MapFrame;
Map m = mf.Map;


FeatureLayer fl1 = m.FindLayers("ProjectArea1").First() as FeatureLayer;
QueryFilter queryFilter = new QueryFilter();


using (ArcGIS.Core.Data.RowCursor rowCursor = fl1.Search(queryFilter = null))
{
//Grab the first record (and hopefully only record)
while (rowCursor.MoveNext())
{
//Grab the features geometry
Feature feature1 = rowCursor.Current as Feature;
Geometry geo = feature1.GetShape();

//Set up a spatial query
var spatialQuery = new SpatialQueryFilter()
{ FilterGeometry = geo, SpatialRelationship = SpatialRelationship.Intersects };

//Reference county layer and apply spatial query
FeatureLayer fl2 = m.FindLayers("States").First() as FeatureLayer;
fl2.Select(spatialQuery);

Geometry geo1 = fl2.QueryExtent();

Envelope env = geo1.Extent;
mf.ZoomTo(env);
}
}

0 Kudos
13 Replies
JoshuaO_Neil
New Contributor II

Is it just a fact of changing the method name to get the code to add a new map frame to work? For instance, when I had that same code snippet (starting at "//Build Envelope Geometry for Main Map Frame") without the code starting at "protected override async void OnClick()", it worked. When I added the "//Build Envelope Geometry for Main Map Frame" code above it now it doesn't work.

One difference is that it was under the method "protected override async void OnClick()", but now since I added the "private Task<Envelope> SelectStateAsync(Map m)" method, I had to create a new method to place it within, below the "private Task<Envelope> SelectStateAsync(Map m)" method. Thats where I am having a problem. How do I add code after that method?

0 Kudos
GKmieliauskas
Esri Regular Contributor

Override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. ArcGIS Pro knows when you press Add-in button it needs to call OnClick method which you override for each Add-in button. You need to insert call of your BuildEnvelopeGeometry method for example at the end of OnClick method, like BuildEnvelopeGeometry();

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi Joshua,

I mean after grabbing feature geometry you could use code from sample:

//select features that intersect the sketch geometry
var selection = MapView.Active.SelectFeatures(geo, SelectionCombinationMethod.New);
IEnumerable<KeyValuePair<BasicFeatureLayer, List<long>>> result = from r in selection where string.Compare(r.Key.Name, "States", true) == 0 select r;
//zoom to selection
MapView.Active.ZoomToAsync(result.Select(kvp => kvp.Key), true, TimeSpan.FromSeconds(1.5), true);

0 Kudos
JoshuaO_Neil
New Contributor II

Thank you! I will try this as well.

0 Kudos