# openstreetmap-tile-server This container allows you to easily set up an OpenStreetMap PNG tile server given a `.osm.pbf` file. It is based on the [latest Ubuntu 18.04 LTS guide](https://switch2osm.org/serving-tiles/manually-building-a-tile-server-18-04-lts/) from [switch2osm.org](https://switch2osm.org/) and therefore uses the default OpenStreetMap style. ## Set up PostgreSQL * Create a database * Create extensions `postgis` and `hstore` within it * Create a user and grant him access to the created database ``` CREATE DATABASE osm; CREATE USER renderd WITH PASSWORD ''; GRANT ALL PRIVILEGES ON DATABASE osm TO renderd; CREATE EXTENSION postgis; CREATE EXTENSION hstore; ``` ## Running the initial import ``` podman run \ -it \ -e PGPASSWORD= \ -e PGHOST= \ -e PGUSER=renderd \ -e PGPORT=5432 \ -e PGDB=osm \ -e DOWNLOAD_PBF=https://download.geofabrik.de/russia-latest.osm.pbf \ [-e DOWNLOAD_POLY=https://download.geofabrik.de/russia.poly \] -e UPDATES=enabled \ -v /run/postgresql:/run/postgresql \ -v /home/osm/download:/data \ -v /home/osm/mod_tile:/var/lib/mod_tile \ --rm \ osm \ import ``` ## Running the server Run the server like this: ``` podman run \ -d \ -e PGPASSWORD= \ -e PGHOST= \ -e PGUSER=renderd \ -e PGPORT=5432 \ -e PGDB=osm \ -e UPDATES=enabled \ -v /run/postgresql:/run/postgresql \ -v /home/osm/download:/data \ -v /home/osm/mod_tile:/var/lib/mod_tile \ -v /home/osm/logs:/var/log/tiles \ -v /dev/log:/dev/log \ -p 8080:80 \ osm \ run ``` Your tiles will now be available at `http://localhost:8080/osm/{z}/{x}/{y}.png`. The demo map in `leaflet-demo.html` will then be available on `http://localhost:8080`. Note that it will initially take quite a bit of time to render the larger tiles for the first time. `UPDATES=enabled` will enable a background process that automatically downloads changes from the OpenStreetMap server, filters them for the relevant region polygon you specified, updates the database and finally marks the affected tiles for rerendering. ### Cross-origin resource sharing To enable the `Access-Control-Allow-Origin` header to be able to retrieve tiles from other domains, simply set the `ALLOW_CORS` variable to `enabled`: ``` podman run \ -p 8080:80 \ -v openstreetmap-data:/var/lib/postgresql/12/main \ -e ALLOW_CORS=enabled \ -d osm \ run ``` ## Performance tuning and tweaking Details for update procedure and invoked scripts can be found here [link](https://ircama.github.io/osm-carto-tutorials/updating-data/). ### POSTGRESQL https://pgtune.leopard.in.ua ### THREADS The import and tile serving processes use 4 threads by default, but this number can be changed by setting the `THREADS` environment variable. For example: ``` podman run \ -p 8080:80 \ -e THREADS=24 \ -v openstreetmap-data:/var/lib/postgresql/12/main \ -d osm \ run ``` ### CACHE This image uses 4096 MB RAM cache by default for osm2pgsql, but this number can be changed by option -C. For example: ``` podman run \ -p 8080:80 \ -e "OSM2PGSQL_EXTRA_ARGS=-C 8192" \ -v openstreetmap-data:/var/lib/postgresql/12/main \ -d osm \ run ``` ### Flat nodes If you are planning to import the entire planet or you are running into memory errors then you may want to enable the `--flat-nodes` option for osm2pgsql. You can then use it during the import process as follows: ``` podman run \ -v /absolute/path/to/luxembourg.osm.pbf:/data/data.osm.pbf \ -v openstreetmap-nodes:/nodes \ -v openstreetmap-data:/var/lib/postgresql/12/main \ -e "OSM2PGSQL_EXTRA_ARGS=--flat-nodes /nodes/flat_nodes.bin" \ osm \ import ``` >Note that if you use a folder other than `/nodes` then you must make sure that you manually set the owner to `renderer`! ### Benchmarks You can find an example of the import performance to expect with this image on the [OpenStreetMap wiki](https://wiki.openstreetmap.org/wiki/Osm2pgsql/benchmarks#debian_9_.2F_openstreetmap-tile-server). ## Troubleshooting ### ERROR: could not resize shared memory segment / No space left on device If you encounter such entries in the log, it will mean that the default shared memory limit (64 MB) is too low for the container and it should be raised: ``` renderd[121]: ERROR: failed to render TILE osm_retina 2 0-3 0-3 renderd[121]: reason: Postgis Plugin: ERROR: could not resize shared memory segment "/PostgreSQL.790133961" to 12615680 bytes: ### No space left on device ``` To raise it use `--shm-size` parameter. For example: ``` podman run \ -p 8080:80 \ -v openstreetmap-data:/var/lib/postgresql/12/main \ --shm-size="192m" \ -d osm \ run ``` For too high values you may notice excessive CPU load and memory usage. It might be that you will have to experimentally find the best values for yourself. ### The import process unexpectedly exits You may be running into problems with memory usage during the import. Have a look at the "Flat nodes" section in this README. ## License ``` Copyright 2019 Alexander Overvoorde Copyright 2021 Vitaliy Filippov Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ```