Search code examples
pythonpython-2.xarcpy

ListRasters, TypeError: 'NoneType' object is not iterable


Hi I have very minimal python experience and am not sure why I am getting this type error. I am trying to perform a raster to polygon conversion with the rasters from a different workspace than the initial env.workspace. Is this possible? And how can there be a no data error in the raster2 Listasters()?

The reclassify command works fine and creates the output in the defined folder but the raster to polygon tool is what is signaling the error.

Thanks for the help I need this done for work as soon as possible.

Here is the error:

Traceback (most recent call last):
  File "C:\Users\mkelly\Documents\Namibia\Raster_Water\Script_try2.py", line 30, in <module>
    for raster2 in arcpy.ListRasters():
TypeError: 'NoneType' object is not iterable

Here is the code:

# Import arcpy module 
import arcpy 
from arcpy import env

arcpy.env.overwriteOutput = True

# Check out any necessary licenses 
arcpy.CheckOutExtension("3D")

#Set the workplace 
arcpy.env.workspace = r"C:\Users\mkelly\Documents\Namibia\Raster_Water\1993"

#for all files in 1993, reclassify to water only rasters 
for raster in arcpy.ListRasters(): 
    folder = r"C:\Users\mkelly\Documents\Namibia\Raster_Water\1993\Reclass" + "\\" 
    outraster = folder + raster 
    arcpy.Reclassify_3d(raster, "Value", "1 1", outraster, "NODATA") 

#Can I set up a new env workspace to get reclassified rasters from "Reclass" folder?
arcpy.env.workspace = r"C:Users\mkelly\Documents\Namibia\Raster_Water\1993\Reclass"

#for all files in 1993\Reclass, perform RastertoPolygon 
for raster2 in arcpy.ListRasters(): 
    folder2 = r"C:\Users\mkelly\Documents\Namibia\Raster_Water\1993\Polygons" + "\\" 
    outraster2 = folder2 + raster2 
    arcpy.RasterToPolygon_conversion(raster2, outraster2, "NO_SIMPLIFY", "VALUE") 

print "end Processing..."`

Thanks in advance to anyone that can give guidance or suggestions!


Solution

  • arcpy.ListRasters() doesn't take any mandatory argument, see the help page. Are you sure there are any rasters in the Reclass folder? Are they successfully created by Reclassify_3d? My guess is that outraster is not well understood as you combine single and double backslashes in the path. Instead, write outraster = os.path.join(folder, raster) and import os in the beginning of your script.

    Also, the script will have trouble creating the polygons, because raster2 is probably something like raster.tiff or raster.jpg. You're using this to name your output shapefile. If your rasters have an extension, you should trim it using e.g. arcpy.Describe(raster).baseName. And in any case, add .shp as you're saving the output in a folder.

    EDIT: There is a typo in the second workspace, you've forgotten the \ after the C in r"C:Users\mkelly\Documents\Namibia\Raster_Water\1993\Reclass". The workspace is wrong so your list of rasters is empty.

    What format are your rasters? The extension (e.g. '.tiff') will be used in the name of the output shapefile so you have to remove it. That's what I mean by 'trim'. And you should add '.shp'.