I have been searching the samples and the API, but so far I have not been able to find any signs on how to make a "Select by Location" in C# With PRO-SDK.
Anyone out there that can point me in the right direction?
Regards
Hi Rene,
You can use spatial filter on MCT thread:
// execute the select on the MCT
QueuedTask.Run(() =>
{
foreach(FeatureLayer pItem in pointLayers)
{
// define the spatial query filter
var spatialQuery = new SpatialQueryFilter() { FilterGeometry = geometry, SpatialRelationship = SpatialRelationship.Contains };
// gather the selection
var pointSelection = pItem.Select(spatialQuery);
}
}
If you want to do selection from the tool then you need to override OnSketchCompleteAsync event
protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
}
and add code above inside.
Hello,
I am trying to do a select by location (select the state (California, Florida, etc.) from a states polygon layer that polygon project boundary intersects with, and then zoom the map to that state. Would the code for this be similar to above? I cannot seem to figure out how to do this.
Thank you
You can also use GetFeatures to return features, as a dictionary, that intersect a search geometry.
protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
return QueuedTask.Run(() =>
{
//create search polygon at 5 pixels
var searchPoly = CreateSearchPolygon(geometry as MapPoint, 5);
//find features under the search geometry
var searchFeatures = MapView.Active.GetFeatures(searchPoly);
return true;
});
Here is another example that uses the selection from one feature class to select features in another feature class.
//"SelectByLocation" - Select Counties based on selected State polygon
Layout layout = LayoutView.Active.Layout;
await QueuedTask.Run(() =>
{
MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
Map m = mf.Map;
//Select a single state polygon
FeatureLayer fl1 = m.FindLayers("State_Polygons").First() as FeatureLayer;
QueryFilter queryFilter = new QueryFilter();
string whereClause = "State_Name = 'Rhode Island'";
queryFilter.WhereClause = whereClause;
//Use a cursor to get to a feature's geometry
using (ArcGIS.Core.Data.RowCursor rowCursor = fl1.Search(queryFilter))
{
//Grab the first record (and hopefully only record)
while (rowCursor.MoveNext())
{
//Grab the features geometry
Feature feature = rowCursor.Current as Feature;
Geometry geo = feature.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("Counties").First() as FeatureLayer;
fl2.Select(spatialQuery);
}
}
});
Jeff, this worked great to select the county as in your example above! Now that I have the selected county, I just cannot figure out how to zoom the map to the selected county.
I am using this, but it zooms to the entire layer, not just the selected feature within the layer.
var lyrExtent = fl2.QueryExtent();
mf.ZoomTo(lyrExtent);
Thank you so much for your help on this!
In the above code you already have the geometry of the selection feature.
Geometry geo = feature.GetShape();
So add these two lines to the bottom of the code above.
//Reference county layer and apply spatial query
FeatureLayer fl2 = m.FindLayers("Counties").First() as FeatureLayer;
fl2.Select(spatialQuery);
Envelope env = geo.Extent;
mf.ZoomTo(env);
Hmm, that seems to zoom to the state polygon, not the selected county polygon.
Is it possible to zoom to the selected county polygon, not the state polygon?
Your help is much appreciated!
@Joshua -
Could you use either of the following?
ZoomToSelected Method
https://pro.arcgis.com/en/pro-app/sdk/api-reference/index.html#topic12028.html
ZoomToSelectedAsync Method
https://pro.arcgis.com/en/pro-app/sdk/api-reference/index.html#topic12029.html
Good Luck!
+ bill