A pretty handy feature that is not well documented in TileMill, is the possibility to call the export command from the command line. This is useful, for example, when you have to export a project repeatedly with almost the same settings.
In our case we have a dataset with eleven years worth of point data. For our final map, we want to visualize each of these years in a separate layer. Since a TileMill export only contains one layer, we need to export each of these years one by one. To facilitate this process, we create a simple bash script that loops over the years and calls the export process.
To start, we add a selector to the Carto stylesheet that limits the data to a particular year.
Our bash script loops over these years and basically does the following:
- changes the selector in the style.mss;
- changes the project’s metadata in project.mml;
- export the project to .mbtiles format; and
- upload it to MapBox.
First we define the range that the script should loop over.
sed we search and replace the selector in the style.mss stylesheet for the correct year.
sed again, we update the project.mml and change the project name and description so they contain a reference to the year. This helps greatly when browsing the layers on MapBox.
With the right selector set in the style.mss, we are ready to export the layer. The actual export command is pretty self-explanatory. The basic syntax on Ubuntu is:
/usr/share/tilemill/index.js export [name of project] [destination of export], where the export path is relative to the home folder.
The export command comes with a couple of other options that we didn’t include in our example. For a full list you can run
/usr/share/tilemill/index.js export --help.
Once exported, we also upload the layer to MapBox hosting with our script. The command to invoke is basically the same as the one used for the export, except for the
We also add the year to the name of the project to prevent issues when uploading the tiles. If we use the same name for every layer, MapBox simply overwrites the one that was previously uploaded.
Note that the –syncAccount and –syncAccessToken options are left empty in this example, but you have to provide them to be able to upload the layers.
This is all you need to export a TileMill project with a bash script. To see a more complete example of this script, you can have a look at the following Gist: https://gist.github.com/olafveerman/5391804.