使用gdalwarp重新采样会导致IndentationError:意外缩进

时间:2019-04-24 11:36:18

标签: python bash resampling sentinel2

我使用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

我已经尝试使用相同类型的代码来制作一个子集,并且它起作用了。我不知道我的错误来自哪里。

1 个答案:

答案 0 :(得分:1)

错误类型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)