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)
lyr.ResetReading()
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.
Best
gdalwarp
is included in the python bindings asgdal.Warp
, no need forsubprocess.call