I'm trying to create a simple map algebra calculation not managed by ModelBuilder, but writing it in Python is trickier than I thought. I have a list of raster in a GDB and I must multiply them by each value contained in a population field. I created my raster list, the a search cursor for the population value, and I use everything in 2 nested FOR loop to do my multiplication.

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *

#Check Spatial Analyst extension

# Set the current workspace to iterate on rasters
arcpy.env.workspace = "D:/PROJECTS/03_2014_AB_HYDRO_MODELLING/02_TRANSFORM_DATA/WPWFS/WaterSupplyVulnerability.gdb"

PiS = "D:/PROJECTS/03_2014_AB_HYDRO_MODELLING/02_TRANSFORM_DATA/WPWFS/SurfaceWaterUtilities.gdb/SurfaceWaterNetwork/SurfUtsAB_Proj_corr25m" # Water treatment station feature class
Pi = ["POPN_CURRE"] # Get the population value in the field "POPN_CURRE" for each water treatment station
WiS = arcpy.ListRasters("WEI*", "All") # Create a list of rasters
Qn = arcpy.Raster('WaterYield_CubicMeters_SqKm_Year') # Water yield raster

#  For each Station's population Pi in All stations PiS, multiply POPN_CURRE with rasts (119 rasters)
with arcpy.da.SearchCursor(PiS, Pi) as cursor:
    for row in cursor:
        for Wi in WiS:
            print Wi
            WiPi = Wi * row
            WiPi.save("D:/PROJECTS/03_2014_AB_HYDRO_MODELLING/02_TRANSFORM_DATA/WPWFS/WaterSupplyVulnerability.gdb/WiPi" + row)
            WiPiS = arcpy.ListRasters("Wi*", "All") # Create a list to iterate through WiPi calculation rasters
            SWiPi = CellStatistics("WiPiS", "SUM", "DATA")
            SWiPi.save("D:/PROJECTS/03_2014_AB_HYDRO_MODELLING/02_TRANSFORM_DATA/WPWFS/WaterSupplyVulnerability.gdb/SWiPi" + row)
            IMPn = Qn * SWiPi
            IMPn.save("D:/PROJECTS/03_2014_AB_HYDRO_MODELLING/02_TRANSFORM_DATA/WPWFS/WaterSupplyVulnerability.gdb/IMPn" + row)
        arcpy.Delete_management(WiPiS) #Delete intermediate raster from Times processing

It returns the following error : Runtime error

Traceback (most recent call last):
  File "<string>", line 4, in <module>
TypeError: can't multiply sequence by non-int of type 'tuple'

I understand that my population field is not a integer, and so ? When I try the 2 for loop with just a print rather than the calculation, it works. So what is the function or method to call ?



When you create a data access cursor object, the rows are returned as tuples with a length equal to the number of columns in the feature (or the fields specified when creating the cursor). In the line where you are calculating WiPi = Wi * row you need to call the desired tuple value by using row[0]. In your case there is only 1 value in the tuple, but you still need to index the row object to get that value.

You will also need to create a raster object for each raster in the list your are iterating over. You can do this simply by using the Raster() command of the arcpy.sa module:

WiPi = Raster(Wi) * row

Additionally, the row.next() command is superfluous in this context. Because the rows returned by the data access style cursor are tuple objects, the row.next() command is actually returning the next value of the row tuple. You aren't assigning the value to any variable so as-is the line does not do anything. It's not hurting anything, but just for the sake of cleaner code you can remove this line.

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.