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
Based on spatial query selecting multiple features and going through selected features
QueuedTask.Run(() => { using (Geodatabase DMPGeodatabase = new Geodatabase(DMPReadConn)) { FeatureClass featureClassToBeSelected = DMPGeodatabase.OpenDataset<FeatureClass>("featureClassToBeSelected"); FeatureClass featureClassSelectionCriteria = DMPGeodatabase.OpenDataset<FeatureClass>("featureClassSelectionCriteria"); using (RowCursor rowCursor = featureClassSelectionCriteria.Search()) { while (rowCursor.MoveNext()) { using (ArcGIS.Core.Data.Row row = rowCursor.Current) { if (row["Field"].ToString() == "someAtrribute") //e.g., row[city] = "Chicago" { Feature feature = rowCursor.Current as Feature; Geometry geometry = feature.GetShape(); //Get Chicago's shape point, polygon or line var spatialQuery = new SpatialQueryFilter() { FilterGeometry = geometry, SpatialRelationship = SpatialRelationship.Intersects }; var selectedFeatures = featureClassToBeSelected.Select(spatialQuery, SelectionType.ObjectID, SelectionOption.Normal); //selected features inside Chicago using (RowCursor rowCursorPB = selectedFeatures.Search()) { while (rowCursorPB.MoveNext()) { using (ArcGIS.Core.Data.Row rowPropBoundsPB = rowCursorPB.Current) { //Go through all selected features based on spatial query //Do something with selected features } } } } } } } } });
Thanks for the code, it would make more sense if formatted properly.
Is it possible to zoom to the selected county polygon, not the state polygon?
Your help is much appreciated!
Hmm, that seems to zoom to the state polygon, not the selected county polygon.
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);
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!
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); } } });
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.
<SPAN class="keyword token">protected</SPAN> <SPAN class="keyword token">override</SPAN> Task<SPAN class="operator token"><</SPAN><SPAN class="keyword token">bool</SPAN><SPAN class="operator token">></SPAN> <SPAN class="token function">OnSketchCompleteAsync</SPAN><SPAN class="punctuation token">(</SPAN>Geometry geometry<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">return</SPAN> QueuedTask<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Run</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">//create search polygon at 5 pixels</SPAN> <SPAN class="keyword token">var</SPAN> searchPoly <SPAN class="operator token">=</SPAN> <SPAN class="token function">CreateSearchPolygon</SPAN><SPAN class="punctuation token">(</SPAN>geometry <SPAN class="keyword token">as</SPAN> MapPoint<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//find features under the search geometry</SPAN> <SPAN class="keyword token">var</SPAN> searchFeatures <SPAN class="operator token">=</SPAN> MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetFeatures</SPAN><SPAN class="punctuation token">(</SPAN>searchPoly<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.