I am currently using batch process in QGIS based on clip raster by extent to clip geotiff raster layers based on different shapefiles. I am trying to clip many raster layers from the same region with multiple discrete shapefiles. I previously tried gdalwarp but this approach changed the image dimension. Can anyone suggest a way in Python to automate the raster clipping based on multiple shapefiles?

Example of a raster layer and few shapefiles:

enter image description here

I merged all the shapefiles to one file and tested the code from the link below in jupyter notebook, but it returns error. Clipping raster with multiple polygons and naming the resulting rasters

import glob, os
import fiona
from subprocess import Popen

path= 'C:/dataset'

with fiona.open('polygons.shp', 'r') as dst_in:
    for index, feature in enumerate(dst_in):
        with fiona.open('separated/polygon{}.shp'.format(index), 'w', **dst_in.meta) as dst_out:

polygons = glob.glob('separated/*.shp')  ## Retrieve all the .shp files

for polygon in polygons:
    feat = fiona.open(polygon, 'r')
    name = feat['properties']['name']  #name of the attribute considering the region name
    command = 'gdalwarp -dstnodata -9999 -cutline {} ' \
              '-crop_to_cutline -of GTiff ./input.tiff ./outputraster/{}.tiff'.format(polygon, name)
    Popen(command, shell=True)

TypeError                                 Traceback (most recent call last)
<ipython-input-21-8ce8105d9f76> in <module>
      8 for polygon in polygons:
      9     feat = fiona.open(polygon, 'r')
---> 10     name = feat['properties']['name']  #name of the attribute considering the region name
     11     command = 'gdalwarp -dstnodata -9999 -cutline {} ' \
     12               '-crop_to_cutline -of GTiff ./input.tiff ./outputraster/{}.tiff'.format(polygon, name)

TypeError: 'NoneType' object is not subscriptable
  • Hi Sam. Are you concerned with the processing time itself or are you manually running the tool for every raster / shapefile clipping combination? Also please clarify - are you using multiple shapefiles to clip multiple rasters? My guess is you have many rasters to apply the same clipping operation to with multiple shapefiles. Just checking. Mar 2, 2021 at 14:57
  • @ NW_Photo_Laureate, I have many raster layers from the same geographic region and also multiple shapefiles (polygons) from this study area. I want to repeat the clipping task for each raster layer using the discrete multiple polygons.
    – Sam
    Mar 2, 2021 at 15:15
  • gdal clip raster by extent uses a bounding box by the way. So N, S, E, W essentially. If your shapefiles are not boxes aligned with the x, y coords then a different gdal tool / qgis function will be necessary. Are your shapefiles bounding boxes? Mar 2, 2021 at 15:24
  • @ NW_Photo_Laureate, all the shapefiles are square covering 128x128 pixels. The N, S, E , and W coordinates are floating numbers. After performing the clip with gdal, raster dimension becomes 127x127 with integer coordinates for N, S, E , and W. Apart from gdal, "clip raster by extent" in QGIS does the task but it needs lots of repetitions.
    – Sam
    Mar 2, 2021 at 16:24
  • @ NW_Photo_Laureate, I tested a code as mentioned in the question, but it returns error.
    – Sam
    Mar 2, 2021 at 18:11


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.