1

I am trying to reproject a .tif file then clip it to the boundaries of a shapefile using GDAL Warp. I want to do this in a single script, and have successfully used the Python subprocess module to reproject my data. Can I use the subprocess module a second time to run part 2 of my script (the clipping) or should I envelop both process in a single line? Below is the code I'm using and not having any luck with. Thanks!

Best,

Mike

import os, gdal, subprocess, sys
cmd = ['gdalwarp', '-t_srs','+proj=longlat   +ellps=WGS84','data/band4.tif','data/band4_r.tif']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout,stderr=proc.communicate()
exit_code=proc.wait()

if exit_code: #Oops, something went wrong!
    raise RuntimeError(stderr)
else: print stdout  

# This section will clip my data to a vector's boundary

cmd = ['gdalwarp', '-cutline','clipfeature.shp','crop_to_cutline','-dstalpha, 'data/band4_r.tif','data/band4_r_clipped.tif']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout,stderr=proc.communicate()
exit_code=proc.wait()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.