3.3 GDAL

GDAL, Geospatial Data Abstraction Library is a translator library for raster and vector geospatial data formats

ogr2ogr

Converts simple features data between file formats. In this case we will convert from Shapefiles to GeoJSON.

Binaries exist for macOS, Linux and Windows. See GDAL.org for downloads.

macOS

Using brew gdal-brew

brew install gdal

Ubuntu

Using apt-get gdal-linux

sudo add-apt-repository ppa:ubuntugis/ppa && sudo apt-get update
sudo apt-get install gdal-bin

Test to verify install

ogr2ogr --version

# sample response, the version number could be different
# GDAL 2.3.1, released 2018/06/22

GDAL

GDAL is a translator library for raster and vector geospatial data formats. There are API documentation for C and C++. There are Node.js bindings and Python bindings and a Python cookbook.

There are several great tools for the command line and API’s that you can use to wrangle your geodata. The example that we will be discussing is raster GeoTIFFs from the US Forest Service FSTopo Raster Gateway.

gdal-config

You can use gdal-config to obtain the GDAL version

gdal-config --version

or you can list the supported formats in your GDAL installation

gdal-config --formats

Learning with Mt Whitney

Since we are working straight from the command line, we can start by calling commands to fetch to our GeoTIFFs. We have examples for wget and curl. You can use the FSTopo Raster Gateway if you prefer, but you can also start with the page that shows FSTopos near Mt Whitney.

wget http://data.fs.usda.gov/geodata/rastergateway/data/36118/fstopo/363011815_Mount_Whitney_FSTopo.tif

# or

curl -O http://data.fs.usda.gov/geodata/rastergateway/data/36118/fstopo/363011815_Mount_Whitney_FSTopo.tif

gdalinfo

With gdalinfo you can obtain great metadata about your file

gdalinfo 363011815_Mount_Whitney_FSTopo.tif

# output gdalinfo in json
gdalinfo -json 363011815_Mount_Whitney_FSTopo.tif

# histogram of palette colors
gdalinfo -hist 363011815_Mount_Whitney_FSTopo.tif

gdal_translate

Translating our GeoTIFF to other formats is useful, so we can use gdal_translate to perform operations. One caveat is that when you translate out of GeoTIFF, then you lose all geospatial information.

gdal_translate -of PNG 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.png

# or translate to JPEG
gdal_translate -of JPEG 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.jpg
gdal_translate -of JPEG -expand rgb 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.jpg

# or translate to WEBP
gdal_translate -of WEBP -expand rgb 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.webp

# or translate to uncompressed TIFF
gdal_translate -of GTIFF -expand rgb 363011815_Mount_Whitney_FSTopo.tif 363011815_Mount_Whitney_FSTopo.TIFF

gdal-linux - http://www.sarasafavi.com/installing-gdalogr-on-ubuntu.html. A Good resource with step by step intro.

gdal-brew - http://brew.sh. The missing package manager for macOS.

Back to top
Close