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
+ 168
7
Compare changes
  • Side-by-side
  • Inline

Files

files/start.sh 0 → 100755
+ 111
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"
main () {
printf "Connecting to PostgreSQL on '%s':\n" "${OBM_DATABASE_HOST}"
# Wait until database comes up and is available
until execute_database_command "SELECT datname FROM pg_database;"; do
printf "Database is unavailable - trying again in 10s\n"
sleep 10
done
# Initiate new or reuse existing database
if ! execute_database_command "\c ${OBM_DATABASE_NAME}"; then
execute_database_command "CREATE DATABASE ${OBM_DATABASE_NAME};"
add_database_extensions
printf "Initiated new database: %s\n" "${OBM_DATABASE_NAME}"
else
printf "Reuse existing database: %s\n" "${OBM_DATABASE_NAME}"
fi
# Obtain mapping information for database schema
get_opentstreetmap_database_style
# Import OpenStreetMap data if database is empty
database_exists=$(psql --host="${OBM_DATABASE_HOST}" --username="${OBM_DATABASE_USER}" "${OBM_DATABASE_NAME}" --tuples-only --command="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
printf "OpenStreetMap data has been imported.\n"
else
printf "Assuming already imported database: %s\n" "${OBM_DATABASE_NAME}"
fi
}
execute_database_command () {
psql --host="${OBM_DATABASE_HOST}" --username="${OBM_DATABASE_USER}" --command="${1}"
}
execute_database_query () {
psql --host="${OBM_DATABASE_HOST}" --username="${OBM_DATABASE_USER}" "${OBM_DATABASE_NAME}" --command="${1}"
}
add_database_extensions () {
if psql -h "${OBM_DATABASE_HOST}" -U "${OBM_DATABASE_USER}" -c "\c ${OBM_DATABASE_NAME}"; then
execute_database_query "CREATE EXTENSION postgis;"
printf " postgis extension added to %s database.\n" "${OBM_DATABASE_NAME}"
execute_database_query "CREATE EXTENSION hstore;"
printf " hstore extension added to %s database.\n" "${OBM_DATABASE_NAME}"
execute_database_query "CREATE EXTENSION btree_gin;"
printf " btree_gin extension added to %s database.\n" "${OBM_DATABASE_NAME}"
execute_database_query "CREATE EXTENSION btree_gist;"
printf " btree_gist extension added to %s database.\n" "${OBM_DATABASE_NAME}"
else
printf "Error: Unable to connect to database: %s\n" "${OBM_DATABASE_NAME}"
exit 1
fi
}
# Prepare style file for osm2pgsql
get_opentstreetmap_database_style () {
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_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}"
else
printf "Error: No import file '%s' has been found.\nGood bye.\n" "${OBM_IMPORT_FILE}"
exit 1
fi
}
main
Loading