How to get the coordinates of a selected feature?

3185
4
05-29-2019 07:00 PM
JaewonYang
New Contributor III

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.

0 Kudos
4 Replies
GKmieliauskas
Esri Regular Contributor

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
          }
        }

JaewonYang
New Contributor III

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?

0 Kudos
GKmieliauskas
Esri Regular Contributor

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

}

JaewonYang
New Contributor III

Thanks,

I will try your code and let you know if I need further assistance. Thanks again.

0 Kudos