在压缩列表上使用循环指定输出路径
Specifying output path with loop on zipped list
我所做工作的本质是基于 GIS,我的问题是基于 python,这就是我在这里发帖的原因。
我有 4 个包含光栅文件(.tif 文件)的文件夹。我运行对它们进行操作,然后将输出保存到特定位置。这就是我的问题所在,指定我的输出路径。
我使用的代码如下:
import arcpy
from arcpy.sa import *
#set pathway to rasters
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\NDVI'
NDVIraster=arcpy.ListRasters('*tif')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\NDII'
NDIIraster=arcpy.ListRasters('*tif')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\RGR'
RGRraster=arcpy.ListRasters('*tif')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\SWIR32'
SWIR32raster=arcpy.ListRasters('*tif')
#set the output pathway
outpath='F:\Sheyenne\Normalized_Indices\Fuzzy_Membership\'
#run my operation
for ndvi, ndii, rgr, swir32, in zip(NDVIraster, NDIIraster,RGRraster, SWIR32raster):
outpath=outpath + ndvi
outraster= arcpy.gp.FuzzyOverlay_sa([ndvi, ndii, rgr, swir32], outpath, "AND")
所以我希望我的输出路径是初始 outpath
和 ndvi
中文件名的组合。当我打印 outpath
虽然它首先保存到第一个文件名,然后第二个文件保存到第一个文件名和第二个文件名。所以输出一是 file1.tif,输出二是 file1.tiffile2.tif,输出三是 file1.tiffile2.tiffile3.tif 等
如何只保存到 ndvi
中相应的文件名,而不是使用迭代来继续添加名称?
在再次添加之前简单地重置 outpath。
#run my operation
for ndvi, ndii, rgr, swir32, in zip(NDVIraster, NDIIraster,RGRraster,SWIR32raster):
outpath='F:\Sheyenne\Normalized_Indices\Fuzzy_Membership\'
outpath=outpath + ndvi
outraster= arcpy.gp.FuzzyOverlay_sa([ndvi, ndii, rgr, swir32], outpath, "AND")
您正在覆盖 outpath
变量。您需要使用 2 个变量来按照您想要的方式工作,例如使用 outpath_base
作为根。
#set the output pathway
outpath_base='F:\Sheyenne\Normalized_Indices\Fuzzy_Membership\'
#run my operation
for ndvi, ndii, rgr, swir32, in zip(NDVIraster, NDIIraster,RGRraster, SWIR32raster):
outpath=outpath_base + ndvi
outraster= arcpy.gp.FuzzyOverlay_sa([ndvi, ndii, rgr, swir32], outpath, "AND")
我所做工作的本质是基于 GIS,我的问题是基于 python,这就是我在这里发帖的原因。
我有 4 个包含光栅文件(.tif 文件)的文件夹。我运行对它们进行操作,然后将输出保存到特定位置。这就是我的问题所在,指定我的输出路径。
我使用的代码如下:
import arcpy
from arcpy.sa import *
#set pathway to rasters
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\NDVI'
NDVIraster=arcpy.ListRasters('*tif')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\NDII'
NDIIraster=arcpy.ListRasters('*tif')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\RGR'
RGRraster=arcpy.ListRasters('*tif')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\Fuzzy_Overlay\SWIR32'
SWIR32raster=arcpy.ListRasters('*tif')
#set the output pathway
outpath='F:\Sheyenne\Normalized_Indices\Fuzzy_Membership\'
#run my operation
for ndvi, ndii, rgr, swir32, in zip(NDVIraster, NDIIraster,RGRraster, SWIR32raster):
outpath=outpath + ndvi
outraster= arcpy.gp.FuzzyOverlay_sa([ndvi, ndii, rgr, swir32], outpath, "AND")
所以我希望我的输出路径是初始 outpath
和 ndvi
中文件名的组合。当我打印 outpath
虽然它首先保存到第一个文件名,然后第二个文件保存到第一个文件名和第二个文件名。所以输出一是 file1.tif,输出二是 file1.tiffile2.tif,输出三是 file1.tiffile2.tiffile3.tif 等
如何只保存到 ndvi
中相应的文件名,而不是使用迭代来继续添加名称?
在再次添加之前简单地重置 outpath。
#run my operation
for ndvi, ndii, rgr, swir32, in zip(NDVIraster, NDIIraster,RGRraster,SWIR32raster):
outpath='F:\Sheyenne\Normalized_Indices\Fuzzy_Membership\'
outpath=outpath + ndvi
outraster= arcpy.gp.FuzzyOverlay_sa([ndvi, ndii, rgr, swir32], outpath, "AND")
您正在覆盖 outpath
变量。您需要使用 2 个变量来按照您想要的方式工作,例如使用 outpath_base
作为根。
#set the output pathway
outpath_base='F:\Sheyenne\Normalized_Indices\Fuzzy_Membership\'
#run my operation
for ndvi, ndii, rgr, swir32, in zip(NDVIraster, NDIIraster,RGRraster, SWIR32raster):
outpath=outpath_base + ndvi
outraster= arcpy.gp.FuzzyOverlay_sa([ndvi, ndii, rgr, swir32], outpath, "AND")