How and when to use QGIS 3 tools “Warp (reproject)”, “Clip raster by extent” and “Clip raster by mask layer”
Plus, solved: I cut my raster based on another raster and they have different sizes anyway
Fellow researchers and open-source GIS enthusiasts,
Welcome to my blog!
I’d like to start with a disclaimer – I may be a researcher of this very area but that doesn’t mean everything I do or write here will work for you, in your own desktop configurations and package versions. I have no responsibility if you lose data or mess up your installation. I also do not authorize any copies of my content.
Today, I am discussing different ways of cutting (clipping) a raster in QGIS 3.X and what they do. Especially, I am comparing QGIS GDAL tools “Clip raster by mask layer”, “Clip raster by extent” and “Warp (reproject)” in what are they used for, and what exactly do they do to the original raster data.
Setting the raster to be clipped and the vector mask
In this example, I am using QGIS 3.18 “Zürich” and custom rasters and vectors.
I generated a random 3x4 .tif raster with discrete values from 2 to 7.
With these definitions:
Dimensions X: 3 Y: 4 Bands: 1
Pixel Size 12500,-12500
If I need to cut this raster by a vector mask like this one (Polygon, .shp)
Will anything be cut out?
Yes. But what, it depends on the tool that you use.
Using GDAL “Clip raster by mask layer”
In the tool setup, if you set just the input raster and mask layer
You get this as a resulting raster:
Dimensions X: 1 Y: 2 Bands: 1
Pixel Size 12500,-12500
Yes, just these two pixels. Why? Let’s check the original raster and mask again:
All outer pixels were cut by the mask layer, so the algorithm kept just the inner two.
Using GDAL Clip Raster by Extent
In the input setup, choose the input raster layer and choose the vector layer for which the extent will be used.
Run the algorithm.
The resulting raster is:
Dimensions X: 3 Y: 4 Bands: 1
Pixel Size 12500,-12500
The tool cut the rightmost column of the raster and added a padding of zeros on its left, as we can see when I plot the generated raster along with the original raster:
Using a different vector mask (in the image, over the original raster):
Results in only the pixels that are totally outside of the mask extent cut out of the raster.
Dimensions X: 2 Y: 2 Bands: 1
Pixel Size 12500,-12500
Using GDAL “Warp (reproject)”
Adding the mask as an extent:
When you click the mask layer, its extent is filled on the “Not set” field. Pick the CRS of the layer chosen (in this case, “retangulo” is in WGS84).
Run the algorithm.
This is the result I got:
Dimensions X: 3 Y: 4 Bands: 1
Pixel Size 12185.00049795155974,-11712.21441720891744
The pixel size changed, but the dimensions of the raster are the same. This means the extents of the raster are different from the original one. In order to show it more clearly, I applied a different symbology on this one:
Turning both layers on, “Rand Raster” (the original) and “Warp Reproject” (the new one) we can see that the raster was, for sure, clipped:
So, the differences are
Clip raster by mask layer leaves any pixels that are partly out of the mask, out of the resulting raster. Keeps the resolution of the raster and does not perform any interpolation. Use this one when your goal is to preserve the original values and resolution of the raster. Also, use this one when you need to use a cutting mask which is not rectangular.
Clip raster by extent clip leaves any pixels that are completely outside of the mask bounds out of the resulting raster. It also keeps the resolution of the raster and does not perform any interpolation. Use this one when your goal is to preserve the original values and resolution of the raster. You can either use vector or raster masks for cutting (in case of rasters, the extent of the raster will be used).
Warp (reproject) clips the raster exactly over the mask that has been provided. To do that, an interpolation is needed (though the default one is Nearest Neighbor, that copies the value from the original raster pixel that is closer to the each pixel of the created raster). Pixel sizes and resolution may change. Use this one when you need to adjust the size of a raster to match a given extent (from either a vector or a raster).
Are Warp (reproject) and Clip raster by mask layer based on the same GDAL tool?
Yes, they are.
GDAL Warp is a very flexible tool, that has many options. These two QGIS 3.18 tools are adaptations of Warp with different options. You can check them all at https://gdal.org/programs/gdalwarp.html
You can see this on the setup screen before running the algorithm, below “GDAL/OGR console call”.
For Clip raster by mask layer:
gdalwarp -of GTiff -cutline D:\...\retangulo.shp -cl retangulo -crop_to_cutline D:/… /rand_raster.tif C:/…/OUTPUT.tif
The options used are “-cutline” and “-crop_to_cutline”.
“-cutline” selects the vector cutline to be used to crop the raster. “-cl” is the layer from the vector to be used. “-crop_to_cutline” changes the extent of the generated raster accordingly. If “-crop_to_cutline” is not used, the resulting raster keeps its old extent, but locations outside the vector mask receive the “No Data” value.
For Warp (reproject):
gdalwarp -r near -te -52.62951648 -29.26231411 -52.248264859 -28.84361955 -te_srs EPSG:4326 -of GTiff D:/… /rand_raster.tif C:/…/OUTPUT.tif
The options used are “-r”, “-te”, and “-te_srs”.
“-r near” chooses the interpolation as Nearest Neighbor. “-te -52.62951648 -29.26231411 -52.248264859 -28.84361955” are the limits of the mask layer provided as extent (“retangulo”). “-te_srs EPSG:4326” is the CRS in which this extent is given.
Which GDAL tool is associated with “Clip raster by extent”?
GDAL translate. This can be seen on GDAL/OGR console call:
gdal_translate -projwin 342205.0947980235 6794960.329155264 365666.03336452536 6771499.432501004 -of GTiff D:/…/rand_raster.tif C:/…/OUTPUT.tif
The “-projwin” option selects a window (extent) from which the raster will be cut.
What if I want to clip the raster by the mask, but still don’t want the pixel size to change?
If you fill the “Output file resolution in target georeferenced units” you can ensure that the pixel size will be the one enforced. Fill it with the value from the original raster. However, in a case like the example shown, all the algorithm will do is to “move” your whole raster to the start of the mask, since you forced the raster pixels to have this definite size.
Dimensions X: 3 Y: 4 Bands: 1
Pixel Size 12500,-12500
Can I clip a raster based on another raster on QGIS?
Yes, you can. To do that, you should, on Warp (reproject) tool, choose a raster on your cutting extent (“Georeferenced extents of output file to be created”). This will create a raster that has exactly the same extent as the selected layer. To make sure the grids match completely, choose the raster as your cutting extent and fill the pixel size with the pixel size of the grid you want to match. For best results, choose an adequate interpolation.
Can I clip a raster based on another raster WITHOUT interpolation, on QGIS?
Yes. To do that, pick the tool “Clip raster by extent” but be aware that the resulting extent can be different from the raster used as a clipping mask! This will happen unless the grids of the rasters match perfectly.
I cut my raster based on another raster and their extents don’t match anyway. What could have happened?
You probably used “Clip raster by extent” when you should use “Warp (reproject)”. Try again using “Warp (reproject)” tool.