Axelle
Axelle

Reputation: 123

Resampling with gdalwarp results in IndentationError: unexpected indent

I work with Sentinel2 images and I'm trying to resample them.

I tried the following code:

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)

But I still get the same error message :

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

I already tried the same type of code to make a subset and it worked. I don't understand where my mistake came from.

Upvotes: 1

Views: 144

Answers (1)

finefoot
finefoot

Reputation: 11232

The error type IndentationError should be taken literally: Your indentation seems to be wrong. Your line

def findRasters (path, filter):

is too far indented, but needs to be at the same indentation level as the previous line

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

The full code sample you provided should look like this:

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)

Also, as you've written in the additional comment, your line

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

seems to be wrong as inRaster and outRaster won't be used in the string. Use String formatting instead:

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

Upvotes: 0

Related Questions