2

I've written a script to run through a point layer, using the SearchCursor in the data access module and then read the XY values. Within the with statement, I then use GetCellValue to get the elevation values and print them.

What I'd like to do is go one step further and write the value to a field in the row. Is there a way to do this? I can use CalculateField, but that'll be slow as it'll have to use a query to limit itself to the current row. I can also possibly use a list to write the values to and then run through the list and update the fields using the UpdateCursor, but I think that'll be slow too.

Is there a way to do this inside the with statement?

import arcpy

##arcpy.AddField_management('C:\Data\Surveyed.shp', 'Z', 'DOUBLE')

with arcpy.da.SearchCursor('C:\Data\Surveyed.shp', ['SHAPE@XY']) as cursor:
    for row in cursor:
        result = arcpy.GetCellValue_management('Database Connections\DC_SDEEBG_gisp.sde\SDEEBG.GIS.SRTM_DEM_NSWVICTAS_1SEC', str((row[0])[0]) + " " + str((row[0])[1]), "1")
        CellValue = float(result.getOutput(0))
        print CellValue
1

Yes - you should use arcpy.da.UpdateCursor instead of arcpy.da.SearchCursor.

Elaboration:

This is what I would try:

import arcpy

arcpy.AddField_management('C:\Data\Surveyed.shp', 'Z', 'DOUBLE')

with arcpy.da.UpdateCursor(r'C:\Data\Surveyed.shp', ('SHAPE@XY', 'Z')) as cursor:
    for row in cursor:
        result = arcpy.GetCellValue_management('Database Connections\DC_SDEEBG_gisp.sde\SDEEBG.GIS.SRTM_DEM_NSWVICTAS_1SEC', str((row[0])[0]) + " " + str((row[0])[1]), "1")
        CellValue = float(result.getOutput(0))
        print CellValue
        #  Use CellValue to set the second value (Z)
        row[1] = CellValue
        #Update the FC row
        cursor.updateRow(row)
  • Can you elaborate on this please? I've been stuffing around with arcpy.da.UpdateCursor, but I couldn't get it going. Can you search and write rows with this? – MierMoto May 16 '13 at 9:12
  • Thanks mate, that worked a charm. Had to make a small edit, but that was minor. Much appreciated. – MierMoto May 17 '13 at 5:22

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.