Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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)
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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