Skip to content
Snippets Groups Projects

Added Docker setup for initial database import

Merged Felix Delattre requested to merge feature/add-docker-setup into master
Compare and
4 files
+ 142
7
Compare changes
  • Side-by-side
  • Inline

Files

files/start.sh 0 → 100755
+ 85
0
#!/bin/bash
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
set -e
printf "\nWelcome to the OpenBuildingMap importer\n\n"
# Wait until database is up and available
printf "Connecting to PostgreSQL on \"${OBM_DATABASE_HOST}\":\n"
until psql -h "${OBM_DATABASE_HOST}" -t -U "${OBM_DATABASE_USER}" -c 'SELECT datname FROM pg_database;'; do
printf "Database is unavailable - trying again in 10s\n"
sleep 10
done
# Report if database was created or reused
if ! psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" -c "\c ${OBM_DATABASE_NAME}"; then
psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" -c "CREATE DATABASE ${OBM_DATABASE_NAME}"
if psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" -c "\c ${OBM_DATABASE_NAME}"; then
psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" ${OBM_DATABASE_NAME} -c 'CREATE EXTENSION postgis;'
printf " postgis extension added to ${OBM_DATABASE_NAME} database.\n"
psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" ${OBM_DATABASE_NAME} -c 'CREATE EXTENSION hstore;'
printf " hstore extension added to ${OBM_DATABASE_NAME} database.\n"
psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" ${OBM_DATABASE_NAME} -c 'CREATE EXTENSION btree_gin;'
printf " btree_gin extension added to ${OBM_DATABASE_NAME} database.\n"
psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" ${OBM_DATABASE_NAME} -c 'CREATE EXTENSION btree_gist;'
printf " btree_gist extension added to ${OBM_DATABASE_NAME} database.\n"
else
printf "Error: Unable to create database for OpenBuildingMap!\n"
exit 1
fi
fi
# Prepare style file for osm2pgsql
base_style="/usr/share/osm2pgsql/empty.style"
if [ ! -r "${base_style}" ]
then
printf "Cannot find the `empty.style` file of osm2pgsql\n"
exit 1
fi
cat /usr/share/osm2pgsql/empty.style > /usr/share/osm2pgsql/the.style
cat ${OBM_IMPORT_STYLE} >> /usr/share/osm2pgsql/the.style
# Import OSM data if database is empty
database_exists=${psql -h ${OBM_DATABASE_HOST} -U ${OBM_DATABASE_USER} ${OBM_DATABASE_NAME} -t -c "SELECT CASE WHEN EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'planet_osm_line' LIMIT 1) THEN 'true' ELSE 'false' end;"}
if ! ${database_exists}; then
# Import OpenStreetMap data
if [ -f "${OBM_IMPORT_FILE}" ]; then
printf " \nImporting OpenStreetMap data with "
osm2pgsql \
--host ${OBM_DATABASE_HOST} --username ${OBM_DATABASE_USER} --database ${OBM_DATABASE_NAME} \
--verbose \
--create \
--slim \
--style "/usr/share/osm2pgsql/the.style" \
--multi-geometry \
--hstore \
--latlong \
--extra-attributes \
--hstore-add-index \
--cache-strategy dense \
${OBM_IMPORT_FILE}
printf "Success: OpenStreetMap data has been imported.\n"
else
printf "Error: No import file '${OBM_IMPORT_FILE}' has been found.\nGood bye.\n"
exit 1
fi
fi
Loading