gpt4 book ai didi

python - 使用 gdalwarp 重新采样导致 IndentationError : unexpected indent

转载 作者:太空宇宙 更新时间:2023-11-04 04:13:54 25 4
gpt4 key购买 nike

我使用 Sentinel2 图像并尝试对它们重新采样。

我尝试了以下代码:

import os, fnmatch

INPUT_FOLDER = "/d/afavro/Bureau/test_resampling/original"
OUTPUT_FOLDER = "/d/afavro/Bureau/test_resampling/resampling_10m"

def findRasters (path, filter):
for root, dirs, files in os.walk(path):
for file in fnmatch.filter(files, filter):
yield file

for raster in findRasters(INPUT_FOLDER,'*.tif'):
print(raster)
inRaster = INPUT_FOLDER + '/' + raster
print(inRaster)
outRaster = OUTPUT_FOLDER + '/resample' + raster
print (outRaster)
cmd = "gdalwarp -tr 10 10 -r cubic " % (inRaster,outRaster)
os.system(cmd)

但我仍然收到相同的错误消息:

def findRasters (path, filter): ^
IndentationError: unexpected indent

我已经尝试过使用相同类型的代码来制作一个子集并且它有效。我不明白我的错误从何而来。

最佳答案

错误类型IndentationError 应该按字面理解:你的缩进似乎是错误的。你的线路

def findRasters (path, filter):

缩进太多,但需要与上一行保持相同的缩进级别

OUTPUT_FOLDER = "/d/afavro/Bureau/test_resampling/resampling_10m"

您提供的完整代码示例应如下所示:

import os, fnmatch

INPUT_FOLDER = "/d/afavro/Bureau/test_resampling/original"
OUTPUT_FOLDER = "/d/afavro/Bureau/test_resampling/resampling_10m"

def findRasters (path, filter):
for root, dirs, files in os.walk(path):
for file in fnmatch.filter(files, filter):
yield file

for raster in findRasters(INPUT_FOLDER,'*.tif'):
print(raster)
inRaster = INPUT_FOLDER + '/' + raster
print(inRaster)
outRaster = OUTPUT_FOLDER + '/resample' + raster
print (outRaster)
cmd = "gdalwarp -tr 10 10 -r cubic " % (inRaster,outRaster)
os.system(cmd)

此外,正如您在附加评论中所写,您的台词

cmd = "gdalwarp -tr 10 10 -r cubic " % (inRaster,outRaster)

似乎是错误的,因为 inRasteroutRaster 不会在字符串中使用。使用 String formatting相反:

cmd = 'gdalwarp -tr 10 10 -r cubic "{}" "{}"'.format(inRaster, outRaster)

关于python - 使用 gdalwarp 重新采样导致 IndentationError : unexpected indent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55829171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com