2

I am writing a script (to be used in ArcGIS 10.7.1) that uses a Zonal Statistics tool to create an average of a raster that is beneath a grid. I then use the Summary Statistics tool to take the summation of all the grid values and display them in a table. The resulting table has only one row with three columns, as shown below:

enter image description here

I'm trying to use a cursor to assign the value of the "SUM_Value" to a variable, because that value will be used later to compute some fertilizer amounts.

How can I do this?

The methods that I've tried aren't working out. I get this error when I do so:

Traceback (most recent call last):
 File "<pyshell#132>", line 1, in <module>
 totalNitrogen = row.getValue(field)
NameError: name 'row' is not defined

Here is what I've tried:

#Define workspace
env.workspace = "C:\\someWorkspace"
#Defining variable 'Grid'
Grid = #Previously created grid
#Zonal statistics tool
recommendationMap = ZonalStatistics(Grid, "grid_field", outputRaster, "MEAN")
#Compute raster to Int
recommendationMapInt = Int(recommendationMap)
#Save raster
recommendationMapInt.save(env.workspace + "\\nitrogenRecommendationMap")
#Generate statistics with Summary Statistics tool
nitrogenStats = arcpy.Statistics_analysis(recommendationMapInt,"outputRaster",[["Field_to_summarize", "SUM"]])
#Resulting output field defined for cursor
nitrogenField = "SUM_Value"
#Cursor defined
nitrogenCursor = arcpy.SearchCursor(nitrogenStats)
#Error occurs here
totalNitrogen = row.getValue(field)

As previously stated, I need that totalNitrogen variable to equal the "SUM_Value" field from the Statistics_analysis table.

asked Dec 5, 2019 at 3:25
1
  • 1
    Only mistake that I can see is that you are not defining row object after your cursor. Presuming you will always have one row in your Summary Statistics, if you say row = nitrogenCursor.next() after your nitrogenCursor assignment, you will be fine. Commented Dec 5, 2019 at 6:48

1 Answer 1

2

Your problem is you're not iterating your cursor so row is never set, you should be using an arcpy.da cursor like:

with arcpy.da.SearchCursor(nitrogenStats,"SUM_Value") as nitrogenCursor: 
 for row in nitrogenCursor: 
 totalNitrogen = row[0] 
 break # to ensure we only read the first value exit the loop

Or as a one-liner:

totalNitrogen = [row[0] for row in arcpy.da.SearchCursor(nitrogenStats,'SUM_Value')][0]

Which creates a list of all the values in the table but then assigns totalNitrogen as the first element.

If you insist on using the old style cursor it can be achieved like:

nitrogenCursor = arcpy.SearchCursor(nitrogenStats)
for row in nitrogenCursor:
 totalNitrogen = row.getValue(field)
 break # to ensure we only read the first value exit the loop
del nitrogenCursor # important to release locks.
answered Dec 5, 2019 at 6:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.