2016-03-01 63 views
0

我正在做的事情的本质是基于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的相应文件名,而不是使用迭代不断添加名称?

+0

这是因为outpath不会重置为每个循环的基路径。它会保留它的价值。您可以处理这两种方式,在添加初始化值之前将其移动到循环中。或者,我会推荐的是将'outpath'的名称更改为'outroot'并将添加更改为'outpath = outroot + ndvi' – TehCorwiz

回答

1

只需在重新添加内容之前重置超出路径即可。

#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") 
0

您正在覆盖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")