forked from Overv/openstreetmap-tile-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·193 lines (154 loc) · 8.62 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
set -x
function createPostgresConfig() {
cp /etc/postgresql/12/main/postgresql.custom.conf.tmpl /etc/postgresql/12/main/conf.d/postgresql.custom.conf
sudo -u postgres echo "autovacuum = $AUTOVACUUM" >> /etc/postgresql/12/main/conf.d/postgresql.custom.conf
cat /etc/postgresql/12/main/conf.d/postgresql.custom.conf
}
function setPostgresPassword() {
sudo -u postgres psql -c "ALTER USER renderer PASSWORD '${PGPASSWORD:-renderer}'"
}
if [ "$#" -ne 1 ]; then
echo "usage: <import|run>"
echo "commands:"
echo " import: Set up the database and import /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
if [ "$1" = "import" ]; then
# Ensure that database directory is in right state
chown postgres:postgres -R /var/lib/postgresql
if [ ! -f /var/lib/postgresql/12/main/PG_VERSION ]; then
sudo -u postgres /usr/lib/postgresql/12/bin/pg_ctl -D /var/lib/postgresql/12/main/ initdb -o "--locale C.UTF-8"
fi
# Initialize PostgreSQL
createPostgresConfig
service postgresql start
sudo -u postgres createuser renderer
sudo -u postgres createdb -E UTF8 -O renderer 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 renderer;"
sudo -u postgres psql -d gis -c "ALTER TABLE spatial_ref_sys OWNER TO renderer;"
# Create new editor user "osmsrbija"
sudo -u postgres psql -d gis -c "CREATE USER osmsrbija WITH PASSWORD 'osmsrbija';"
setPostgresPassword
# Download Luxembourg as sample if no data is provided
#if [ ! -f /data.osm.pbf ] && [ -z "$DOWNLOAD_PBF" ]; then
# echo "WARNING: No import file at /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
if [ ! -f /data.osm.pbf ]; then
#Serbia
cd /
#Preuzimanje podataka
echo "INFO: Downloading PBF files of Serbia and Kosovo and Metohija":
sudo wget --backups=1 https://download.geofabrik.de/europe/serbia-latest.osm.pbf
sudo wget --backups=1 https://download.geofabrik.de/europe/kosovo-latest.osm.pbf
#Konverzija i spajanje podataka
echo "INFO: Merging PBF files of Serbia and Kosovo and Metohija":
sudo osmconvert kosovo-latest.osm.pbf -o=kosovo-latest.o5m
sudo osmconvert serbia-latest.osm.pbf -o=serbia-latest.o5m
sudo osmconvert kosovo-latest.o5m serbia-latest.o5m -o=srbija.o5m
echo "INFO: Creating joint data.osm.pbf":
sudo osmconvert srbija.o5m -o="data.osm.pbf"
sudo chmod 755 "data.osm.pbf"
echo "INFO: File data.osm.pbf is ready for import":
sudo rm serbia-latest.osm.pbf kosovo-latest.osm.pbf kosovo-latest.o5m serbia-latest.o5m srbija.o5m
fi
#if [ -n "$DOWNLOAD_PBF" ]; then
# echo "INFO: Download PBF file: $DOWNLOAD_PBF"
# wget -nv "$DOWNLOAD_PBF" -O /data.osm.pbf
# if [ -n "$DOWNLOAD_POLY" ]; then
# echo "INFO: Download PBF-POLY file: $DOWNLOAD_POLY"
# wget -nv "$DOWNLOAD_POLY" -O /data.poly
# fi
#fi
if [ "$UPDATES" = "enabled" ]; then
# determine and set osmosis_replication_timestamp (for consecutive updates)
osmium fileinfo /data.osm.pbf > /var/lib/mod_tile/data.osm.pbf.info
osmium fileinfo /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.poly ]; then
sudo -u renderer cp /data.poly /var/lib/mod_tile/data.poly
fi
# Import data of Serbia
sudo -u renderer osm2pgsql -d gis --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.osm.pbf ${OSM2PGSQL_EXTRA_ARGS}
# Grant editing to osmsrbija
sudo -u postgres psql -d gis -c "GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA public TO osmsrbija;"
sudo -u postgres psql -d gis -c "GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO osmsrbija;"
# Data customization
# Delete from public.planet_osm_line
sudo -u postgres psql -d gis -c "delete from public.planet_osm_line where admin_level = '2';"
sudo -u postgres psql -d gis -c "delete from public.planet_osm_line where admin_level = '3';"
sudo -u postgres psql -d gis -c "delete from public.planet_osm_line where admin_level = '4' and boundary = 'administrative';"
# Delete fromplanet_osm_roads
sudo -u postgres psql -d gis -c "delete from public.planet_osm_roads where admin_level = '2' and boundary = 'administrative';"
sudo -u postgres psql -d gis -c "delete from public.planet_osm_roads where admin_level = '3' and boundary = 'administrative';"
#import from serbian_borders.csv
sudo -u postgres psql -d gis -c "COPY public.planet_osm_line FROM '/serbian_border.csv' DELIMITER ',' CSV HEADER;"
sudo -u postgres psql -d gis -c "COPY public.planet_osm_roads FROM '/serbian_border.csv' DELIMITER ',' CSV HEADER;"
#Language
sudo -u postgres psql -d gis -c "UPDATE planet_osm_line SET name = \"name:sr\" where \"name:sr\" is not null;"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_line SET name = \"name:sr-Latn\" where \"name:sr\" is null;"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_point SET name = \"name:sr\" where \"name:sr\" is not null;"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_point SET name = \"name:sr-Latn\" where \"name:sr\" is null;"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_roads SET name = \"name:sr\" where \"name:sr\" is not null;"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_roads SET name = \"name:sr-Latn\" where \"name:sr\" is null;"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_polygon SET name = \"name:sr\" where \"name:sr\" is not null;"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_polygon SET name = \"name:sr-Latn\" where \"name:sr\" is null;"
#Pristina
sudo -u postgres psql -d gis -c "UPDATE planet_osm_point SET admin_level = null where name = 'Приштина';"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_point SET tags = '\"ele\"=>\"72\", \"is_in\"=>\"Serbia\", \"capital\"=>\"4\", \"wikidata\"=>\"Q25270\", \"population\"=>\"204721\"' where name = 'Приштина';"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_polygon SET tags = '\"capital\"=>\"no\"'where name = 'Приштина';"
# Kosovo -> Kosovo i Metohija
sudo -u postgres psql -d gis -c "UPDATE planet_osm_polygon SET name = '' where name='Косово';"
sudo -u postgres psql -d gis -c "UPDATE planet_osm_point SET name = '' where name='Косово';"
# Create indexes
sudo -u postgres psql -d gis -f indexes.sql
# Register that data has changed for mod_tile caching purposes
touch /var/lib/mod_tile/planet-import-complete
service postgresql stop
exit 0
fi
if [ "$1" = "run" ]; then
# Clean /tmp
rm -rf /tmp/*
# Fix postgres data privileges
chown postgres:postgres /var/lib/postgresql -R
# Configure Apache CORS
if [ "$ALLOW_CORS" == "enabled" ] || [ "$ALLOW_CORS" == "1" ]; then
echo "export APACHE_ARGUMENTS='-D ALLOW_CORS'" >> /etc/apache2/envvars
fi
# Initialize PostgreSQL and Apache
createPostgresConfig
service postgresql start
service apache2 restart
setPostgresPassword
# Configure renderd threads
sed -i -E "s/num_threads=[0-9]+/num_threads=${THREADS:-4}/g" /usr/local/etc/renderd.conf
# 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"
service postgresql stop
exit 0
fi
echo "invalid command"
exit 1