I have IFeatureSelection. Then select from it items by QueryFilter. How can i get coordinates of selected item? (If there are more then 1 object, then select first)?

Selected item is Point in Attribute Table. I am using C# to write code.

Using samples and Links from @artwork21 and @GISKid i think that i can get coordinates of selected item using this code:

var fClass = featureLayer.FeatureClass;
var fCursor = fClass.Update(null, false);
var aFeature = fCursor.NextFeature();
var tPoint = aFeature.Shape as IPoint;
// then i get Coordinates from tPoint.

But i get new problem - method Update isn't implemented. Have SomeBody any advices? Thanks.

  • Are you trying to get the selected feature coordinate from the map, or from the feature table/xy field? – artwork21 Jun 4 '14 at 16:08
  • @artwork21 i try to Identify it, i attribute table coordinates is absent. – user2167382 Jun 4 '14 at 16:14
  • Rather than add an additional question ("method Update isn't implemented") into this Question I think you should Accept an Answer to your original Question and then research/ask the new one separately. – PolyGeo Jun 4 '14 at 21:36
  • Do you really need to Update? fClass.Search(null,true) would do the job unless you are changing and committing the feature. Even with the Search cursor the aFeature.Store() option should be available, it is slower, but if you only need to change a few (or possibly none) it may serve. BTW, a recycling cursor works faster provided you never need to go back to the previous feature. – Michael Stimson Jun 4 '14 at 21:50

If you're using FeatureSelection, either from the map or from the feature layer it impements IEnumFeature... no need to search/update at all.

// IMap.FeatureSelection implements IEnumFeature
IEnumFeature pFsel = (IEnumFeature) map.FeatureSelection;
pFsel.Reset(); // make sure it starts from the first feature
IFeature pFeat = pFsel.Next();
    ESRI.ArcGIS.Geometry.IGeometry pGeom = pFeat.ShapeCopy;
    if (pGeom.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
        ESRI.ArcGIS.Geometry.IPoint pPnt = (ESRI.ArcGIS.Geometry.IPoint)pGeom;
        double x, y;
        pPnt.QueryCoords(out x,out y); // use the coordinates from here
    pFeat = pFsel.Next();
} while (pFeat != null);
| improve this answer | |
  • You're welcome. I remember you were using SDC data, like I said before it's special. SDC feature classes don't support the update method - they are read only, which is why you were getting the error... the search method is implemented on SDC data (see previous comment) if you want to iterate through each feature or supply a queryfilter and step through the results. – Michael Stimson Jun 5 '14 at 22:03

Here is one way to get feature x and y coordinates (in VB though, just some minor syntax changes needed):

Private Sub AddXY_Points()

        Dim fCursor As IFeatureCursor
        Dim aFeature As IFeature
        Dim thePoint As IPoint

        fCursor = fClass.Update(Nothing, False)
        aFeature = fCursor.NextFeature

        Do Until aFeature Is Nothing
            thePoint = aFeature.Shape
            aFeature.Value(X_Pos) = thePoint.X
            aFeature.Value(Y_Pos) = thePoint.Y
            aFeature = fCursor.NextFeature

        MsgBox("Finished", vbInformation, "Add Coordinates")

    End Sub
| improve this answer | |
  • It was a module scope variable (IFeatureClass). – artwork21 Jun 4 '14 at 13:56
  • Hey, @artwork21, why the update cursor, and why the update? A search would do the job without putting write locks on the data and the feature is unmodified. Is this cut down from code that modifies features? – Michael Stimson Jun 4 '14 at 21:53

The first step is to set up your data so that you can retrieve the coordinates.

You need two create two separate fields X and Y or Lat/Long whichever makes the most sense for your data and coordinate system. From there you need to right click and select "Calculate Geometry". This should calculate the X and Y coordinates for all of your points in the attribute table.

From there you can run your script to return the x and y value. Update your question if you need help with the python code :)

EDIT I am no expert in ArcObjects but I have found the following resources which may lead you in the right direction:

  1. Get Map Coordinates from screen coordinates
  2. HowTo: Use ArcObjects to get the x and y coordinates of points, polygon centroids, or line midpoints
| improve this answer | |
  • thanks for reply. Really, i don't have data, i just have Esri Map with data. From it i get IFeatureSelection. – user2167382 Jun 4 '14 at 13:42
  • updated the answer, hopefully those thinks help! – GISKid Jun 4 '14 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.