[GIS] Tiling Rasters based on an existing Polygon Shapefile (Vector Grid)

gdalopen-source-gisrastershapefiletiles

I have a tile index representing the tile scheme used to tile Lidar data. I want to tile imagery rasters using the same tiling scheme. I have not found anyway using open source tools to tile a raster based on a shapefile template. All the tools I have found require you to know the tile size and origins.

Are there any tools (especially GDAL) that will do this out of the box?

Best Answer

I don't think there is a direct way to do it, but the following bash script should work for you. It's a two step procedure :

  1. You'll need to create temporary shapefiles for each feature so that you can extract the feature Extent
  2. You will then pass the extent to gdal_translate.
for id in $(ogrinfo -ro -al input.shp | grep uniqueID | grep -o '[0-9]*'); 
do 
   ogr2ogr tmp_ouput$id.shp input.shp -where "uniqueID = $id"; 
   extent=$( ogrinfo -al -so input$id | grep Extent | sed 's/Extent: (//g' | sed 's/)//g' | sed 's/(//g' | sed 's/-/,/g' | awk -F, '{print $1 $2 $3 $4}' ); 
   gdal_translate -projwin $extent -of GTiff input_raster.tif subset_raster.tif"; 
done

You'll need to replace 'uniqueID' with a unique Feature or Object ID in your shapefile.
Kudos to this post as it was very useful.