0

第一次发帖和已经用尽所有其他选项的 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')
4

1 回答 1

0

在我的脚本上从另一个问答论坛获得帮助后,我现在正在提供我自己的 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()

My results after running the script:
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

谢谢!

于 2012-05-07T17:04:37.113 回答