I have to clip a quite large raster (corn crops) into individuals rows. It's working fine with the "Clip raster by mask layer" tool with a grid I created.

However, I need the clipped images to be named after the mask feature that's been used.

I've created a new feature in the grid so every cell is named how I want it to.

Is there a way to do so?

  • Maybe splitting the mask layer into several layers each containing a single feature, then clipping the raster using batch processing and your attribute could work.
    – Erik
    May 25 at 12:42
  • I've tried this but I dont now what expression/formula to use to name the output file after the mask..
    – basile
    May 25 at 12:51

2 Answers 2


If you've split the grid into individual layers that are saved in a directory, then you can select that directory (using Autofill...) as the Mask layer field for Clip raster by mask layer tool in Batch Processing mode.

enter image description here

Then for the Clipped (mask) field, select Autofill... -> Calculate by expression, and use an expression like:

file_path(@MASK) + '\\' + base_file_name(@MASK)+'.tif'

to translate the mask filenames into corresponding output filenames. Obviously you may need to play with the details to get it to work for your filesystem.

enter image description here


You can use pyqgis and iterate over each polygon, create a temp mask layer using its id, clip using the mask layer:

import os
output_raster_folder = r"C:\temp\test" #Adjust
rasterlayer = QgsProject.instance().mapLayersByName("land_use")[0] #Adjust
vectorlayer = QgsProject.instance().mapLayersByName("clip_layer")[0] #Adjust
name_field="name" #Adjust

for feature in vectorlayer.getFeatures(): #For each polygon in vectorlayer
    #Create a temp layer using current feature id
    masklayer = vectorlayer.materialize(QgsFeatureRequest().setFilterFid(feature.id()))
    name = next(masklayer.getFeatures())[name_field] #Fetch its name from name_field

    print(f"Clipping polygon: {name}")
    #Create output raster name using the name: #Example: 'C:\\temp\\test\\clip_c669.tif'
    output_raster = os.path.join(output_raster_folder, f"clip_{name}.tif") #Adjust if you want to 
    #Clip using the temp layer of only one polygon feature
    processing.run("gdal:cliprasterbymasklayer", {'INPUT':rasterlayer,

enter image description here

Your Answer

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

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