Selfhost OverPass API

I don't know how many Overpass Api requests I will need for my current hobby project idea. So I decided to try selfhosting Overpass.

There is a ready to use AWS AMI with the planet inside. But I don't want to use AWS for private projects so I will try to run this on a Hetzner VPS instance. The server I used was a CX32 with 4 cores, 8 GB memory and 80GB disk. As operating system I used Debian 12, but any Linux should work here.

The 8GB of memory are not enough so we create a swapfile with additonal 8GB of memory:

fallocate -l 8G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show

Then I installed Docker Engine.

I converted the Docker command from the Overpass API readme into a Docker Compose file. Here I replaced Monaco with Baden-W├╝rttemberg and used the more current pbf version and not the .osm.bz2 one. The initial processing for converting the 4GB pbf into bz2 takes a while, but this should be faster than processing the minutes for 200 days (If that is even possible/happening). Also the whole importing and updating before the first start takes quite a bit of time and this is also the memory consuming part.

The docker-compose.yml I used:

---
services:
  overpass:
    image: wiktorn/overpass-api
    container_name: overpass
    environment:
      OVERPASS_META: no
      OVERPASS_MODE: init
      OVERPASS_PLANET_URL: https://download.geofabrik.de/europe/germany/baden-wuerttemberg-latest.osm.pbf
      OVERPASS_DIFF_URL: https://download.geofabrik.de/europe/germany/baden-wuerttemberg-updates/
      OVERPASS_RULES_LOAD: 10
      OVERPASS_COMPRESSION: gz
      OVERPASS_UPDATE_SLEEP: 3600
      OVERPASS_PLANET_PREPROCESS: 'mv /db/planet.osm.bz2 /db/planet.osm.pbf && osmium cat -o /db/planet.osm.bz2 /db/planet.osm.pbf && rm /db/planet.osm.pbf'
    volumes:
      - ./overpass_db/:/db
    ports:
      - "80:80"

And then run with docker compose up. This will take quite some time and may swap a bit, depending on the size of your planet file and the minutes. When the processing is done, the container will exit and when you restart it again the same way it will be live and happy to serve.

Now the API is running and we use the same query as in my overpass post from last year. Only this time we use our own overpass instance.

import json
# pip install OSMPythonTools
from OSMPythonTools import overpass

ip = "IP_ADDRESS_OF_YOUR_SERVER"
api = overpass.Overpass(endpoint=f"http://{ip}/api/")
# bbox for Stuttgart Mitte (roughly)
bbox_coords = [48.76605, 9.1657, 48.78508, 9.18995]
query = overpass.overpassQueryBuilder(
    bbox=bbox_coords,
    elementType=["node", "way"],
    # all restaurants that have a positive diet:vegan tag
    selector=['"amenity"="restaurant"', '"diet:vegan"~"yes|only"'],
    out="body",
)
result = api.query(query, timeout=60)

for index, item in enumerate(result.toJSON()["elements"]):
    print(index, item["tags"])

Last time 14 vegan restaurants were found in Stuttgart Mitte. This increased by one to 15 restaurants.