第一次发帖和已经用尽所有其他选项的 python newb。我有兴趣将选定的栅格属性(使用 arcpy.GetRasterProperties_management(input_raster, "property_type") 函数)附加到以逗号分隔的表中,但在弄清楚如何为多个结果执行此操作时遇到了麻烦。作为一个(我的实际脚本的)删节示例,我创建了两个“for”循环;一个用于我有兴趣输出的每个栅格属性(即像元大小 X、像元大小 Y)。我的栅格列表包括 S01Clip_30m 到 S05Clip_30m。我的目标是创建一个看起来像这样的 .txt 文件:
RasterName, CellSizeX, CellSizeY
S01Clip_30m, 88.9372, 88.9375
S02Clip_30m, 88.9374, 88.9371
到目前为止我的代码如下(底部有一些不确定的、拙劣的语法)。当我运行它时,我得到了这个结果:
S05Clip_30m,88.9374
(列表中的最后一个栅格,CellSizeY)
感谢您在关键的底部代码块上提供的任何帮助。
import arcpy
from arcpy import env
env.workspace = ('C:\\StudyAreas\\Aggregates.gdb')
InFolder = ('C:\\dre\\python\\tables')
OutputFile = open(InFolder + '\\' + 'RasterProps.txt', 'a')
rlist = arcpy.ListRasters('*','*')
for grid in rlist:
if grid[-8:] == "Clip_30m":
result = arcpy.GetRasterProperties_management(grid,'CELLSIZEX')
CellSizeX = result.getOutput(0)
for grid in rlist:
if grid[-8:] == "Clip_30m":
result = arcpy.GetRasterProperties_management(grid,'CELLSIZEY')
CellSizeY = result.getOutput(0)
> I know the syntax below is incorrect, but I know there are *some* elements that
> should be included based on other example scripts that I have...
> if result.getOutput(0) == CellSizeX:
> coltype = CellSizeX
> elif result.getOutput(0) == CellSizeY:
> coltype = CellSizeY
> r = ''.join(grid)
> colname = r[0:]
> OutputFile.writelines(colname+','+coltype+'\n')