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
arcpy.CheckOutExtension("Spatial")
# Set the current workspace to iterate on rasters
arcpy.env.workspace = "D:/PROJECTS/03_2014_AB_HYDRO_MODELLING/02_TRANSFORM_DATA/WPWFS/WaterSupplyVulnerability.gdb"
#Variables
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:
row.next()
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 ?
Thanks