Self-hosting maps on my laptop
December 14, 2021 [Programming][See also: Providing MapLibre-compatible style JSON from openstreetmap-tile-server]
As part of my research for working on location sharing for Element Web, the Matrix-based instant messenger, I have been learning about tile servers.
I managed to get OSM tile server stack working on my laptop:
Video of a map zooming in and out with network requests fetching lots of PNG files.
Here are a couple useful pages I read during my research:
- Switch2OSM - The Basics - explanation of some of the concepts in a way I could understand.
- Switch2OSM - serving tiles using a Docker container - good explanation of how to do what I write up below.
- openstreetmap-tile-server README - much more detail than this blog post.
- Location Guard Firefox Plugin - allows setting a hard-coded location instead of sharing your real location.
- Implementing Vector Tile Support in Libshumate - explanation of the ideas from James Westman
Today I managed to run a real tile server on my laptop, using data downloaded from OpenStreetMap in a way that I think complies with their terms of use.
To run these commands you will need Docker, and hopefully nothing much else.
Download the data
I downloaded the UK data like this:
wget 'https://download.geofabrik.de/europe/great-britain-latest.osm.pbf'
You can find downloads for other regions at download.geofabrik.de/
Import it
Then I ran an import, which converts the PBF data into tiles that can be shown in a UI:
docker volume create openstreetmap-data docker volume create openstreetmap-rendered-tiles docker run \ -v $PWD/great-britain-latest.osm.pbf:/data.osm.pbf \ -v openstreetmap-data:/var/lib/postgresql/12/main \ -v openstreetmap-rendered-tiles:/var/lib/mod_tile \ -e THREADS=24 \ overv/openstreetmap-tile-server:1.3.10 \ import
(Change "great-britain" to match what you downloaded.)
On my quite powerful laptop this took 39 minutes to run.
Run the tile server
Finally, I launched the server:
(Make sure you've done the "Import it" step first.)
docker run \ -p 8080:80 \ -v openstreetmap-data:/var/lib/postgresql/12/main \ -v openstreetmap-rendered-tiles:/var/lib/mod_tile \ -e THREADS=24 \ -e ALLOW_CORS=enabled \ -d overv/openstreetmap-tile-server:1.3.10 \ run
This should launch the docker container in the background, which you can check with docker ps.
Test it
You can now grab a single file by going to http://127.0.0.1:8080/tile/0/0/0.png, or interact with the map properly at http://127.0.0.1:8080.
It was quite unresponsive at first, but once it had cached the tiles I was looking at, it was very smooth.