ListRasters,类型错误:'NoneType'对象不可迭代
你好,我对Python的了解非常有限,不太明白为什么会出现这种类型错误。我正在尝试将栅格数据转换为多边形,但这些栅格数据来自一个与我最初设置的工作空间不同的地方。这可能吗?而且,为什么在使用raster2 Listasters()时会出现无数据错误呢?
重新分类的命令运行得很好,并且在我定义的文件夹中创建了输出,但栅格转多边形的工具却出现了错误。
谢谢你的帮助,我需要尽快完成这个工作。
以下是错误信息:
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
这是我的代码:
# 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..."`
提前感谢任何能提供指导或建议的人!
1 个回答
arcpy.ListRasters()
这个函数不需要任何必须的参数,具体可以查看帮助页面。你确定在 Reclass 文件夹里有栅格数据吗?这些数据是通过 Reclassify_3d
成功创建的吗?我猜 outraster
的路径可能写得不太对,因为你在路径中混用了单斜杠和双斜杠。你可以这样写:outraster = os.path.join(folder, raster)
,并且在脚本开头加上 import os
。
另外,脚本在创建多边形时可能会遇到问题,因为 raster2
可能是像 raster.tiff 或 raster.jpg 这样的文件。你用这个名字来命名输出的 shapefile。如果你的栅格数据有扩展名,你应该把它去掉,比如用 arcpy.Describe(raster).baseName
。无论如何,记得在保存输出时加上 .shp
。
编辑:第二个工作空间有个拼写错误,你忘了在 r"C:Users\mkelly\Documents\Namibia\Raster_Water\1993\Reclass"
中的 C 后面加上 \
。工作空间不正确,所以你的栅格列表是空的。
你的栅格数据是什么格式的?扩展名(例如 '.tiff'
)会被用在输出的 shapefile 名称中,所以你需要把它去掉。这就是我说的“修剪”。同时,你应该加上 '.shp'
。