In my add-in project I have many layers (points, polylines, polygons). I want to get the coordinates of a feature selected from any feature layer in the list through a map tool and show into the text boxes of a winform. Until now I could only find a sample to return the coordinates of a point clicked on the map.
Hi Jaewon,
This is code combined from few ArcGIS Pro snippets:
var firstLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
var selectionfromMap = firstLayer.GetSelection();
ArcGIS.Core.Data.QueryFilter filter = new ArcGIS.Core.Data.QueryFilter
{
ObjectIDs= selectionfromMap.GetObjectIDs();
};
// get the row
using (ArcGIS.Core.Data.RowCursor rowCursor = featureClass.Search(filter, false))
{
while (rowCursor.MoveNext())
{
long oid = rowCursor.Current.GetObjectID();
// get the shape from the row
ArcGIS.Core.Data.Feature feature = rowCursor.Current as ArcGIS.Core.Data.Feature;
Polygon polygon = feature.GetShape() as Polygon;
// get the attribute from the row (assume it's a double field)
double value = (double)rowCursor.Current.GetOriginalValue(fldIndex);
// do something here
}
}
Thanks Gintautas Kmieliauskas,
Can you please explain it a little. what is "featureClass" and "fidIndex"?
and coordinates data is not stored in the attribute table? how can we retrieve coordinates from attribute table then? the double value looks like the object id?
Hi,
Sorry. I have combined code in notepad.
Instead of featureClass you need to use firstLayer. I send you update peace of code:
var firstLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
var selectionfromMap = firstLayer.GetSelection();
ArcGIS.Core.Data.QueryFilter filter = new ArcGIS.Core.Data.QueryFilter
{
ObjectIDs = selectionfromMap.GetObjectIDs()
};
// get the row
using (ArcGIS.Core.Data.RowCursor rowCursor = firstLayer.Search(filter))
{
while (rowCursor.MoveNext())
{
long oid = rowCursor.Current.GetObjectID();
// get the shape from the row
ArcGIS.Core.Data.Feature feature = rowCursor.Current as ArcGIS.Core.Data.Feature;
Polygon polygon = feature.GetShape() as Polygon;
// do something here
}
Thanks,
I will try your code and let you know if I need further assistance. Thanks again.