第一次发布帖子和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')
答案 0 :(得分:0)
在我的脚本上收到另一个Q& A论坛的帮助之后,我现在提供我自己的GIS相关问题的答案来关闭这个帖子(并转到gis.stackexchange :) - 感谢L.Yip的评论) 。这是最终更正的脚本,它输出我的两个栅格属性(X方向的像素大小,Y方向的像素大小),将栅格列表输出到.txt文件中:
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":
resultX = arcpy.GetRasterProperties_management(grid,'CELLSIZEX')
CellSizeX = resultX.getOutput(0)
resultY = arcpy.GetRasterProperties_management(grid,'CELLSIZEY')
CellSizeY = resultY.getOutput(0)
OutputFile.write(grid + ',' + str(CellSizeX) + ',' + str(CellSizeY) + '\n')
OutputFile.close()
运行脚本后的结果:
S01Clip_30m,88.937158083333,88.9371580833333
S02Clip_30m,88.937158083333,88.937158083333
S03Clip_30m,88.9371580833371,88.9371580833333
S04Clip_30m,88.9371580833308,88.937158083333
S05Clip_30m,88.9371580833349,88.937158083333
谢谢!