5

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

I see I can use:

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

but can I do both at once?

2 Answers 2

6

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)
0
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]
0

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