Simple GDAL Setup Using Docker


  Reading 3 minutes
  
  Link

TIL hands on docker gdal

I often work with different geospatial data sets, including geolocation information, and I want to present documents in the context of the Map’s position. To get the job done, I use Elastic Kibana Maps for my visualizations. Elastic provides Elastic map service (ESMS), which anyone can use for geospatial visualizations. The service includes different vector layers out of the box and is kept up to date and improved continuously. Only recently, all the Europe regions were completed on ESMS and released to the public.

The issue - dependencies for GDAL latest version

Elastic posted a fantastic blog post on How to Ingest Geospatial Data Into Elasticsearch with GDAL which explains how to use GDAL library in the context of Elasticsearch and geospatial data. The blog post is self-explanatory, but when I run into dependency issues using brew to install the latest version of the library, I started to look for a different solution to set up the GDAL library with the latest features available.

The solution - simple wrapper using Docker

While I was working on an interesting problem and once again run into dependency issues, I got help from a colleague (Thanks to Jose!) who gave me a tip to use wrapper script using Docker with the latest version of GDAL library. The implementation is straight forward - especially when you are already familiar with Docker.

Shell script

The first step is to create a shell script and save it into your favorite location. I prepared a script version for Linux and macOS, which should work without any further changes. The only difference between versions is the mount for the home directory.

macOS

#!/bin/bash
docker run --rm --network=host -u $(id -u ${USER}):$(id -g ${USER}) \
-v /Users:/Users \
osgeo/gdal:alpine-small-latest \
ogr2ogr "$@"

Linux

#!/bin/bash
docker run --rm --network=host -u $(id -u ${USER}):$(id -g ${USER}) \
-v /home:/home \
osgeo/gdal:alpine-small-latest \
ogr2ogr "$@"

Execute the script

The next task is to use and execute your new wrapper for the GDAL library. Either with the full path to your script or update the script’s path in $PATH variable and use it as any other CLI command. I’ll leave it up to you, and I have put together a few examples to help you out with the first run.

Note: The name of the saved script from the previous step is dogr2ogr in my case.

  1. Ingest content of the GeoJSON file directly into Elasticsearch
dogr2ogr -lco INDEX_NAME=index-name  "ES:http://elastic:changeme@localhost:9200"  \
  "$(pwd)/file-name.geojson"

2. Ingest Shape file directly into Elasticsearch

dogr2ogr -lco INDEX_NAME=index-name  "ES:http://elastic:changeme@localhost:9200"  \
  "$(pwd)/file-name.shp"

3. Convert Shape file to GeoJSON format using full path to the script

~/opt/bin/dogr2ogr -f GeoJSON "$(pwd)/file-name.geojson" "$(pwd)/file-name.shp" \
  -lco RFC7946=YES
Note: $(pwd) variable in above examples is important as you must use full paths to the file in the context of the Docker to work correctly with the input.

This simple trick will save you lots of headaches when dealing with the dependencies of a specific library or version of application not only for the GDAL library but also with other applications. Let me know if the setup works for you down in the comments or suggest any improvements you use daily.