In this series: Part VII, Repeating and sharing. The chapters before made pictures. This one makes a complete map sheet, with a title, names, a legend and its sources
You will build: Two printed maps of the seas around Indonesia, with title, legend, coordinates, scale, notes and curved names, in two languages, drawn by GDAL alone
You need: Chapter 0, for the environment, and section 5 of Chapter 1, for lines that work in every shell. Chapters 4 and 5 explain the earthquakes and the storms. About 1 GB of free space
Time to run: The commands took about three minutes when tested, including 53 MB of downloads.
Level: Advanced; the lines are long, and each one does a single job
Every chapter of this tutorial ended with a picture. None of them was a finished map. A finished map has a title, a legend, coordinates, a scale, the sources of its data, and names. Usually, these are added in another program, for example in the print layout of QGIS.
This last chapter adds them with GDAL alone. It makes two sheets for printing, and every letter on them is placed by a line of SQL. This is possible because a font can be a layer of polygons, and GDAL can draw a layer of polygons.
NoteLearning objectives
By the end of this chapter you will be able to:
put several datasets over a relief, with transparency;
write text on a map with SQL, straight or along a curve;
keep all words of a map in a small table, and change the language with one word;
make a legend that follows the same rules as the map;
draw a graticule, a scale bar and a small globe that shows where the map lies;
show the sea floor as layers of paper, with soft shadows;
write a file in a size for printing.
Before running this chapter, do section 1 of Chapter 0 and section 5 of Chapter 1. In a terminal, make a new empty folder and copy into it the files of sample-data/ch17. In this notebook, the next cell does that.
import sysimport timefrom pathlib import Path# The repository root, whether the notebook runs from en/, id/ or the root itselfROOT = Path.cwd() if (Path.cwd() /"tutorial.yml").exists() else Path.cwd().parentsys.path.insert(0, str(ROOT /"src"))import onelineT0 = time.perf_counter()WORK = oneline.start("17")print("working folder:", WORK.name, "| files copied there:", sorted(f.name for f in (ROOT /"sample-data/ch17").iterdir()))
A plan for the sheet. Both maps have the same size. The sheet reaches from 87.2 to 147.2 degrees east and from 23.4 degrees south to 19 north, and the frame of the map inside it from 90 to 145 and from -18 to 14. Everything outside the frame is margin, for the title and the legend. So the positions of the storms and the position of the title use the same coordinates, in degrees.
Warm-up
A few short commands first, in the form of Chapter 0. The folder has a copy of the background of this map, the relief of the land and the sea floor. Three commands make a picture of it, the fourth one gives it a frame, and the last one makes it smaller:
Warning 1: -srcwin -60 -60 1950 1440 falls partially outside source raster extent. Pixels outside the source raster extent will be set to the NoData value (if defined), or zero.
The frame is made by cutting out an area that is larger than the picture. --window takes the first column and row, and the width and height of the area. It starts 60 cells outside the picture and is 120 cells wider and higher than it. --allow-bbox-outside-source permits this, and the cells that do not exist become 0, which is black. The warning of GDAL is about this.
Look before moving on. This is a picture, and not a map yet. It has no names, no scale, no legend and no source, and nothing shows which places are important. The rest of this chapter adds them one by one.
1. The background
ETOPO 2022 is a relief of the whole Earth, on land and under water (ledger L58). The file on the server covers the world. -projwin downloads only the part that is needed:
The colours and the shading are two pictures of the same relief. blend joins them: with hsv-value, the hue comes from the colours, and the brightness comes from the shading. --opacity 45 uses less than half of the shading, because the background must not be stronger than the data on top of it.
If the NOAA server cannot be reached, use the copy in the chapter’s folder, which has half the resolution. Write gdal_translate offline_relief.tif relief.tif in place of the first line. In the same way, ogr2ogr -overwrite map.gpkg offline_data.gpkg.zip can replace the first three lines of section 2. Chapter 1 shows more about working with local files, in section 9.
2. Storms and earthquakes
The two datasets are from earlier chapters: the storms of Chapter 5, with a line for each step, and the earthquakes of Chapter 4, from a larger box. The third line adds the land:
ogr2ogr-overwrite map.gpkg "/vsizip//vsicurl/https://www.ncei.noaa.gov/data/international-best-track-archive-for-climate-stewardship-ibtracs/v04r01/access/shapefile/IBTrACS.since1980.list.v04r01.points.zip"-dialect SQLite -sql"select name, kt, makeline(b,geom) geom from (select name, usa_wind kt, geometry geom, lag(geometry) over (partition by sid order by iso_time) b from 'IBTrACS.since1980.list.v04r01.points' where track_type='main' and iso_time<'2024') where kt>=34 and st_x(b) between 80 and 155 and st_x(geom) between 80 and 155 and st_y(geom) between -25 and 20"-nln step -nlt LINESTRING -a_srs EPSG:4326ogr2ogr-update-overwrite map.gpkg "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=1990-01-01&endtime=2026-01-01&minmagnitude=5&minlatitude=-19&maxlatitude=15&minlongitude=89&maxlongitude=146"-dialect SQLite -sql"select mag, st_z(geometry) depth, geometry geom from OGRGeoJSON"-nln quake -dim XY -a_srs EPSG:4326ogr2ogr-update-overwrite map.gpkg /vsizip//vsicurl/https://naciscdn.org/naturalearth/50m/physical/ne_50m_land.zip -nln land -nlt PROMOTE_TO_MULTI -clipsrc 80 -30 155 25ogr2ogr-f CSV /vsistdout/ map.gpkg -sql"select 'steps of storms' layer, count(*) shapes from step union all select 'earthquakes', count(*) from quake"
layer,shapes
steps of storms,41182
earthquakes,12650
Now turn the data into marks. A mark is a shape with a class number v, and the colour file will give each class its colour. The order of the rows is the order of drawing:
ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"select 13 v, st_buffer(st_boundary(geom),0.025) geom from land union all select * from (select case when depth<70 then 10 when depth<300 then 11 else 12 end, st_buffer(geom,0.03+0.035*pow(1.7,mag-5)) from quake order by depth desc) union all select * from (select case when kt<64 then 3 when kt<96 then 4 when kt<137 then 5 else 6 end, st_buffer(geom,0.03) from step order by kt)"-nln mark -nlt GEOMETRYgdal pipeline read map.gpkg ! sql "select v, geom from mark" ! edit --crs EPSG:4326 ! rasterize --attribute-name v --init 0 --ot Byte --size"5400,3816"--extent"87.2,-23.4,147.2,19" ! edit --nodata none ! color-map --color-map night.txt --add-alpha ! materialize ! resize --size"1800,1272"-r average ! write marks.tif --overwritegdal raster blend night_ground.png marks.tif data.png --overwrite
Part
Meaning
order by depth desc
the deep earthquakes first, so that the shallow ones lie on top
pow(1.7,mag-5)
each step of magnitude makes the dot 1.7 times as wide. The energy grows much faster than that (Chapter 4), and dots in true proportion would cover the map
--add-alpha
night.txt has a fifth column: 0 is invisible and 255 is opaque. Class 0, the empty paper, is invisible
blend without --operator
puts the second picture over the first, and the background is visible where the second one is transparent
The other chapters ended with a picture like this. Without a title and a legend, a reader can not be sure what it shows.
3. A font is a layer
The file letters.gpkg.zip holds two fonts. Each letter is one polygon, and it is made from a font file by a small program of this tutorial, tools/font_to_layer.py, which you do not need to run. Look at a few of them:
ogr2ogr-update-overwrite map.gpkg letters.gpkg.zip letterogr2ogr-f CSV /vsistdout/ map.gpkg -sql"select font, char(code) letter, code, advance, round(st_maxy(geom),2) top, round(st_miny(geom),2) bottom, st_numpoints(st_exteriorring(st_geometryn(geom,1))) points from letter where font='title' and code in (unicode('N'),unicode('i'),unicode('g'),unicode('a')) order by 3"
the number of the character in Unicode. SQL has a function for it: unicode('N') is 78
advance
how far to the right the next letter starts. An i needs less space than an N
the shape
the base line is at 0, and the letter size is 1. The g reaches below the base line
To write a word, take its letters one by one, and move each one to the right by the sum of the advances before it. That is the running sum of Chapter 5:
gdal pipeline read map.gpkg ! sql "with recursive c(i) as (select 1 union select i+1 from c where i<length('Nusantara')), p as (select geom g, advance, sum(advance) over (order by i)-advance x from c join letter on font='title' and code=unicode(substr('Nusantara',i,1))) select 14 v, buildmbr(x+0.01,-0.3,x+advance-0.01,0.95) geom from p union all select 16, st_buffer(makeline(makepoint(-1,0),makepoint(9,0)),0.006) union all select 9, st_translate(g,x,0,0) from p" ! edit --crs none ! rasterize --attribute-name v --init 0 --ot Byte --size"5400,1518"--extent"-0.1,-0.4,5.236,1.1" ! edit --nodata none ! color-map --color-map night.txt ! materialize ! resize --size"1800,506"-r average ! write word.png --overwrite
The boxes are the advances, and the orange line is the base line. Read the query from the left: c counts from 1 to the length of the word, substr takes the letter with that number, join finds its polygon, and sum(advance) over (order by i)-advance gives the place where it begins.
Nusantara is an Indonesian name for the whole archipelago (ledger L62). It has nine letters, and the same line can write a title or a footnote.
4. All words in one table
The words of both sheets are in words.csv. Each row has a font, a size, a place, a class for the colour, and the text in two languages:
ogr2ogr-update-overwrite map.gpkg words.csv -oo AUTODETECT_TYPE=YES -nln wordsogr2ogr-update-overwrite map.gpkg key1.csv -oo AUTODETECT_TYPE=YES -nln key1ogr2ogr-update-overwrite map.gpkg key2.csv -oo AUTODETECT_TYPE=YES -nln key2ogr2ogr-update-overwrite map.gpkg way.csv -oo AUTODETECT_TYPE=YES -nln wayogr2ogr-update-overwrite map.gpkg lead.csv -oo AUTODETECT_TYPE=YES -nln leadogr2ogr-f CSV /vsistdout/ map.gpkg -sql"select sheet, font, size, x, y, anchor, v, halo, en, id from words where size>0.7 or halo=1 limit 5"
sheet,font,size,x,y,anchor,v,halo,en,id
1,title,2.1,90,16.35,0,9,0,Between the storms,Di antara badai
1,text,0.8,90,14.95,0,8,0,Tropical cyclones 1980 to 2023 and earthquakes 1990 to 2025 around Indonesia,Siklon tropis 1980 sampai 2023 dan gempa bumi 1990 sampai 2025 di sekitar Indonesia
1,text,0.62,90.8,-2,0,9,1,26 December 2004:,26 Desember 2004:
1,text,0.62,90.8,-2.85,0,9,1,magnitude 9.1,"magnitudo 9,1"
1,text,0.62,106.2,3.4,0,9,1,"December 2001: Vamei,","Desember 2001: Vamei,"
Column
Meaning
size
in degrees, like everything on the sheet. The title is 2.1 degrees high
anchor
0 writes to the right of the place, 1 to the left, and 0.5 to both sides
halo
1 puts a dark shadow behind the letters, for words that lie on the map itself
The other files are also short: key1.csv and key2.csv are the legends, way.csv has the letters for north, south and east, and lead.csv has three short lines from the notes to their places.
Now collect all the texts of the first sheet, in English. The words come from the table, the texts of the legend from the legend, and the coordinates are calculated:
ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive x(d) as (select 95 union select d+10 from x where d<135), y(d) as (select -15 union select d+5 from y where d<10) select font, size, x, y, anchor, v, halo, cast(en as text) txt from words where sheet=1 union all select 'text', 0.6, x+case kind when 'wind' then 1.5 else 0.9 end, y, 0.0, 8, 0, cast(en as text) from key1 union all select 'text', 0.52, d, -18.85, 0.5, 7, 0, d||en from x join way on way='E' union all select 'text', 0.52, 89.6, d-0.18, 1.0, 7, 0, abs(d)||en from y join way on way=case when d<0 then 'S' when d>0 then 'N' else '0' end"-nln labelogr2ogr-f CSV /vsistdout/ map.gpkg -sql"select count(*) texts, sum(length(txt)) characters from label"
texts,characters
37,553
The degree sign is in the file way.csv, and not in the line. A shell can change characters that are not in the English alphabet, and in a file they stay as they are.
The next line is the most important one of this chapter. It does for all texts what section 3 did for one word:
ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive c(n,i) as (select fid,1 from label union all select n,i+1 from c join label on fid=n where i<length(txt)), p as materialized (select l.*, f.geom g, sum(f.advance) over (partition by n order by i)-f.advance pos, anchor*sum(f.advance) over (partition by n) back from c join label l on l.fid=n join letter f on f.font=l.font and f.code=unicode(substr(txt,i,1))), q as materialized (select v, halo, atm_transform(g, atm_create(size, 0.0, 0.0, size, x+(pos-back)*size, y)) geom from p where g is not null) select 23 v, st_buffer(geom,0.13) geom from q where halo=1 union all select v, geom from q"-nln setting -nlt MULTIPOLYGONogr2ogr-f CSV /vsistdout/ map.gpkg -sql"select v, count(*) letters from setting group by 1"
v,letters
7,154
8,155
9,177
23,126
Part
Meaning
c(n,i)
one row for each letter: number i of text n
p
finds the polygon, and sums up the advances. anchor*sum(...) moves the whole text to the left, if it must end or be centred at its place
atm_transform(g, atm_create(a,b,c,d,x,y))
a general function to move and change a shape. Here it makes the letter larger by size and puts it at x and y. Section 9 will also turn it
23 v, st_buffer(geom,0.13)
the halo: the same letters, a little thicker, in a dark and half transparent colour. They come first, so they lie behind
materialized
keeps the result of a step. Without it, SQLite may calculate the same shapes several times
5. Frame, graticule, scale and globe
None of these four needs a new dataset about this area. SQL calculates them:
ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive x(d) as (select 95 union select d+10 from x where d<135), y(d) as (select -15 union select d+5 from y where d<10) select 2 v, st_buffer(makeline(makepoint(d,-18),makepoint(d,14)),0.02) geom from x union all select 2, st_buffer(makeline(makepoint(90,d),makepoint(145,d)),0.02) from y"-nln grid -nlt GEOMETRY -a_srs EPSG:4326ogr2ogr-update-overwrite map.gpkg /vsizip//vsicurl/https://naciscdn.org/naturalearth/110m/physical/ne_110m_land.zip -dialect SQLite -sql"select 15 v, geometry geom from ne_110m_land union all select 16, st_boundary(buildmbr(90,-18,145,14,4326))"-nln globe -nlt GEOMETRY -clipsrc 0.1 -89.9 179.9 89.9 -segmentize 1 -s_srs EPSG:4326 -t_srs"+proj=ortho +lat_0=0 +lon_0=90 +R=6371000"ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive b(i) as (select 0 union select i+1 from b where i<3) select 1 v, st_difference(buildmbr(87,-24,148,20),buildmbr(90,-18,145,14)) geom union all select 8, st_buffer(st_boundary(buildmbr(90,-18,145,14)),0.04) union all select 9-2*mod(i,2), st_buffer(makeline(makepoint(145-(4-i)*250/111.3195,-20),makepoint(145-(3-i)*250/111.3195,-20)),0.08) from b union all select 14, st_buffer(makepoint(142.9,16.45),2.0,64) union all select v, st_translate(case v when 16 then st_buffer(scalecoords(geom,2.0/6371000),0.06) else scalecoords(geom,2.0/6371000) end,142.9,16.45,0) from globe"-nln frame -nlt GEOMETRY -a_srs EPSG:4326
The graticule is a line every 10 degrees of longitude and every 5 of latitude. It is drawn under the data.
The frame is a polygon of the whole sheet with a hole. st_difference removes the area of the map from the sheet, and the rest covers everything that lies outside the map. So it is not necessary to cut the storm tracks.
The scale bar has four pieces of 250 km. One degree on the equator is 111.3195 km long (ledger L59). Further north or south, a degree from west to east is shorter, and for this reason a text at the bar says where it is correct.
The globe is the first map of Chapter 1, an orthographic view. It is calculated in metres, and then scalecoords makes it small, and st_translate moves it into the corner. The frame of the map is shown on it, in the same projection.
6. A legend made by the same rules as the map
A legend that is drawn by hand can differ from the map. This happens when a colour or a limit is changed in one place, and not in the other. So do not draw it by hand. The file key1.csv has samples: a storm with 80 knots, an earthquake at 200 km. The same case and the same formula for the size are used for them and for the data:
ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"select 8 v, st_buffer(makeline(makepoint(x1,y1),makepoint(x2,y2)),0.03) geom from lead union all select case when depth<70 then 10 when depth<300 then 11 else 12 end, st_buffer(makepoint(x+0.35,y+0.2),0.03+0.035*pow(1.7,mag-5)) from key1 where kind='quake' union all select case when kt<64 then 3 when kt<96 then 4 when kt<137 then 5 else 6 end, st_buffer(makeline(makepoint(x,y+0.2),makepoint(x+1.1,y+0.2)),0.05) from key1 where kind='wind'"-nln key -nlt GEOMETRY -a_srs EPSG:4326
The classes of the wind are those of the Saffir-Simpson scale (ledger L42), and the classes of depth are those of the USGS (ledger L31). The three lines from the notes to their places are in this layer, too.
7. The first sheet
Now all the layers are ready. One line puts them in order, from the back to the front, and a second one puts them over the background:
gdal pipeline read map.gpkg ! sql "select v, geom from grid union all select v, geom from mark union all select v, geom from frame union all select v, geom from key union all select v, geom from setting" ! edit --crs EPSG:4326 ! rasterize --attribute-name v --init 0 --ot Byte --size"5400,3816"--extent"87.2,-23.4,147.2,19" ! edit --nodata none ! color-map --color-map night.txt --add-alpha ! materialize ! resize --size"1800,1272"-r average ! write night_ink.tif --overwritegdal raster blend night_ground.png night_ink.tif between_the_storms.png --overwrite
Look before moving on. This map shows two dangers. The storms need distance from the equator (ledger L44), so there is a strip without storms. The earthquakes are in this strip, along the island arcs. Most of Indonesia has only one of the two dangers. In the Philippines, there are both. The notes give examples:
the earthquake of 26 December 2004, the strongest on the sheet;
Vamei, the storm that came within 1.5 degrees of the equator;
the deep earthquakes below the Java Sea, which Chapter 4 showed in a cross-section.
Are the numbers in the notes right? They are written by hand in words.csv, so check them against the data:
ogr2ogr-f CSV /vsistdout/ map.gpkg -sql"select (select max(mag) from quake) strongest, (select round(min(st_miny(geom)),1) from step where name='VAMEI') vamei_degrees_north, (select round(max(depth)) from quake where st_x(geom) between 106 and 115 and st_y(geom) between -7 and -4) java_sea_km"
The same sheet in Indonesian needs another column of the table, id in place of en. The letters are made again, and the rest of the sheet stays:
ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive x(d) as (select 95 union select d+10 from x where d<135), y(d) as (select -15 union select d+5 from y where d<10) select font, size, x, y, anchor, v, halo, cast(id as text) txt from words where sheet=1 union all select 'text', 0.6, x+case kind when 'wind' then 1.5 else 0.9 end, y, 0.0, 8, 0, cast(id as text) from key1 union all select 'text', 0.52, d, -18.85, 0.5, 7, 0, d||id from x join way on way='E' union all select 'text', 0.52, 89.6, d-0.18, 1.0, 7, 0, abs(d)||id from y join way on way=case when d<0 then 'S' when d>0 then 'N' else '0' end"-nln labelogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive c(n,i) as (select fid,1 from label union all select n,i+1 from c join label on fid=n where i<length(txt)), p as materialized (select l.*, f.geom g, sum(f.advance) over (partition by n order by i)-f.advance pos, anchor*sum(f.advance) over (partition by n) back from c join label l on l.fid=n join letter f on f.font=l.font and f.code=unicode(substr(txt,i,1))), q as materialized (select v, halo, atm_transform(g, atm_create(size, 0.0, 0.0, size, x+(pos-back)*size, y)) geom from p where g is not null) select 23 v, st_buffer(geom,0.13) geom from q where halo=1 union all select v, geom from q"-nln setting -nlt MULTIPOLYGONgdal pipeline read map.gpkg ! sql "select v, geom from grid union all select v, geom from mark union all select v, geom from frame union all select v, geom from key union all select v, geom from setting" ! edit --crs EPSG:4326 ! rasterize --attribute-name v --init 0 --ot Byte --size"5400,3816"--extent"87.2,-23.4,147.2,19" ! edit --nodata none ! color-map --color-map night.txt --add-alpha ! materialize ! resize --size"1800,1272"-r average ! write night_ink.tif --overwritegdal raster blend night_ground.png night_ink.tif di_antara_badai.png --overwrite
The title, legend, notes, sources and the letters of the coordinates have changed, and the lines have the same length as before. BT, LU and LS are the Indonesian letters for east, north and south.
8. A second sheet in the style of paper layers
The first sheet is dark and has many data. The second one is light, and has only one dataset. It shows the sea floor as a stack of paper layers. Each layer ends at a line of equal depth, and has a soft shadow on the layer below.
First, two versions of the relief: a detailed one for the coasts, and a very smooth one for the edges of the layers. Then number the layers:
Smoothing with two changes of size.resize to 300 columns with average removes the details, and resize back with cubicspline gives smooth curves. The materialize between the two is necessary. Without it, GDAL joins both steps into one, the small picture is never made, and nothing is smoothed at all (ledger L60).
The calculation counts conditions. A place deeper than 5000 m meets none of the six conditions and is level 0. A place at 2500 m meets three of them, and is level 3. Land is level 7.
The shadow is made in two steps. Move the whole stack a little to the lower right, and blur it. Where the moved stack is higher than the original one, there is a higher layer to the upper left, and its shadow is here:
does not change the picture, only its coordinates: 0.2 degrees further east and south
clip --like level.tif
cuts the moved picture to the cells of the original, so that both can be compared
min(1,max(0,M-L))
0 where nothing is higher, and 1 in the full shadow. The blur gives the values in between
shadow.txt
one dark colour at both ends, and only the fifth column changes, from invisible to half opaque
No hillshade was used here. The impression of depth comes only from the colours and the shadows, as in a paper model.
9. Names along a curve
On many maps, the name of a sea follows the shape of the sea. curve.csv has three points for each name: the beginning, the middle and the end. The first line draws a smooth arc through them:
ogr2ogr-update-overwrite map.gpkg curve.csv -oo AUTODETECT_TYPE=YES -nln curveogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive k(i) as (select 0 union select i+1 from k where i<40) select size, gap, v, en txt, makeline(makepoint((1-t)*(1-2*t)*x1+4*t*(1-t)*x2+t*(2*t-1)*x3, (1-t)*(1-2*t)*y1+4*t*(1-t)*y2+t*(2*t-1)*y3)) geom from curve c, (select i/40.0 t from k order by i) group by c.fid"-nln bow -nlt LINESTRINGogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive c(n,i) as (select fid,1 from bow union all select n,i+1 from c join bow on fid=n where i<length(txt)), p as materialized (select v, size, b.geom bow, st_length(b.geom) len, f.geom g, f.advance a, sum(f.advance+gap) over (partition by n order by i)-gap-f.advance/2 pos, sum(f.advance+gap) over (partition by n)-gap w from c join bow b on b.fid=n join letter f on f.font='text' and f.code=unicode(substr(txt,i,1))), q as materialized (select v, g, a, bow, min(size,len/w) s, 0.5+(pos-w/2)*min(size,len/w)/len m from p where g is not null), r as materialized (select v, g, s, a/2*s h, st_line_interpolate_point(bow,m) o, st_line_interpolate_point(bow,max(0.0,m-0.01)) o1, st_line_interpolate_point(bow,min(1.0,m+0.01)) o2, case v when 22 then 0.0 else 0.2 end k from q), t as materialized (select *, atan2(st_y(o2)-st_y(o1),st_x(o2)-st_x(o1)) t from r) select v, atm_transform(g, atm_create(s*cos(t), s*(k*cos(t)-sin(t)), s*sin(t), s*(k*sin(t)+cos(t)), st_x(o)-h*cos(t), st_y(o)-h*sin(t))) geom from t"-nln bent -nlt MULTIPOLYGON
The last line is the longest of this tutorial, and it has only four new parts:
Part
Meaning
st_line_interpolate_point(bow,m)
the place of a letter: the fraction m of the arc. Chapter 6 used it to find the position of a bus
atan2(...)
the direction of the arc at this place, from a point just before it to a point just after it
min(size,len/w)
a name that is longer than its arc becomes smaller, until it fits. This is important when the language changes
the six numbers in atm_create
size, rotation and slant in one step. k is the slant: 0.2 for water, and 0 for land. Slanted names for water are a common custom on maps (ledger L62)
Between the letters there is an extra gap, so that a name can be spread over its sea.
The choice of names. The waters north of the Natuna islands have the name North Natuna Sea here, in Indonesian Laut Natuna Utara, as on the official map of Indonesia since 2017 (ledger L65). Other countries use other names. Everybody who makes a map has to choose, and with a table, the choice is easy to find and to change.
10. The second sheet
The frame, the graticule, the scale and the globe are already in the file. They only need other colours, and those are in ink.txt. The legend is a row of boxes, with the same numbers as the layers of paper:
ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"select 30+level v, buildmbr(x,-20.1,x+3,-19.5) geom from key2 union all select 8, st_buffer(st_boundary(buildmbr(90,-20.1,114,-19.5)),0.02)"-nln key -nlt GEOMETRY -a_srs EPSG:4326ogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive x(d) as (select 95 union select d+10 from x where d<135), y(d) as (select -15 union select d+5 from y where d<10) select font, size, x, y, anchor, v, halo, cast(en as text) txt from words where sheet=2 union all select 'text', 0.5, x, -20.75, 0.0, 8, 0, cast(en as text) from key2 union all select 'text', 0.52, d, -18.85, 0.5, 7, 0, d||en from x join way on way='E' union all select 'text', 0.52, 89.6, d-0.18, 1.0, 7, 0, abs(d)||en from y join way on way=case when d<0 then 'S' when d>0 then 'N' else '0' end"-nln labelogr2ogr-update-overwrite map.gpkg map.gpkg -sql"with recursive c(n,i) as (select fid,1 from label union all select n,i+1 from c join label on fid=n where i<length(txt)), p as materialized (select l.*, f.geom g, sum(f.advance) over (partition by n order by i)-f.advance pos, anchor*sum(f.advance) over (partition by n) back from c join label l on l.fid=n join letter f on f.font=l.font and f.code=unicode(substr(txt,i,1))), q as materialized (select v, halo, atm_transform(g, atm_create(size, 0.0, 0.0, size, x+(pos-back)*size, y)) geom from p where g is not null) select 23 v, st_buffer(geom,0.13) geom from q where halo=1 union all select v, geom from q"-nln setting -nlt MULTIPOLYGONgdal pipeline read map.gpkg ! sql "select v, geom from grid union all select v, geom from bent union all select v, geom from frame union all select v, geom from key union all select v, geom from setting" ! edit --crs EPSG:4326 ! rasterize --attribute-name v --init 0 --ot Byte --size"5400,3816"--extent"87.2,-23.4,147.2,19" ! edit --nodata none ! color-map --color-map ink.txt --add-alpha ! materialize ! resize --size"1800,1272"-r average ! write ink.tif --overwritegdal raster blend paper.png ink.tif seas_between_islands.png --overwrite
Look before moving on. The pale area between Sumatra, Java and Borneo is less than 200 m deep. East of Borneo the sea floor becomes deeper in many steps, and beyond the next islands lies the Banda Sea, with level 0 in its eastern corner. South of Sumatra and Java, the steps are close together and follow the islands down to the trench. Chapter 4 showed the same place in a cross-section.
The edges of the layers are smoothed. A ridge or a hole that is narrower than about 20 km can be missing. For navigation, use a nautical chart.
11. A file for printing
The pictures above have 1800 columns, for the screen. For printing, 300 dots for each inch are a common choice. The paper background already exists with 5400 columns. Make the letters and lines for that size, too, and write the resolution into the file:
gdal pipeline read map.gpkg ! sql "select v, geom from grid union all select v, geom from bent union all select v, geom from frame union all select v, geom from key union all select v, geom from setting" ! edit --crs EPSG:4326 ! rasterize --attribute-name v --init 0 --ot Byte --size"10800,7632"--extent"87.2,-23.4,147.2,19" ! edit --nodata none ! color-map --color-map ink.txt --add-alpha ! materialize ! resize --size"5400,3816"-r average ! write ink_large.tif --overwritegdal raster blend paper.tif ink_large.tif large.tif --overwritegdal_translate-q-co COMPRESS=DEFLATE -co PREDICTOR=2 -mo TIFFTAG_XRESOLUTION=327 -mo TIFFTAG_YRESOLUTION=327 -mo TIFFTAG_RESOLUTIONUNIT=2 large.tif seas_a3.tif
The long side of A3 paper is 420 mm, or 16.5 inches. 5400 columns on 16.5 inches are 327 dots for each inch, and RESOLUTIONUNIT=2 means inches. The sheet was planned with sides in the proportion of A paper, so it fits with a difference of less than a millimetre.
Here is a part of the large file, in its full size:
The second sheet in Indonesian. Write id txt in place of en txt in the line that makes bow, and make the words of sheet 2 again with id, as in section 7. All names are in the files.
Your own title. Change the first row of words.csv, and run the lines from words.csv on. Use only letters that the fonts have.
Your own seas. Choose another frame, such as the Mediterranean. The numbers 90, -18, 145 and 14 are in section 5, and the names in curve.csv.
A scale bar for 45 degrees north. A degree of longitude is there about 78.8 km long. Which number in section 5 changes?
A third sheet. Put the buses of Chapter 6 or the stars of Chapter 3 into this frame.
Checkpoint
The cell below stops with an error if the results are not what this chapter expects.
import csvimport iofrom osgeo import gdalgdal.UseExceptions()def rows(sql, dataset="map.gpkg"):"""The rows of a query, as dictionaries."""returnlist(csv.DictReader(io.StringIO(oneline.run(f'ogr2ogr -f CSV /vsistdout/ {dataset} -sql "{sql}"'))))# No letter may be lost: every character that is not a space must have become a polygonwant =int(rows("select sum(length(replace(txt,' ',''))) n from label")[0]["n"])have =int(rows("select count(*) n from setting where v!=23")[0]["n"])assert want == have, f"{want} characters, but {have} letters on the sheet"want =int(rows("select sum(length(replace(txt,' ',''))) n from bow")[0]["n"])have =int(rows("select count(*) n from bent")[0]["n"])assert want == have, f"{want} characters in curved names, but {have} letters on the sheet"big = gdal.Open("seas_a3.tif")inches = big.RasterXSize /float(big.GetMetadataItem("TIFFTAG_XRESOLUTION"))assertabs(inches *25.4-420) <1, "the large file should be 420 mm wide"pictures = ("night_ground.png", "data.png", "word.png", "between_the_storms.png", "di_antara_badai.png", "paper.png", "seas_between_islands.png", "detail.png")assertall((WORK / f).stat().st_size >10_000for f in pictures)print(f"checkpoint passed: no letter lost; the large file is {inches *25.4:.0f} mm wide and {big.RasterYSize / big.RasterXSize * inches *25.4:.0f} mm high")print(f"time since the first cell: {time.perf_counter() - T0:.0f} s")
checkpoint passed: no letter lost; the large file is 419 mm wide and 296 mm high
time since the first cell: 235 s
Troubleshooting
Symptom
Cause
Fix
A letter is missing
The font has no such letter
Check the layer letter, or use another letter
Strange characters in place of a degree sign or an accent
The shell has changed the text
Keep it in a CSV file, saved as UTF-8
The edges of the paper layers are rough, or nothing looks smooth
The materialize between the two resize steps is missing
Another program, or a line that was stopped before its end, has the file open
Close it, and run the line again
The first line stops with an error
The connection to the server was lost
The copies in the chapter’s folder, at the end of section 1
The end of the tutorial
The tutorial began with a simple question: which place is on the other side of the Earth? These are the questions of its parts, and the main results:
Question
Main result
Where?
a place is two numbers, and other pairs of numbers can be drawn as places, too: a star, a depth, a minute
When, and where next?
a table of points becomes a line when each row is compared with the row before it
Whose, and how far?
two neighbours share one border, and a distance can be calculated for every cell of a surface
How high, and how much?
a raster can be read as a table, and slope and shading can be calculated from any numbers, not only from heights
What has changed?
two images of the same place show a change, but clouds and seasons must be removed first
What should be done?
a map can support a decision if the question is clear and the uncertainty is known
How can it be repeated?
a line of text can be saved, sent, read and run again
The tutorial ends with two map sheets, made with the same two tools, gdal and ogr2ogr, in a shell. The lines have become longer, but the principle is the same as in Chapter 0: every step reads a dataset and writes a dataset. Everything that can be put into a table can be drawn on a map, even a title.
Now use the lines for your own questions and for places that you know well. Begin with one command and one file.
If you make a map with it, we would like to see it: osgeo.id.
Data: ETOPO 2022 of NOAA’s National Centers for Environmental Information; IBTrACS, doi:10.25921/82ty-9e16; the ANSS Comprehensive Earthquake Catalog of the US Geological Survey; and Natural Earth, in the public domain. All read on 3 December 2026. The letters are made from the fonts Miriam Libre by Michal Sahar and another family by Eduardo Tunni, both under the SIL Open Font License (ledger L61). The licence texts are next to the file.