5

I am trying to enumerate each row of a points shapefile and get a certain field's value (elevation) and the xy coordinates.

But I see I can only use:

  • arcpy.SearchCursor to get the field; OR
  • arcpy.da.SearchCursor(fc, "SHAPE@XY") for the xy coordinates

Can I do both at once?

5

Yes - the code below uses the SHAPE@XY token and a field called elevation to do it on a test feature class:

import arcpy

fc = r"C:\temp\test.gdb\pointFC"
fields = ['elevation', 'SHAPE@XY']

with arcpy.da.SearchCursor(fc, fields) as cursor:

   for row in cursor:
       elev = row[0]
       coordX = row[1][0]
       coordY = row[1][1]
       print "X: {}, Y: {}, Elevation: {}".format(coordX,coordY,elev)
1
  • PolyGeo's is correct. It should not be assumed the feature class (and the OP's language confirms this) has explicit x and y fields in it. In addition to PolyGeos answer, you can use fields = ['SHAPE@Y', 'SHAPE@X']
    – RyNo's
    Aug 10 '16 at 18:22
2

Yes, you just have to define additional fields to search against, see e.g.

fields = ['elevation', 'x', 'y']

with arcpy.da.SearchCursor(fc, fields) as cursor:

   for row in cursor:
      elev = row[0]
      coordX = row[1]
      coordY = row[2]
3
  • This assumes that there are fields called 'x' and 'y' that contain the XY coordinate values but the Question indicates that these are to be pulled from the SHAPE@XY token.
    – PolyGeo
    Nov 15 '13 at 3:05
  • @PolyGeo, Yes I may be assuming that x and y are existing fields, but to keep it simple I just separated those fields out in my list.
    – artwork21
    Nov 15 '13 at 3:21
  • 1
    I like PolyGeo's answer, but I like yours a bit more as it's a bit more obvious especially for beginners. Nov 18 '13 at 6:45

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.