Splitting map scripts to offer initializing and resetting map.

This commit is contained in:
Roland 2022-11-03 20:27:12 -06:00
parent aef7f1162d
commit 3ef6ceb06f
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
5 changed files with 155 additions and 69 deletions

View file

@ -17,6 +17,13 @@ if [ "${1}" == "" ]; then
exit 3
fi
HAS_ICE_SHEET=false
psql gis wrolpi -c '\d' | grep icesheet_polygons && HAS_ICE_SHEET=true
if [ ${HAS_ICE_SHEET} = false ]; then
echo "Missing icesheet_polygons. Run reset_map.sh"
exit 4
fi
function cleanup() {
if [ -f ${MERGED_TMP_FILE} ]; then
rm ${MERGED_TMP_FILE}
@ -60,11 +67,11 @@ if [[ ${1} == *.osm.pbf ]]; then
elif [[ ${1} == *.dump ]]; then
if [ ! -f "${1}" ]; then
echo "File does not exist! ${1}"
exit 4
exit 7
fi
# Import a Postgresql dump.
nice -n 18 pg_restore -j3 --no-owner -d gis "${1}"
else
echo "Cannot import unknown map file"
exit 7
exit 8
fi

43
scripts/initialize_map.sh Executable file
View file

@ -0,0 +1,43 @@
#!/bin/bash
if [ ! -d /opt/openstreetmap-carto ]; then
echo "/opt/openstreetmap-carto does not exist. Run install.sh!"
exit 1
fi
# Use D.C. to initialized DB because it is so small.
wget --continue https://download.geofabrik.de/north-america/us/district-of-columbia-latest.osm.pbf \
-O /tmp/district-of-columbia-latest.osm.pbf || :
cd /opt/openstreetmap-carto || exit 2
# Initialize indexes and global polygons. This may take multiple tries.
MAX_TRIES=3
COUNT=0
SUCCESS=false
external_data=true
dc_import=false
indexes=false
while [ "${COUNT}" -lt "${MAX_TRIES}" ]; do
[[ "${external_data}" = false ]] &&
sudo -u wrolpi nice -n 18 /opt/openstreetmap-carto/scripts/get-external-data.py \
-C \
-d gis -U wrolpi &&
external_data=true
[[ "${dc_import}" = false ]] &&
sudo -u wrolpi nice -n 18 /opt/wrolpi/scripts/import_map.sh /tmp/district-of-columbia-latest.osm.pbf &&
dc_import=true
[[ "${indexes}" = false ]] &&
sudo -u wrolpi psql -d gis -f /opt/openstreetmap-carto/indexes.sql &&
indexes=true
if [[ "${external_data}" == true && "${dc_import}" == true && "${indexes}" == true ]]; then
SUCCESS=true
break
fi
((COUNT = COUNT + 1))
done
if [[ "${SUCCESS}" == false ]]; then
echo "Could not initialize map data. Try running reset_map.sh, or install.sh."
exit 3
fi

View file

@ -1,6 +1,6 @@
#! /usr/bin/bash
# This script installs OpenStreetMap tile server.
# This server uses Apache2, renderd and mod_tile to render and display map tiles.
# This server uses Apache2, renderd, and mod_tile to render and display map tiles.
set -x
set -e
@ -31,26 +31,18 @@ sed -i -e 's/^DAEMON=.*/DAEMON=\/usr\/bin\/$NAME/' /etc/init.d/renderd
sed -i -e 's/^DAEMON_ARGS=.*/DAEMON_ARGS="-c \/etc\/renderd.conf"/' /etc/init.d/renderd
cp /tmp/mod_tile/debian/renderd.service /lib/systemd/system/
chmod +x /etc/init.d/renderd
[ ! -d /var/lib/mod_tile ] && mkdir /var/lib/mod_tile
chown wrolpi /var/lib/mod_tile
# Initialize gis database.
git clone https://github.com/lrnselfreliance/openstreetmap-carto.git /opt/openstreetmap-carto || :
sudo -u wrolpi /bin/bash -c '(cd /opt/openstreetmap-carto && git fetch && git checkout master && git reset --hard origin/master && git pull --ff)'
chown -R wrolpi:wrolpi /opt/openstreetmap-carto
cd /opt/openstreetmap-carto
if [[ ! -f /opt/openstreetmap-carto/mapnik.xml || ! -s /opt/openstreetmap-carto/mapnik.xml ]]; then
/usr/local/bin/carto project.mml >/opt/openstreetmap-carto/mapnik.xml
fi
# Initialize indexes and global polygons.
sudo -u wrolpi /opt/openstreetmap-carto/scripts/get-external-data.py -d gis -U wrolpi
# Use D.C. to initialized DB because it is so small.
wget --continue https://download.geofabrik.de/north-america/us/district-of-columbia-latest.osm.pbf \
-O /tmp/district-of-columbia-latest.osm.pbf
# Run import in "create" mode so basic tables will be created.
sudo -u wrolpi osm2pgsql -d gis --create --slim -G --hstore --tag-transform-script \
/opt/openstreetmap-carto/openstreetmap-carto.lua -C 2000 --number-processes 4 \
-S /opt/openstreetmap-carto/openstreetmap-carto.style /tmp/district-of-columbia-latest.osm.pbf
sudo -u wrolpi psql -d gis -f /opt/openstreetmap-carto/indexes.sql
# Reset map. Force it.
/opt/wrolpi/scripts/reset_map.sh -f
cp /opt/wrolpi/etc/debian11/renderd.conf /etc/renderd.conf
# Configure Apache2 to listen on 8084.

View file

@ -1,6 +1,16 @@
#! /usr/bin/bash
# This script installs OpenStreetMap tile server.
# This server uses Apache2, renderd and mod_tile to render and display map tiles.
# This server uses Apache2, renderd, and mod_tile to render and display map tiles.
function yes_or_no {
while true; do
read -p "$* [y/n]:" yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) return 1 ;;
esac
done
}
set -x
set -e
@ -18,21 +28,6 @@ apt install -y libboost-all-dev git tar unzip wget bzip2 build-essential autocon
/usr/bin/npm install -g carto
/usr/bin/carto -v
function yes_or_no {
while true; do
read -p "$* [y/n]:" yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) return 1 ;;
esac
done
}
sudo -u postgres psql -c '\l' | grep gis &&
yes_or_no "Map database already exists. Replace it?" &&
systemctl stop renderd &&
sudo -u postgres dropdb gis
# Create `gis` database for map.
sudo -u postgres psql -c '\l' | grep gis || sudo -u postgres createdb -E UTF8 -O wrolpi gis
sudo -u postgres psql -d gis -c "CREATE EXTENSION postgis;" || :
@ -56,50 +51,15 @@ chown wrolpi /var/lib/mod_tile
# Initialize gis database.
git clone https://github.com/lrnselfreliance/openstreetmap-carto.git /opt/openstreetmap-carto || :
sudo -u wrolpi /bin/bash -c '(cd /opt/openstreetmap-carto && git fetch && git checkout master && git reset --hard origin/master && git pull --ff)'
chown -R wrolpi:wrolpi /opt/openstreetmap-carto
cd /opt/openstreetmap-carto
if [[ ! -f /opt/openstreetmap-carto/mapnik.xml || ! -s /opt/openstreetmap-carto/mapnik.xml ]]; then
/usr/bin/carto project.mml >/opt/openstreetmap-carto/mapnik.xml
fi
# Use D.C. to initialized DB because it is so small.
wget --continue https://download.geofabrik.de/north-america/us/district-of-columbia-latest.osm.pbf \
-O /tmp/district-of-columbia-latest.osm.pbf
# Initialize indexes and global polygons. This may take multiple tries.
set +e
MAX_TRIES=3
COUNT=0
SUCCESS=false
external_data=false
dc_import=false
indexes=false
while [ "${COUNT}" -lt "${MAX_TRIES}" ]; do
[[ "${external_data}" = false ]] &&
sudo -u wrolpi /opt/openstreetmap-carto/scripts/get-external-data.py -d gis -U wrolpi &&
external_data=true
[[ "${dc_import}" = false ]] &&
# Run import in "create" mode so basic tables will be created.
sudo -u wrolpi nice -n 18 osm2pgsql -d gis --create --slim -G --hstore \
--tag-transform-script /opt/openstreetmap-carto/openstreetmap-carto.lua \
-C 2000 --number-processes 3 \
-S /opt/openstreetmap-carto/openstreetmap-carto.style /tmp/district-of-columbia-latest.osm.pbf &&
dc_import=true
[[ "${indexes}" = false ]] &&
sudo -u wrolpi psql -d gis -f /opt/openstreetmap-carto/indexes.sql &&
indexes=true
if [[ "${external_data}" == true && "${dc_import}" == true && "${indexes}" == true ]]; then
SUCCESS=true
break
fi
((COUNT = COUNT + 1))
done
if [[ "${SUCCESS}" == false ]]; then
echo "Could not import map data"
exit 6
fi
set -e
# Reset map. Force it.
/opt/wrolpi/scripts/reset_map.sh -f
cp /opt/wrolpi/etc/ubuntu20.04/renderd.conf /usr/local/etc/renderd.conf
# Enable mod_tile.

