Simple Map Algebra Looped Through Rasters in a Directory

945
2
02-17-2013 04:12 AM
DerekAzar
New Contributor
I think I must be missing something pretty basic here because this isn't a complex operation. That said, I've always had difficulty with Map Algebra in scripts. I'd like to apply a constant multiplier to all of the rasters in a directory. They are in . tif format. When I run the separately in Python I get this error: 

Traceback (most recent call last):
  File "C:\Users\Derek\Desktop\multiply rasters.py", line 15, in <module>
    for raster_image in raster_list:
TypeError: 'NoneType' object is not iterable

When I run in it piecemeal in the Python window I get this error:

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

I don't think that the raster is being recognized as an input during the multiplication. I also don't know what else to try. Here is the code:

import arcpy,os,sys,string
from arcpy import env
from arcpy.sa import *


arcpy.CheckOutExtension("Spatial")


input_folder = r'C:\Users\Derek\Desktop\ToJordan\ToJordan\landsat'
arcpy.env.worspace = input_folder


#Set reflectance constant
const = 0.0001


# Get raster list, loop through and multiply be constant
raster_list = arcpy.ListRasters()
for raster_image in raster_list:
      print raster_image
      rast_mult = Raster(raster_image) * const
      raster_mult_out = input_folder + os.sep + raster_image + "_reflect.img"
      rast_mult.save(raster_mult_out)
      print raster_image + " Finished"

I hope someone can help.  Thanks!
Tags (2)
0 Kudos
2 Replies
LT
by
Occasional Contributor
Hehe sneaky little error.  you've probably caught it by now, but ...

arcpy.env.worspace = input_folder

should be

arcpy.env.workspace = input_folder

This means that when your script ran, arcpy.env.workspace was actually just the default value (empty string)

When you run ListRasters with the workspace set to empty string it returns None. 

Meaning raster_list == None

Then you tried to iterate on None.

This is a really good example of why debuggers are essential.

Cheers!
0 Kudos
DerekAzar
New Contributor
I did catch that soon after I posted, thanks though!
0 Kudos