Some time back, I wrote about self-hosting OpenStreetMaps widgets. However, since then, some of the techniques I discussed are no longer valid or necessary. Hence, I've put together this update.
Why self-host map widgets?
Why host an interactive map yourself when it's easy to embed one using Mapbox or Google Maps?
I prefer to have the map data hosted on my own server, as it means I don't have to worry about third-party providers. This makes it easier for me to customize or migrate my website.
Additionally, I don't have to worry about the privacy or GDPR compliance of a third-party provider.
Recently, I stumbled upon Protomaps, a new file format designed for efficiently storing map data. It makes the process of publishing and self-hosting OpenStreetMaps map widgets much simpler.
To self-host a map using Protomaps, you can follow these steps:
- determine the desired map outline
- download the corresponding map material
- integrate and display the map material on a website
- (optional) customize the map style
Step 1: Determining the map outline
Let's start by creating a new folder for our project, where we'll keep all the important data throughout this tutorial.
First, we will define the outline of our map and save it as a file. We will then use this outline in the next step to download the appropriate map data.
It's essential to consider the size of your outline, as it determines the amount of map data to be downloaded and the storage space required. Starting with a smaller outline, around 10 x 10 km, is advisable.
We'll describe this outline using the GeoJSON format and save it as a .geojson
file in our project folder.
The graphical editor geojson.io is free and allows us to create and edit a GeoJSON file:
Under Save
> GeoJSON
we can download the file. We then save it to our project folder and rename it to bounds.geojson
.
At this point, our project folder should look like this:
Alternatively, it is of course also possible to create the GeoJSON file manually or with other editors.
Step 2: Downloading the map data
Now that we've saved the map outline to a file, we're ready to download the map data.
Protomaps provides OpenStreetMaps map data in their file format. However, the datasets available on Protomaps' builds page cover the entire world and are quite large, spanning many gigabytes. For most map applications, a smaller section of the map is sufficient.
To download the map section defined in Step 1, we'll use a command-line tool called go-pmtiles
.
You can download go-pmtiles
from its GitHub Releases page. On the page, you'll find various files listed. Choose the appropriate zip file for your operating system and unpack it.
Inside the downloaded zip archive, we'll find an executable file named pmtiles
. We place this file in our project folder:
Once we have successfully downloaded the go-pmtiles
tool, we can proceed to download the map data.
On the Protomaps Builds page we'll find a list with the latest map data. We select the latest version and download the data for our map section.
To do this, we first copy the link address of the download
link (in my case, for example, https://build.protomaps.com/20231002.pmtiles).
We then use this link address in our terminal command to download the map data:
After a short time, the data should be downloaded...
Our project folder should then look like this:
This completes the download of the map data and we can now proceed to display the map data on a website.
Step 3: Integrate and display map data on the website
We will use the JavaScript library Maplibre GL JS to integrate the map.
We download the following JavaScript and CSS files and save them in our project folder:
- https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.js under
assets/js/maplibre-gl.js
- https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.css under
assets/css/maplibre-gl.js
- https://unpkg.com/pmtiles@2.11.0/dist/index.js under
assets/js/pmtiles.js
We also move the file mapdata.pmtiles
to the assets
folder.
The contents of the project folder should now look something like this:
Next, we create a simple_map.html
file with the following content in our project folder:
If we upload the project folder to a web server (or start a web server in this folder) and then open the simple_map.html
file in the browser, our map will now be displayed. (Important: the web server must support HTTP Range Requests).
Step 4: Customize map display
We can define and enhance the visual map display using style rules. These rules allow us to customize elements such as building colors or street markings. More detailed explanations of these style rules can be found on the Maplibre page.
Protomaps kindly provides some ready-made map styles. These can be tried out, adapted and copied on the test page. We save the map style in our project folder as a file under assets/map_style.json
.
In addition to the map data, fonts in .pbf
format are also required for the text display. These can also be downloaded from the basemaps-assets Repository of Protomaps on GitHub (or simply click this link). We also add the included fonts
folder to our project folder, under /assets
.
Alternatively, we could use the online tool Maplibre Font Maker to convert our font files (.ttf
, .otf
) into the .pbf
format.
Next, we create a new file called styled_map.html
in our project folder with the following content:
When we open this file in the browser, it will look something like this:
With our self-hosted map as a basis, all paths are open to us: On the website of MapLibre there are various examples of what you can do with your self-hosted map.
A simple example: The visualization of a GPX file
Leave a comment
Replied on your own website? Send a Webmention!