1

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

zakaria mouqcit
  • 889
  • 1
  • 14
  • 28
  • Does gdalinfo work? – SMiller Aug 02 '18 at 15:02
  • May be a duplicate of https://gis.stackexchange.com/questions/59957/use-subprocess-call-with-gdalwarp -- Per that thread, specify the path to gdal. Mac gdal installation instructions on this thread may be helpful: https://gis.stackexchange.com/questions/155403/installing-gdal-on-macosx or at https://tilemill-project.github.io/tilemill/docs/guides/gdal/ – SMiller Aug 02 '18 at 15:06
  • If you're using GDAL 2.1+ then gdalwarp is included in the python bindings as gdal.Warp, no need for subprocess.call – user2856 Aug 03 '18 at 02:05

0 Answers0