I'm trying to clip ~300 rasters from a time series by vector masks to extract only reliable pixels - the vector polygons are different per raster. The file names are corresponding eg. rasterlayer_YEAR_DOY.tif and masklayer_YEAR_DOY.shp.

When using batch processor of the "clip raster by mask layer" tool I can add a whole directory of files to either the input or mask column, but if I then add a directory for the other it loads below the rows containing data in the other column, and auto-fills the missing column with a single raster or mask layer.

Is there a way to match the raster and mask layer in the batch processor? It seems like there must be a quicker way to do this than manually select the matching layers.

I'm not a Python user, but I'm familiar with R if there's an easier solution that way.

1 Answer 1


When you select the folder with your rasters, you can than use for the mask layer Autofill... / Calculate by Expression…. There, you have a context variable @INPUT at your disposition: this returns the whole path of the corresponding raster layer you used in the input of the respective row.

Use QGIS expressions to delete those parts of the path you don't need and keep only the file-name, than add file-extension and path to your vector-layer folder.

In the example below, I used this expression (where [path] is the path of the tif-files on my system). Be aware with forward/backward backslashes: / vs. \:

replace ('C:/Users/DU/Desktop/temp/'  || replace (@INPUT , '[path]/Geodaten/HoehenDaten/GTOPO30/', ''),'tif','shp')

Screenshot: as you see, using an appropriate expression, you get a new path with a shapefile with the same name as the input tiff-file, based on the variable @INPUT:

enter image description here

  • Unfortunately results in the same problem for me as the other methods! Adds the vector files but in new rows below the rasters. Thank you for your answer - not sure why it doesn't work for me.
    – wpier
    Jul 1, 2021 at 14:12
  • Did you also try the second entry in the autofill menu: Calculate by Expression…? This should work
    – Babel
    Jul 1, 2021 at 14:16
  • That worked instantly - thank you so much!
    – wpier
    Jul 1, 2021 at 14:54
  • Great to hear it worked! Sorry for the first version with the wrong command.
    – Babel
    Jul 1, 2021 at 15:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.