Select to view content in your preferred language

How to run geoprocessing on row selected by cursor?

1034
2
Jump to solution
02-04-2020 09:34 AM
RyanHowell1
Occasional Contributor

I have a point feature class that has a unique ID field and a julian date field.

I also have a pile of rasters in a geodatabase that have daily values that I want to extract to the corresponding date in the point file. I have already written a script that goes through and renamed them so that the julian date is tagged on to the end of the name of the raster.

To do this, I have the following code:

input = point feature class


#Iterate through to extract values to points based on date
arcpy.env.workspace = #geodatabase with the rasters
fl = arcpy.ListRasters()
dict = {}
for feature in fl:
    j = feature[-5:-1] + feature[-1]#returns the julian date
    
    with arcpy.da.SearchCursor(input, ["julian"]) as cursor:
        for row in cursor:
            if row[0] == int(j):
                temp = #temporary output string
                table = arcpy.sa.ExtractValuesToPoints(input, r'string for geodatabase' + feature, r"ouput geodatabase" + feature + "_testtable")
              
                with arcpy.da.SearchCursor(table, ['ID', 'RASTERVALU']) as cursor2:
                    for row2 in cursor2:
                        ID = row2[0]
                        value = row2[1]
                        dict.update({ID : value})
                arcpy.Delete_management(table)
                
            else:
                print('not equal' + str(row[0]))

with arcpy.da.UpdateCursor(input, ['ID', "Value column"]) as cursor:
    for row in cursor:
        row[1] = dict[row[0]]
        cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I think the problem is line 15; the input for the Extract Values to Points tool is being overwritten by the last raster that it iterates over. So, the values for November 6th are writing over all of the previous dates in the dictionary and subsequently the point feature class. 

How do I code this so that it only runs the Extract Values to Points tool on the selected row in the search cursor?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor
input = point feature class
input_fl = arcpy.MakeFeatureLayer_management(input, "input_fl")


#Iterate through to extract values to points based on date
arcpy.env.workspace = #geodatabase with the rasters
fl = arcpy.ListRasters()
dict = {}
for feature in fl:
    
    j = feature[-5:-1] + feature[-1]#returns the julian date
    
    with arcpy.da.SearchCursor(input, ["julian", "OBJECTID"]) as cursor:
        for row in cursor:
            counter = 1
            if row[0] == int(j):
                query =  "OBJECTID = " + str(row[1])
                selection = arcpy.SelectLayerByAttribute_management("input_fl", "NEW_SELECTION", query)
                
                temp = #temporary output string
                table = arcpy.sa.ExtractValuesToPoints(selection, r'string for geodatabase' + feature, r"ouput geodatabase" + feature + "_testtable"+str(counter))
                counter += 1
              
                with arcpy.da.SearchCursor(table, ['ID', 'RASTERVALU']) as cursor2:
                    for row2 in cursor2:
                        ID = row2[0]
                        value = row2[1]
                        dict.update({ID : value})
                arcpy.Delete_management(table)
                
            else:
                print('not equal' + str(row[0]))

with arcpy.da.UpdateCursor(input, ['ID', "Value column"]) as cursor:
    for row in cursor:
        row[1] = dict[row[0]]
        cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hi,

Something along these lines by using a selection on each?

View solution in original post

0 Kudos
2 Replies
DavidPike
MVP Frequent Contributor
input = point feature class
input_fl = arcpy.MakeFeatureLayer_management(input, "input_fl")


#Iterate through to extract values to points based on date
arcpy.env.workspace = #geodatabase with the rasters
fl = arcpy.ListRasters()
dict = {}
for feature in fl:
    
    j = feature[-5:-1] + feature[-1]#returns the julian date
    
    with arcpy.da.SearchCursor(input, ["julian", "OBJECTID"]) as cursor:
        for row in cursor:
            counter = 1
            if row[0] == int(j):
                query =  "OBJECTID = " + str(row[1])
                selection = arcpy.SelectLayerByAttribute_management("input_fl", "NEW_SELECTION", query)
                
                temp = #temporary output string
                table = arcpy.sa.ExtractValuesToPoints(selection, r'string for geodatabase' + feature, r"ouput geodatabase" + feature + "_testtable"+str(counter))
                counter += 1
              
                with arcpy.da.SearchCursor(table, ['ID', 'RASTERVALU']) as cursor2:
                    for row2 in cursor2:
                        ID = row2[0]
                        value = row2[1]
                        dict.update({ID : value})
                arcpy.Delete_management(table)
                
            else:
                print('not equal' + str(row[0]))

with arcpy.da.UpdateCursor(input, ['ID', "Value column"]) as cursor:
    for row in cursor:
        row[1] = dict[row[0]]
        cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hi,

Something along these lines by using a selection on each?

0 Kudos
RyanHowell1
Occasional Contributor

That appears to have done it. Thanks David!

0 Kudos