Python 使用gdalwarp重新采样会导致缩进错误:意外缩进

Python 使用gdalwarp重新采样会导致缩进错误:意外缩进,python,bash,resampling,sentinel2,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

我处理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)
似乎是错误的,因为字符串中不会使用
inRaster
outRaster
。改用:


是的,我才意识到,非常感谢!现在我有一条消息说:File“”,第18行,在cmd=“gdalwarp-tr 10-r cubic”%(InMaster,outRaster)类型错误:不是所有的参数在字符串格式化过程中都被转换,我想我的订单括号中缺少一个参数。。。我对bash代码没有很好的掌握
cmd = 'gdalwarp -tr 10 10 -r cubic "{}" "{}"'.format(inRaster, outRaster)