Last post ended with downloading OpenStreetMap data. This post will leave the data aside and switch to downloading and building a style. There’s lots of styles available, but we’re going to use OpenStreetMap Carto, the current default on OpenStreetMap.org. Also, because we need software not packaged in Debian, that needs to be installed.
For the script, we’re going to assume that the carto
binary is in the PATH. Unfortunately, this requires installation, which requires npm, which itself needs to be installed.
Given nodejs and npm is a huge headache of versions, the easiest route I’ve found is to install nvm, then install nodejs 6 with nvm install 6
. CartoCSS is then installed with npm install -g carto
.
The shell script starts off with some variables from last time.
1 2 3 |
|
OpenStreetMap Carto is hosted on Github, which offers the ability to download a project as a zip file. This is the logical way to get it, but isn’t usable from a script because the internal structure of the zip file isn’t easily predicted. Instead, we’ll clone it with git, only getting the specific revision needed.
1 2 3 4 5 |
|
Setting advice.detachedHead=false
for this command avoids a warning about a detached HEAD, which is expected.
OpenStreetMap Carto sets the database name to be “gis”. There are various ways to override this for development, but in this case we want to override it for the generated XML file. Fortunately, the database name only appears once, as dbname: "gis"
in project.mml. One way to override it would be to remove the line and rely on the libpq environment variables like PGDATABASE
. Another is replacing “gis” with a different name. It’s not clear which is better, but I decided to go with replacing the name, using a patch which git applies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
With project.mml patched, it’s easy to generate the Mapnik XML, because CartoCSS was installed earlier.
1
|
|
Lastly, OpenStreetMap Carto needs some data files like coastlines. It comes with a script to download them, so we run it.
1
|
|
Taking all of this and re-arranging it as, we end up with the following script.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|