Making your own vector tiles with Tippecanoe
Want to build your own vector tiles? tippecanoe is the droid you’re looking for. There are two steps to this process:
- Get your layers in GeoJSON.
- Run tippecanoe.
Optional steps include proclaiming bullshit loudly at the idea it really did what it said it did that quickly, and setting your other tile making tools on fire.
Get your layers in GeoJSON
gdal. Whatever you were planning on using is likely using gdal anyway. If your platform of choice proves challenging to get GDAL on, run GDAL via Docker. There are lots of docker images for GDAL, but here’s mine:
1 | docker run -v c:/workspace:/data -i fuzzytolerance/gdal ogr2ogr -f "GeoJSON" buildings.geojson -sql "SELECT 3 as height, the_geom from buildings" PG:"host=hostname user=login dbname=database password=password port=5432" -s_srs EPSG:2264 -t_srs EPSG:4326 -progress |
Getting your data from PostGIS is great, because you can use a SQL call tailor your data exactly as you need it. But GDAL works with almost anything. Don’t carry extra fields you don’t need, and if you don’t need any, just the geometry is fine. And your field names are stupid. Best to address that before you start pounding out tiles.
Run tippecanoe
tippecanoe
will run on however many GeoJSON files you give it, and the command line gives you the option of specifying vector tile layer names for each geojson file. That’s about it though for file-specific configuration. The rest of tippecanoe’s arguments operate on the whole conversion. That usually isn’t desirable when working with more than one layer. In particular, you might want your rivers to start tiling at zoom level 6, but your buildings you might not want until zoom level 13.
Fortunately you can write some data to your GeoJSON files to address that by layer. I use good old sed
for this. Here I’m letting tippecanoe
know that the buildings file should only start tiling at zoom level 13.
1 | sed -i 's/"type": "Feature",/"type": "Feature", "tippecanoe" : { "minzoom": 13 },/g' buildings.geojson |
The proper way to do this would be using a tool that treats the JSON as JSON, but sed
will rip through a 200MB text file in a second, and I have things to do.
Once that’s done, you’re ready to make vector tiles. Tippecanoe is compiled C, and you can download and compile it yourself. If you just threw up in your mouth at the word compile, I have a Docker container for tippecanoe
you can use, with credit to Jakes Skeates.
Here I’m doing our parcels and buildings, and via the -L
option, the vector tile layer names will be parcels
and buildings
.
1 | docker run -it -v $(pwd):/data fuzzytolerance/tippecanoe tippecanoe -L parcels:parcels.geojson -L buildings:buildings.geojson -o funtiles.mbtiles |
For Windows users, you might need to change the path to your GeoJSON files from $(pwd)
to C:/where/ever/the/hell
.
On my desktop machine, it eats our parcels and buildings (758,167 features) and spits out vector tiles in 54 seconds. The MBTiles file itself is 23MB. Toss it on your tile server of choice, Bob’s your uncle.
Here I’m getting a couple of layers I like to stick on top of OpenMapTiles, but if you wanted to build an entire set of base tiles with your own data, you’d do it exactly the same way. And since it’s all command-line driven, it’s cron
-tastic.