Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

How to clip a raster using a shapefile in GDAL

Clipping Rasters using GDAL and QGIS

In this article, we will explore how to use GDAL and QGIS, two widely used open-source GIS tools, to clip a raster using a shapefile. We will go over the basic syntax and options of the gdalwarp command in GDAL and the corresponding tools in QGIS.

Let’s start with GDAL (Geospatial Data Abstraction Library)

GDAL (Geospatial Data Abstraction Library) is a powerful open-source library that provides a wide range of command-line tools for working with geospatial data. These tools allow you to perform various operations on raster and vector data, including reading, writing, converting, reprojecting, and more. Some of the most popular command-line tools in GDAL are:

  • gdalinfo: This tool provides information about the format, size, projection, and other metadata of a raster file.
  • gdal_translate: This tool allows you to convert raster data from one format to another and to perform various other operations like resampling, changing the data type and setting the no data value.
  • gdalwarp: This tool allows you to warp or reproject raster data from one coordinate system to another, resample data, and perform other operations like cutline and cropping.
  • ogr2ogr: This tool allows you to convert vector data from one format to another, perform spatial and attribute selections, and perform other operations like dissolving, merging, and more.

GDAL command line tools are widely used by GIS professionals and researchers for data processing, conversion, and analysis. They are designed to be efficient and can handle large datasets, making them well-suited for automating GIS workflows. They are also well-documented and have a good user community which means it is easy to find examples of how others are using them.

Clipping a raster with a shapefile using Gdalwarp

You can use the gdalwarp command in GDAL to clip a raster using a shapefile. The basic syntax is:

gdalwarp -cutline path/to/shapefile.shp -crop_to_cutline input.tif output.tif

This will clip the input raster (input.tif) using the shapefile (shapefile.shp) and save the output as a new raster (output.tif). The -cutline flag specifies the shapefile to use for clipping, and the -crop_to_cutline flag tells gdalwarp to actually perform the clipping operation.

You can also use the -dstnodata flag to set the value of pixels that are outside of the shapefile,

gdalwarp -cutline path/to/shapefile.shp -crop_to_cutline -dstnodata 0 input.tif output.tif

In this example, the -dstnodata flag set the value of the pixels that are outside of the shapefile to 0.

Here is a table of some common parameters for the gdalwarp command in GDAL, along with a brief description of what they do and an example of how to use them:

ParameterDescriptionExample
-ofOutput format. Can be set to a variety of raster formats (e.g. GTiff, PNG, etc.).gdalwarp -of GTiff input.tif output.tif
-rResampling method. Can be set to one of several resampling algorithms (e.g. nearest neighbor, bilinear, cubic, etc.).gdalwarp -r cubic input.tif output.tif
-t_srsTarget SRS (spatial reference system). Can be set to a EPSG code or a proj4 string.gdalwarp -t_srs EPSG:4326 input.tif output.tif
-teTarget extent. Can be set to a set of coordinates defining the bounding box of the output image.gdalwarp -te -120 40 -110 30 input.tif output.tif
-trTarget resolution. Can be set to a set of x and y resolution values.gdalwarp -tr 30 30 input.tif output.tif
-cutlineCutline shapefile to use for clipping.gdalwarp -cutline path/to/shapefile.shp input.tif output.tif
-crop_to_cutlinePerform the clipping operationgdalwarp -cutline path/to/shapefile.shp -crop_to_cutline input.tif output.tif
-dstnodataSet the value of pixels that are outside of the shapefile.gdalwarp -cutline path/to/shapefile.shp -crop_to_cutline -dstnodata 0 input.tif output.tif

These are just a few examples of the many options available with gdalwarp, there are many others, you can check the documentation for more details.

Automating the process

Here are some examples of how you can call the gdalwarp command from a batch file and a Python script:

Batch file:

gdalwarp -t_srs EPSG:4326 input1.tif output1.tif
gdalwarp -r cubic -te -120 40 -110 30 input2.tif output2.tif
gdalwarp -tr 30 30 -cutline shapefile.shp input3.tif output3.tif

You can create a .bat file, with the commands above, open the command prompt and navigate to the directory where the .bat file is located and execute the file.

Python script:

import subprocess

subprocess.call("gdalwarp -t_srs EPSG:4326 input1.tif output1.tif", shell=True)
subprocess.call("gdalwarp -r cubic -te -120 40 -110 30 input2.tif output2.tif", shell=True)
subprocess.call("gdalwarp -tr 30 30 -cutline shapefile.shp input3.tif output3.tif", shell=True)

You can call these commands using the subprocess module in Python. Make sure the gdalwarp command is in the PATH or specify the full path to the gdalwarp.exe file.

It is also possible to use os.system() to call gdalwarp, but it is not recommended as it does not return the exit code and it does not allow capturing the standard output.

Using QGIS to clip a raster with a shapefile

Here are the basic steps to do this:

  1. Open QGIS and add the raster and shapefile to the project.
  2. Go to the “Raster” menu and choose “Extraction” > and choose either “Clip Raster By Extent”.
  3. In the Clipper dialog box, select the input raster and shapefile, and choose the output file name.
  4. Click “OK” to run the clip operation.

Keep in mind that QGIS uses GDAL under the hood, so it will use the same algorithms, parameters and options as gdalwarp.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.

Leave a Reply