Generate mbtiles – node.js & mapnik
Usually to generate mbtiles we do require some 3rd party software, however there is one method which at the end does require only one command to be executed and we can create mbtiles from any compatible services.
Example:
Let’s say we need a map for Lake District UK and we would like to use OpenTopoMap as our base-map.
- We need to identify what is the URL for the actual tiles (you can use Google Chrome Developer Tools for that) – In our case the URL to a single tile looks like:
- We need the extent of the map we need to cache – the good tool for that is bboxfinder:
http://bboxfinder.com/#54.355356,-3.395462,54.678992,-2.619553
- Just copy the extent of the box – in our case it will be:-3.395462,54.355356,-2.619553,54.678992
- Remove the comas from it so it looks like:-3.395462 54.355356 -2.619553 54.678992
- Now you need to build the node.js command this will look like:
tl copy -z 0 -Z 15 -b “-3.395462 54.355356 -2.619553 54.678992” “http://api.mapbox.com/v4/mapbox.outdoors/{z}/{x}/{y}.png” mbtiles://./lake-district.mbtiles
- When you execute the command the tiles will be downloaded and lake-district.mbtiles file will be saved in C:\Users\{your_user_name}\ folder.
Easy and effortless, but how to get there?
- Download and install node.js from https://nodejs.org/download/ – make sure it’s 32Bit version otherwise mapnik will not work.
- Install Python if you do not have it already.
- Install mapnik executing command in the node.js console:
npm install -g tl mbtiles tilelive-http
- You should get the screen like bellow after successful install:
- You can try to create test mbtiles file for Tatra mountains:
tl copy -z 0 -Z 15 -b “19.481506 49.050920 20.407791 49.319751” “http://a.tile.openstreetmap.org/{z}/{x}/{y}.png” mbtiles://./tatra.mbtiles
As you can see there is a bit of config work required but ones it’s done it’s done and the generation of the tiles is very quick.