2

I am trying to multiply a list of GRID raster datasets using a different value for each raster. The value is in the raster name. Some examples are [wse_1979_5,wse_1980_0,wse_1980_5], where 1979_5, 1980_0, and 1980_5 will become 1979.5, 1980.0, and 1980.5. Every raster currently is classified where VALUE = 1 for all cells, and multiplying by the above values from the names should make the raster datasets equal to those values. So far I have:

arcpy.env.workspace = *path*
from arcpy.sa import *
rasters = arcpy.ListRasters()
for raster in rasters:
    multValue = float(raster[4:-2]+"."+raster[-1])
    outRaster = raster * multValue
    outRaster.save(raster+"_r")
TypeError: can't multiply sequence by non-int of type 'float'

I am getting "TypeError: can't multiply sequence by non-int of type 'float'". I believe I need to somehow iterate the raster since it is apparently a string(?), but I'm not positive and can't quite figure this one out. It is simple to do in raster calculator, but I have a lot of datasets to multiply.

Thanks!

4

arcpy.ListRasters() is returning a list of raster filenames not Raster objects. In your loop you're trying to multiply a string (the raster filename) by the float value which causes the TypeError exception. You need to multiply a Raster object by the float value. Convert your file name to a Raster object using arcpy.sa.Raster

from arcpy.sa import *

rasters = arcpy.ListRasters()
for raster in rasters:
    multValue = float(raster[4:-2]+"."+raster[-1])
    outRaster = Raster(raster) * multValue  # < ---------- Raster object * float value
    outRaster.save(raster+"_r")

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.