84
scripts/reset_map.sh Executable file
View file

@ -0,0 +1,84 @@
#!/bin/bash
# Use 1/4 of the RAM to import. 1/2 causes crashes on RPi.
RAM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')
MAX_CACHE=$((RAM_KB / 1024 / 4))
function yes_or_no {
while true; do
read -p "$* [y/n]:" yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) return 1 ;;
esac
done
}
Help() {
# Display Help
echo "Reset the Map cache/database"
echo
echo "Syntax: reset_map.sh [-h] [-f]"
echo "options:"
echo "h Print this help."
echo "f Reset without prompt."
echo
}
FORCE=false
while getopts ":hf:" option; do
case $option in
h) # display Help
Help
exit
;;
f)
FORCE=true
;;
*) # invalid argument(s)
echo "Error: Invalid option"
exit 1
;;
esac
done
if [ "${FORCE}" == false ]; then
yes_or_no "Are you sure you want to delete the map database and cache? This can't be undone!" || exit 1
fi
if [[ ! -f /usr/lib/apache2/modules/mod_tile.so ||
! -f /opt/openstreetmap-carto/mapnik.xml ||
! -s /opt/openstreetmap-carto/mapnik.xml ||
! -f /opt/openstreetmap-carto/scripts/get-external-data.py ||
! -f /opt/openstreetmap-carto/openstreetmap-carto.style ||
! -f /opt/openstreetmap-carto/indexes.sql ]]; then
echo "The map has not been installed. Run install.sh"
exit 2
fi
set -e
set -x
systemctl stop renderd
if [ -d /var/lib/mod_tile ]; then
rm -rf /var/lib/mod_tile/*
else
mkdir /var/lib/mod_tile
fi
chown -R wrolpi:wrolpi /var/lib/mod_tile
sudo -u postgres dropdb gis || :
sudo -u postgres createdb -E UTF8 -O wrolpi gis
sudo -u postgres psql -d gis -c "CREATE EXTENSION postgis;" || :
sudo -u postgres psql -d gis -c "CREATE EXTENSION hstore;" || :
sudo -u postgres psql -d gis -c "ALTER TABLE geometry_columns OWNER TO wrolpi;"
sudo -u postgres psql -d gis -c "ALTER TABLE spatial_ref_sys OWNER TO wrolpi;"
# Reset "imported" status of any map files.
sudo -u postgres psql -d wrolpi -c "UPDATE map_file SET imported=false"
/opt/wrolpi/scripts/initialize_map.sh
echo "Map has been reset"