osm-tile-server/run.sh

144 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "usage: <import|run>"
echo "commands:"
echo " import: Set up the database and import /data/data.osm.pbf"
echo " run: Runs Apache and renderd to serve tiles at /tile/{z}/{x}/{y}.png"
echo "environment variables:"
echo " THREADS: defines number of threads used for importing / tile rendering"
echo " UPDATES: consecutive updates (enabled/disabled)"
exit 1
fi
set -x
if [ "$1" = "import" ]; then
# Download Luxembourg as sample if no data is provided
if [ ! -f /data/data.osm.pbf ]; then
if [ -z "$DOWNLOAD_PBF" ]; then
echo "WARNING: No import file at /data/data.osm.pbf, so importing Luxembourg as example..."
DOWNLOAD_PBF="https://download.geofabrik.de/europe/luxembourg-latest.osm.pbf"
DOWNLOAD_POLY="https://download.geofabrik.de/europe/luxembourg.poly"
fi
echo "INFO: Download PBF file: $DOWNLOAD_PBF"
wget $WGET_ARGS "$DOWNLOAD_PBF" -O /data/data.osm.pbf
if [ -n "$DOWNLOAD_POLY" ]; then
echo "INFO: Download PBF-POLY file: $DOWNLOAD_POLY"
wget $WGET_ARGS "$DOWNLOAD_POLY" -O /data/data.poly
fi
fi
mkdir -p /var/lib/mod_tile
chown -R renderer /data /var/lib/mod_tile
if [ "$UPDATES" = "enabled" ]; then
# determine and set osmosis_replication_timestamp (for consecutive updates)
osmium fileinfo /data/data.osm.pbf > /var/lib/mod_tile/data.osm.pbf.info
osmium fileinfo /data/data.osm.pbf | grep 'osmosis_replication_timestamp=' | cut -b35-44 > /var/lib/mod_tile/replication_timestamp.txt
REPLICATION_TIMESTAMP=$(cat /var/lib/mod_tile/replication_timestamp.txt)
# initial setup of osmosis workspace (for consecutive updates)
sudo -u renderer openstreetmap-tiles-update-expire $REPLICATION_TIMESTAMP
fi
# copy polygon file if available
if [ -f /data/data.poly ]; then
sudo -u renderer cp /data/data.poly /var/lib/mod_tile/data.poly
fi
# Import data
if [ -z "$OSM2PGSQL_EXTRA_ARGS" ]; then
OSM2PGSQL_EXTRA_ARGS="-C 4096"
fi
sudo -E -u renderer osm2pgsql -H "$PGHOST" -d "$PGDB" -U "$PGUSER" -P "${PGPORT:-5432}" \
--create --slim -G --hstore --tag-transform-script /home/renderer/src/openstreetmap-carto/openstreetmap-carto.lua \
--number-processes ${THREADS:-4} -S /home/renderer/src/openstreetmap-carto/openstreetmap-carto.style /data/data.osm.pbf ${OSM2PGSQL_EXTRA_ARGS}
# Create indexes
sudo -E -u postgres psql -h "$PGHOST" -d "$PGDB" -U "$PGUSER" -p "${PGPORT:-5432}" -f /home/renderer/src/openstreetmap-carto/indexes.sql
# Import external data
chown -R renderer: /home/renderer/src
sudo -E -u renderer python3 /home/renderer/src/openstreetmap-carto/scripts/get-external-data.py \
-H "$PGHOST" -d "$PGDB" -U "$PGUSER" -p "${PGPORT:-5432}" \
-c /home/renderer/src/openstreetmap-carto/external-data.yml -D /home/renderer/src/openstreetmap-carto/data
# Register that data has changed for mod_tile caching purposes
touch /var/lib/mod_tile/planet-import-complete
exit 0
fi
if [ "$1" = "run" ]; then
# Clean /tmp
rm -rf /tmp/*
# Configure Apache CORS
if [ "$ALLOW_CORS" == "enabled" ] || [ "$ALLOW_CORS" == "1" ]; then
echo "export APACHE_ARGUMENTS='-D ALLOW_CORS'" >> /etc/apache2/envvars
fi
echo "PGHOST='$PGHOST'" > /home/renderer/env
echo "PGPORT='$PGPORT'" >> /home/renderer/env
echo "PGUSER='$PGUSER'" >> /home/renderer/env
echo "PGDB='$PGDB'" >> /home/renderer/env
echo "PGPASSWORD='$PGPASSWORD'" >> /home/renderer/env
chown renderer /var/log/tiles
cd /home/renderer/src/openstreetmap-carto
sed "s/ dbname: \"gis\"/ host: \"$PGHOST\"\n\
port: \"${PGPORT:-5432}\"\n\
user: \"$PGUSER\"\n\
password: \"$PGPASSWORD\"\n\
asynchronous_request: true\n\
max_async_connection: 2\n\
dbname: \"$PGDB\"/" < project.mml > project1.mml
carto project1.mml > mapnik.xml
cat >>/usr/local/etc/renderd.conf <<EOF
[osm_carto]
URI=/osm/
TILEDIR=/var/lib/mod_tile
XML=/home/renderer/src/openstreetmap-carto/mapnik.xml
HOST=localhost
TILESIZE=256
EOF
cat >>/usr/local/etc/renderd.conf <<EOF
[osm_retina]
URI=/osmr/
TILEDIR=/var/lib/mod_tile
XML=/home/renderer/src/openstreetmap-carto/mapnik.xml
HOST=localhost
TILESIZE=512
SCALE=2
EOF
# Configure renderd threads
sed -i -E "s/num_threads=[0-9]+/num_threads=${THREADS:-4}/g" /usr/local/etc/renderd.conf
# Initialize Apache
service apache2 restart
# start cron job to trigger consecutive updates
if [ "$UPDATES" = "enabled" ] || [ "$UPDATES" = "1" ]; then
/etc/init.d/cron start
fi
# Run while handling docker stop's SIGTERM
stop_handler() {
kill -TERM "$child"
}
trap stop_handler SIGTERM
sudo -u renderer renderd -f -c /usr/local/etc/renderd.conf &
child=$!
wait "$child"
exit 0
fi
echo "invalid command"
exit 1