Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
docker-spearhead
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Global Dynamic Exposure
server-components
containers
docker-spearhead
Merge requests
!2
Integrated spearhead as core for import logic
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Integrated spearhead as core for import logic
feature/spearhead
into
master
Overview
1
Commits
1
Pipelines
0
Changes
2
Merged
Felix Delattre
requested to merge
feature/spearhead
into
master
4 years ago
Overview
1
Commits
1
Pipelines
0
Changes
2
\approve
@ds
\fyi
@marius
0
0
Merge request reports
Compare
master
version 2
1e70179a
4 years ago
version 1
7b470834
4 years ago
master (base)
and
version 2
latest version
19aed7e4
1 commit,
4 years ago
version 2
1e70179a
1 commit,
4 years ago
version 1
7b470834
1 commit,
4 years ago
2 files
+
12
−
199
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
files/start.sh deleted
100755 → 0
+
0
−
193
View file @ 4521a438
#!/bin/bash
#
# Initialize import of OpenStreetMap data into OpenBuildingMap
# 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/.
#######################################
# Main function of this script, to be executed for initial import.
# Globals:
# OBM_DATABASE_HOST
# OBM_DATABASE_NAME
# Arguments:
# None
#######################################
function
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
onnect
${
OBM_DATABASE_NAME
}
"
;
then
printf
"Reuse existing database: %s
\n
"
"
${
OBM_DATABASE_NAME
}
"
else
execute_database_command
"CREATE DATABASE
${
OBM_DATABASE_NAME
}
;"
add_database_extensions
printf
"Initiated new database: %s
\n
"
"
${
OBM_DATABASE_NAME
}
"
fi
# Obtain mapping information for database schema
assemble_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
printf
"Assuming already imported database: %s
\n
"
"
${
OBM_DATABASE_NAME
}
"
else
import_openstreetmap_data
printf
"OpenStreetMap data has been imported.
\n
"
fi
}
#######################################
# Exectute postgresql command outside of a specific database.
# Globals:
# OBM_DATABASE_HOST
# OBM_DATABASE_USER
# Arguments:
# Query command
#######################################
function
execute_database_command
()
{
psql
--host
=
"
${
OBM_DATABASE_HOST
}
"
--username
=
"
${
OBM_DATABASE_USER
}
"
--command
=
"
${
1
}
"
}
#######################################
# Exectute postgresql query on a certain database.
# Globals:
# OBM_DATABASE_HOST
# OBM_DATABASE_NAME
# OBM_DATABASE_USER
# Arguments:
# Query command
#######################################
function
execute_database_query
()
{
psql
--host
=
"
${
OBM_DATABASE_HOST
}
"
--username
=
"
${
OBM_DATABASE_USER
}
"
"
${
OBM_DATABASE_NAME
}
"
\
--command
=
"
${
1
}
"
}
#######################################
# Add geospatial and advanced index extensions to a postgresql database.
# Globals:
# OBM_DATABASE_HOST
# OBM_DATABASE_NAME
# OBM_DATABASE_USER
# Arguments:
# None
#######################################
function
add_database_extensions
()
{
if
psql
--host
"=
${
OBM_DATABASE_HOST
}
"
--username
=
"
${
OBM_DATABASE_USER
}
"
\
--command
=
"
\c
onnect
${
OBM_DATABASE_NAME
}
"
;
then
if
execute_database_query
"CREATE EXTENSION postgis;"
;
then
printf
" postgis extension added to %s database.
\n
"
"
${
OBM_DATABASE_NAME
}
"
else
printf
" Could not add postgis extension added to %s database.
\n
Aborting.
\n
"
"
${
OBM_DATABASE_NAME
}
"
exit
1
fi
if
execute_database_query
"CREATE EXTENSION hstore;"
;
then
printf
" hstore extension added to %s database.
\n
"
"
${
OBM_DATABASE_NAME
}
"
else
printf
" Could not add hstore extension added to %s database.
\n
Aborting.
\n
"
"
${
OBM_DATABASE_NAME
}
"
exit
1
fi
if
execute_database_query
"CREATE EXTENSION btree_gin;"
;
then
printf
" btree_gin extension added to %s database.
\n
"
"
${
OBM_DATABASE_NAME
}
"
else
printf
" Could not add btree_gin extension added to %s database.
\n
Aborting.
\n
"
"
${
OBM_DATABASE_NAME
}
"
exit
1
fi
if
execute_database_query
"CREATE EXTENSION btree_gist;"
;
then
printf
" btree_gist extension added to %s database.
\n
"
"
${
OBM_DATABASE_NAME
}
"
else
printf
" Could not add btree_gist extension added to %s database.
\n
Aborting.
\n
"
"
${
OBM_DATABASE_NAME
}
"
exit
1
fi
else
printf
"Error: Unable to connect to database: %s
\n
"
"
${
OBM_DATABASE_NAME
}
"
exit
1
fi
}
#######################################
# # Prepare style file for osm2pgsql, based on standard 'empty.style' and provided style file.
# Globals:
# OBM_IMPORT_STYLE
# Arguments:
# None
# Outputs:
# Writes combined style file to '/usr/share/osm2pgsql/the.style'
#######################################
function
assemble_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 file into database with osm2pgsql.
# Globals:
# OBM_IMPORT_FILE
# OBM_DATABASE_HOST
# OBM_DATABASE_USER
# OBM_DATABASE_NAME
# Arguments:
# None
#######################################
function
import_openstreetmap_data
()
{
if
[[
-f
"
${
OBM_IMPORT_FILE
}
"
]]
;
then
printf
"
\n
Importing 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: Import file '%s' not found.
\n
Aborting.
\n
"
"
${
OBM_IMPORT_FILE
}
"
exit
1
fi
}
# Start the script
printf
"
\n
Welcome to the OpenBuildingMap importer
\n\n
"
# Call main function of the script
main
Loading