Published in


Create an animated gif of cartographic data via command line

The MapServer way to do it

A person I respect very much, has written to me an email in which he asks to me how to create an animated gif of cartographic data, using the simple and convenient ways typical of the command line.

Several ways to do it, here is the MapServer one.

The goal

What I did was build an animation GIF with my collecting sites in Tasmania. The data are from here: https://doi.org/10.3897/BDJ.9.e70463 but I extracted the Tasmanian collecting events and reduced them to a 3-column table — year, lat, lon — with unique entries (sort | uniq).

The data are points, and in the desired output the red points are those of the specified year and the blue ones, those of the previous years. This is the GIF created by my friend, but his problem was how slow he was doing it:

The slow part was making each of the year categories visible, one after the other, changing the marker stylings and exporting as a PNG with year as filename — 45 minutes.

The MapServer way

MapServer is an Open Source platform for publishing spatial data and interactive mapping applications to the web. Originally developed in the mid-1990’s at the University of Minnesota, MapServer is released under an MIT-style license, and runs on all major platforms (Windows, Linux, Mac OS X).

It has a cli — mapserv - that you can use to create outputs. Moreover it can use run-time substitution to change variables values at run-time.

Then I can use it to create a PNG output file for every year, and then to process these and create an animated GIF.

To use MapServer it’s necessary to create a map file, a configuration file in which set the geographical bounding box, the input layers you want to use, the graphic styles to apply, etc..
I will not explain here what the syntax is, it is not the purpose of this post, I'll just make a few comments.

Some points for the example map file I have created:

  • I have set a decimal degrees bounding box (EPSG:4326);
  • the coordinate reference system (CRS) of the output is again “latitude, longitude”, but it is possible to set the CRS you want;
  • I have used 3 layers: 1) one to have a background map, the polygons of Natural Earh Admin 0 – Countries layer, low resolution, used to create an example; 2) the data layer, a 3 fields CSV (decimalLatitude, decimalLongitude and year); 3) the layer used to add the year as a label;
  • I have set 2 styles for data layer, the red dots for the specified year and the blue dots the previous years. The definition is by variable, passed at run-time via cli (EXPRESSION ("[year]" < '%year%')).

The data layer, is defined as GDAL/OGR virtual layer, that is one of MapServer input formats. It’s based on a XML definition file, to map CSV coordinates fields (and some other metadata).

Then to create a PNG, just run something like:

mapserv -nh "QUERY_STRING=map=data.map&mode=map&year=1977" >1977.png

data.map is the Mapserver configuratione file and year is a variable that you can change at run-time. Then in bash it could be:

for i in {1975..2000}; do
mapserv -nh "QUERY_STRING=map=data.map&mode=map&year=${i}" >${i}.png

To add for each year, the year as label, I have added this in the query (labelyear is the name that I use to add labels to the PNG output):


It’s possible to do it, because in MapServer you can change map file parameters via URL.

The bash script

I have created a bash script to run all:

  • first of all it downloads the data layer and extract decimalLatitude, decimalLongitude and year;
  • then it creates a PNG for every year, using MapServer cli;
  • then it creates the below animated GIF using convert ImageMagick utility.

The tools I use in it are MapServer, Miller and ImageMagick.

The output



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store