GDAL programs#

Overview#

Developed by Vitor Martins

From GDAL webpage: “GDAL is a translator library for raster and vector geospatial data formats that is released under an MIT style Open Source License by the Open Source Geospatial Foundation. As a library, it presents a single raster abstract data model and single vector abstract data model to the calling application for all supported formats. It also comes with a variety of useful command line utilities for data translation and processing.”.

Installation#

The GDAL installation is part of many software, including QGIS. I suggest the OSGeo4W for Windows. Go to the OSGeo4W website: https://trac.osgeo.org/osgeo4w/, select the 64-bit version (most computers), and run the installer. Once you finish it, you can open the command line and verify if your computer is “finding” the gdal programs (e.g., gdalinfo, gdal_translate, gdalwarp etc). Type:

> gdalinfo

If the result is several lines explaining the gdalinfo parameters, you are good! Otherwise, you should set up environment variable to make it easier to use GDAL from the command line. Learn how to set an environmental variable, and you will include the OSGEO bin path (e.g., C:\OSGeo4W64\bin).

You will also need to set up other variables to prevent errors with projections (e.g., cannot find proj.db). If your OSGeo4W is located in “C:\OSGeo4W”, run the below commands line-by-line in the Windows command:

setx PATH "%PATH%;C:\OSGeo4W\bin"
setx GDAL_DATA "C:\OSGeo4W\share\gdal"
setx PROJ_LIB "C:\OSGeo4W\share\proj"

Restart your prompt (close and open it). Now you will test the gdalinfo again.

GDAL raster programs#

Below I selected the most common GDAL programs that you will use for satellite image processing: gdalinfo: Lists information about a raster dataset.

gdalwarp: Image reprojection and warping utility.

gdal_translate: Converts raster data between different formats.

gdalbuildvrt: Builds a VRT from a list of datasets.

gdal_rasterize: Burns vector geometries into a raster.

gdal_polygonize.py: Produces a polygon feature layer from a raster.

GdalInfo#

gdalinfo LC08_L2SP_022039_20190322_20200829_02_T1_SR_B2.TIF
../../_images/gdalinfo.png

Gdal_Translate#

To run, replace the input for the landsat image (LC08_L2SP_022039_20190322_20200829_02_T1_SR_B2.tif)

List raster drivers

> gdal_translate --formats

Convert between raster formats

> gdal_translate -of "JP2OpenJPEG" input.tif output.jp2
> gdal_translate -of "ENVI" input.tif output.envi

Compress my data in LZW

> gdal_translate -of "GTiff" -co "COMPRESS=LZW" LC08_L2SP_022039_20190322_20200829_02_T1_SR_B2.TIF output_compress.tif

GdalWarp#

The gdalwarp has many applications, including reprojection, mosaicing, resampling pixel size, set nodata, cut the raster with other shapefile, and more. To understand the full list of options, see the parameters in gdalwarp page

Clip raster with shapefile for pixels within polygon boundary

> gdalwarp -crop_to_cutline -cutline lake_within_landsat.shp input.tif output.tif

Change no data

> gdalwarp -srcnodata -999999 -dstnodata 0 input.tif output.tif

Reproject raster

> gdalwarp -srcnodata -t_srs "EPSG:4326" input.tif output.tif

Gdal_Build_VRT#

Merge rasters

> dir /b *.tif > filelist.txt
> gdalbuildvrt -input_file_list filelist.txt merged.vrt
> gdalwarp merged.vrt output.tif