[GIS] Getting XY coordinates and field values for each row in ArcPy SearchCursor

arcgis-desktoparcpycoordinatescursorxy

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?

Best Answer

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)