Quick GDAL

WHAT IS GDAL / OGR

GDAL or the Geospatial Data Abstraction Library is awesome. Its one of the foundations of geospatial software and mastering it can really help with daily work processes.

Originally this thing was two separate libraries, GDAL for Raster Data manipulation, ORG for vector manipulation. This separation between the two libraries still exists but the two are now bundled together.

A lot of Geospatial tools use GDAL under the hood, so you’ve probably used it without knowing. A good place to find them is in QGIS and you will see a lot of the tools are GDAL commands with a UI over the top. And that is because fundamentally GDAL/OGR is a command line tool, which is super helpful for automating tasks. But if your only doing the task once then perhaps it’s best to just complete it in QGIS with the help of a UI.

So without further ado, here are some of the things I’ve been using GDAL for recently.

Case 1: It can be alternative to export data when other tools are failing

I’ve hit the use case where for some reason more advanced (by that I mean something with a UI) fails to export large data without losing data. When this happens I used to revert to Python but now I go directly to the ogr2ogr command (for vector).

Here are the relevant docs. And it’s also useful when you want to make some kind of selection export such as:

ogr2ogr -where "GP_RTP=1 and Shape_Leng<1" eu_motorways_tmp.gpkg GRIP4_Region4_vector_shp/GRIP4_region4.shp

Case 2: Using SQL embedded into a GDAL command

Fix this case, make a git push and update the script.

ogr2ogr "$COVERAGE_OUTPUT" "$COVERAGE_INPUT" -nlt PROMOTE_TO_MULTI -dialect sqlite -sql "SELECT ST_Union(geometry) AS geometry FROM """MyData"""" -f "ESRI Shapefile"

This is a really efficient way to dissolve the geometry as well as being automatable.

This complex dataset becomes the front page for our application.

Case 3: a quick check on the diff between coordinate systems

Often I receive data in a variety of, let’s say, exoitc formats because they are coming from the flight planning software used by local coordinate systems. Often I’m not familiar with the exact system and would like to make quick checks to see how it relates to wgs84. Rather than using some fancy tool or website you can easily do this on your terminal:

gdaltransform -s_srs EPSG:32632 -t_srs EPSG:4326

Case 4: automating the simplification process

I often receive a data extract from a provider which is highly detailed, too much so to quickly load into a web server. One of the tricks I’ve come up with is running a simplification process which is available in GDAL. This one line has saved me hours and hours and hours of work.

# Simplification Process

echo "Simplification process begins"

ogr2ogr $SIMPLIFICATION_DATA "$FLAT_DATA" -simplify $SIMPLIFICATION_FACTOR

echo "Output simplified geometry file is called: "$SIMPLIFICATION_DATA""

And there is a separate blog with more details on this here.

Case 5: performing a quick vector to raster conversion

Simple vector to raster (low res) in one line.

gdal_rasterize -ts 100 100 -a_nodata -9999 -burn 1 ne_10m_admin_0_countries.shp admin.tif

Warning: GDAL is powerful and while experimenting I managed to output 80GB files pretty quickly without noticing, just because GDAL was putting them out so fast I assumed they were smallish; not the case.

The same command with a roads dataset give me a quite acceptable road network in a raster format.

gdal_rasterize -ts 100000 100000 -a_nodata -9999 -burn 1 gis.osm_roads_major_v05.shp roads.tif

The step after this was to use some proprietary software to build the raster pyramids but it completely failed me; so this experiment ended here but it is cool to be able to quickly rasterize any feature.

Thanks for reading folks,

Lucas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: