I wrote this pyhton script to clip a raster with a shapefile:

import ogr
import subprocess

inraster = '/Users/sigc2sige/Desktop/gdal/rast.tif'
inshape = '/Users/sigc2sige/Desktop/gdal/poly.shp'

ds = ogr.Open(inshape)
lyr = ds.GetLayer(0)

ft = lyr.GetNextFeature()

while ft:

       com = ft.GetFieldAsString('com')

       outraster = inraster.replace('.tif', '_%s.tif' % com.replace(' ', '_'))    
       subprocess.call(['gdalwarp', inraster, outraster, '-cutline', inshape, 
                 '-crop_to_cutline', '-cwhere', "'com'='%s'" % com])

      ft = lyr.GetNextFeature()

ds = None

When I execute the code I get this error : FileNotFoundError: [Errno 2] No such file or directory: 'gdalwarp': 'gdalwarp'

I have installed gdal but I don't know why the gdalwarp is not recognized. I'm running on Mac.


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.