I've been using my own code to extract multiple rasters using a single shapefile.

I've just substituted the strings (inputs, file paths etc.) to my code:

import subprocess, os, glob, gdal
result_path = "/home/brent/Documents/Hansen_new_data/New_data/10_110/NAMRIA_based//"
mask_layer = ("/home/brent/Documents/Hansen_new_data/NAMRIA_QGIS.shp")

for fname in glob.glob("*.tif"):
    input = os.path.join("/home/brent/Documents/Hansen_new_data/New_data/10_110/", fname)
    (pathshp, shpname) = os.path.split(mask_layer)
    (newName, ext) = os.path.splitext(shpname)
    warp = 'gdalwarp -cutline \'%s\' -crop_to_cutline -co compress=LZW \'%s\' \'%s\'' % (mask_layer, input, result_path+newName+fname)

I wonder what went wrong because it's not working anymore. I'm using QGIS 2.18.

  • Maybe the creation options (-co) need to be in string format. I.e. -co "compress=LZW".
    – Joseph
    Mar 20, 2019 at 11:04
  • hard to know what is wrong without an error or description of "not working" actually means (e.g., produces no output, produces garbage output)
    – Paul H
    Mar 20, 2019 at 20:41
  • also, i would recommend the subprocess module as it'll help you pass those arguments and capture the output
    – Paul H
    Mar 20, 2019 at 20:41
  • @PaulH, it produces no ouput but it seemed to iterate through the rasters. Mar 21, 2019 at 9:30

1 Answer 1


There was an invalid geometry for the shapefile mask I used that's why gdalwarp failed. I used lwgeom plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.