10

I have calculated the dnbr for a fire season across a whole region, and I've clipped it using a mask.

The mask is a layer of ground survey plot locations because I want to find out the dnbr for each plot. Each plot has a number under the field 'ID'. I have clipped the dnbr to each plot location but has returned (over 100 of) these new clipped files as 'new_clip'and a number irrelevant to the plot number. So I can't see which dnbr matches to which plot. I would like the new clipped files to look something like 'ID_247_clip'.

I've done this via Raster -> Extraction -> Clip raster by mask layer: input is Whole Region dNBR, overlay is plot locations. Then I've clicked the 'Iterate over this layer' button. It gives me exactly what I need, just with the wrong title for the clipped layers.

Is there a way to program the clip to name each layer according to the plot numbers in the ID field?

I've tried executing in batch mode but this doesn't work, as I only have one input and one overlay layer, whereas bulk seems to work for multiple input layers. Also, a lot of the plot locations overlap, so I can't do anything like join by location.

enter image description here

4
  • Screenshots would sure help me visualize your issue!
    – Stu Smith
    Jul 5, 2020 at 16:28
  • Hi Stu thanks for replying, I have put a screenshot on there now! :)
    – Emma
    Jul 5, 2020 at 17:19
  • Can you give us the GDAL/OGR console call? Hidden in the screenshot. Could help to provide a fix
    – ThomasG77
    Jul 6, 2020 at 0:31
  • Hi, were you able to find a solution? Is the mask layer used a single layer(file)?
    – mapperx
    Jan 3, 2022 at 9:41

1 Answer 1

1

Here's a possible approach to solving your problem.

Split Mask Layer

Start by splitting the mask layer. This can be done in QGIS using Split Vector Layer. If you use the ID as the unique field to split on, this will generate a list of masks of the form ID_1, ID_2, ... You can create these in a variety of formats, but let's assume Geopackage (GPKG), and they have all been saved to a single directory.

Bulk Clip Raster - inside QGIS

You can then use Clip raster by mask layer in Batch mode.

For the Mask layer, select Add Files by Pattern..., and use *.gpkg on the directory from above. enter image description here

For the output (Clipped (mask)), you can use Calculate by Expression.... I used the following formula for the expression, which puts the output file names in the same format as your question:

file_path(@MASK) + '/' + base_file_name(@MASK) + '_clip.tif'

Any other values you need (eg the Input layer can be selected once and then Autofilled in the batch interface.

Bulk Clip Raster - outside QGIS

Alternatively, Clip raster by mask layer is just a wrapper for the gdalwarp command line tool. So outside QGIS, you can use a batch file or command line to call the gdalwarp tool. For example, in Windows, a command line like the below, run in the directory where the mask files are saved will iterate through each mask, and create .tif files named ID_1_clip.tif, ID_2_clip.tif etc. (%~nf stands for the filename without extension)

for %f in (*.gpkg) do call gdalwarp -overwrite -of GTiff -cutline %~nf.gpkg -cl %~nf -crop_to_cutline D:/path_to/input.tif %~nf_clip.tif
2

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.