GDAL Shapefile – How to Clip Raster by Shapefile in Parts?

clipgdalgdalwarpshapefile

I am trying to write .sh script command to clip raster by polygonal shapefile. Shapefile have name field with unique name of polygon feature. On the output must be rasters with name of polygons in shapefile. I'm use Linux, GDAL.
Now I use command

gdalwarp -dstnodata -9999 -cutline data/shapefile.shp data/input.tiff data/output.tiff

but in output I receive one raster.

Example on the picture:

enter image description here

Best Answer

Reorganise your shapefile so that one shapefile contains one feature (A,B,C in your case) only

Then use a loop like

for i in A B C; do
  gdalwarp -cutline $i.shp ... $i.tif
done

to create each output raster.

Example of script:

#!/bin/sh
#    "shp" - folder for shapefiles 
#    "outputraster" - folder for output rasters
cd /home/user/cliprasters/
#  delete previous files
rm ./outputraster/*.tiff
#  run
for fname1 in ./shp/*.shp do
  fname2="$(basename $fname1 .shp)"
  gdalwarp -dstnodata -9999 -cutline ./shp/$fname2.shp -crop_to_cutline -of GTiff ./input.tiff ./outputraster/$fname2.tiff
done