Introduction Generic Mapping Tools
Posted on December 20, 2022 • 5 minutes • 927 words • Other languages: Deutsch
The Generic Mapping Tools (GMT) is a collection of tools to create maps and other data visualizations. It shines when you want to create maps for printing (EPS, PDF), but you can also export maps to SVG and to more classical image formats like PNG or JPG. On this page, I will introduce GMT on Ubuntu.
Install GMT
GMT has been part of Debian and Ubuntu for many years. The software has changed a lot throughout the years, so older
descriptions will deviate quite a bit from what I will show here. Version 6 of GMT is included in Ubuntu and can be
used on the command line. In addition, there is PyGMT
for Python, and GMT supports Julia (a language with an
excellent reputation amongst programmers, but not widely used, unfortunately – makes me want to look at it).
To install Generic Mapping Tools on a current Ubuntu (meaning the command line version), you have to install it via
apt:
sudo apt install gmt gmt-gshhs-low gmt-gshhs-high gmt-gshhs-full
gmt-gshhs-full
is only really needed if you want to create very detailed maps. A complete documentation of GMT with
a lot of good tutorial can be found at https://www.generic-mapping-tools.org/
.
The First Example
GMT on the command line is best written in short scripts. Let’s start with a minimal example:
gmt begin europe_outline
gmt set GMT_THEME cookbook
gmt coast -R-11/20/32/60 -JM16c -B -Gblack
gmt end show
This will create a mercator projection of Europe. What do the settings do?
gmt begin europe_outline
starts the creation of a map file europe_outline.pdf.gmt set GMT_THEME cookbook
uses the generic cookbook theme.gmt coast
creates coastlines:-R-11/20/32/60
with the coordinates 11°W/20°E und 32°N/60°N-JM16c
means a classic Mercator projection (M
) with a width of 16 cm (you can also write 6i for inches).-B
creates a border with theme settings.-Gblack
colors the landmass in a nice black.
gmt end show
finishes the output and presents it on the desktop (e.g. with Evince).
The output is quite nice:
You can change the settings a bit to create another output format and make the landmass look more colorful for example:
gmt begin europe_outline png
gmt set GMT_THEME cookbook
gmt coast -R-11/20/32/60 -JM16c -B -Gdarkgreen
gmt end show
Here, the output will be a PNG with a green landmass.
Make a Globe
Another nice example to show the capabilities of GMT:
LON=11.566667
LAT=48.133333
NAME='München'
gmt begin my_world
gmt set GMT_THEME cookbook
gmt coast -Rg -JG${LON}/${LAT}/16c -Bag -Dc -A5000 -Gwhite -SLightBlue
echo "${LON} ${LAT}" | gmt plot -Sc0.16c
echo "${LON} ${LAT} $NAME" | gmt text -Dj0.12/0.12 -F+f16p,Helvetica,DarkRed+jBL
gmt end show
First, we set three shell variables (you can change them, if you like). Then we start creating a PDF with the name
my_world.pdf
:
gmt coast
creates coastlines (and, in our case, defines the map projection in general):-Rg
is the part of the map we want to see. In this case, it is the globe (g
).-JG${LON}/${LAT}/16c
is the projection. We will create a globe (G
) with the shell defined coordinates in the center.-Bag
defines the border and the raster system. We want to see thea
andg
lines, meaning the thicker border around the projected globe and the major longitudes and latitude lines.-Dc
is the resolution of the map. Above, we have installed different gshhs packages. In this case, we want to use the crude map which is good enough for global projections. The other resoultions are full, high, intermediate und low.-A5000
will remove elements from the map that are smaller than 5000 km². This will eradicate smaller lakes and islands and make the map look smoother.-Gwhite
is the fill color of the land mass, white.-SLightBlue
is the fill color of water, light blue, obviously.
gmt plot
can create symbols and lines on a map.-Sc0.16c
will render symbols. More specifically, we will create unfilled circles (lower casec
). The diameter will be 0.16cm.echo "${LON} ${LAT}"
will pipe the coordinates to the gmt command. Alternatively, you can create multiple lines to create many symbols on a map. Or you can retrieve the coordinates from a file.
gmt text
functions similarly, but for text:echo "${LON} ${LAT} $NAME"
pipes coordinates and our city name to the command.-Dj0.12/0.12
defines the distance of text and coordinate. We do not want our the to stick too closely to the symbol.-F+f16p,Helvetica,DarkRed+jBL
looks a bit wild, but is actually quite easy:-F
defines the text format. In our case we use a font (+f
) of size 16p, type Helvetica, dark red color. Moreover, the text should be aligned to the coordinate (+j
): The coordinate will be below left of the text. Note that the distance will be added to this alignment.
You can see the result here:
As you can see, GMT contains a bit of a learning curve. But most of it is pretty straightforward and most of the commands work in a very similar way. So it is easy to start experimenting quite fast. The interesting thing about the command line version is the ease of adding data from other sources. You only need to convert them into simple text files usable in GMT.
You are welcome to test the scripts with other coordinates and projection definitions and check out the results quite quickly. GMT is capable of much, much more. Further information and tutorials on the command line options can be found at Dokumentationsseite von GMT .