This is related to this previous post.

As was trying to implement the said answer and inserting it to my code, but the code doesn't seem to work.

#importing all the necessary modules
import os, glob, gdal
from subprocess import call
#call(["ls", "-l"]) #calling the "ls -l" command in linux 

shp = "/home/brent/Documents/Hansen_new_data/Palawan_only.shp"

extent = ("116.928337 7.506939 120.000087 10.000189")
for rname in glob.glob("*AMRIA_gain_10N_110E.tif"): #iterate through the files using glob.glob
    raster = os.path.join("/home/brent/Documents/Hansen_new_data/New_data/10_110/NAMRIA_based", rname) #join the shp file name to a new file path name
    raster2 = rname.replace("NAMRIA_gain_10N_110E","Palawan")
    outRaster =  os.path.join("/home/brent/Documents/Hansen_new_data/New_data/10_110/Province/gain/", raster2) #specifying the output raster new name
    warp = 'gdalwarp -te \'%s\' -cutline \'%s\' -crop_to_cutline -overwrite -co compress=LZW \'%s\' \'%s\'' % (extent, shp, raster, outRaster) #calling gdal warp, crop to cutline is specified here
    os.system(warp) #running gdalwarp, uncomment this line to run
  • have you tried without the barckets around the extent values ? It does not seem necessary.extent should simply be a string
    – radouxju
    Commented May 3, 2019 at 10:15
  • yes, I removed it but it says now that the syntax is invalid. Commented May 3, 2019 at 11:09
  • By the way I'm running the QGIS 3.6 (latest) version. Commented May 3, 2019 at 11:28


Your Answer

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

Browse other questions tagged or ask your own question.