-
Notifications
You must be signed in to change notification settings - Fork 22
/
load-osm.sh
executable file
·224 lines (205 loc) · 5.58 KB
/
load-osm.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/sh
USER=osm
DATABASE=gis
#HSTORE=1
OSM2PGSQL_BUILD_PATH=~/osm2pgsql
OSM2PGSQL_EXTRA_OPTIONS="--cache 16000"
# Load PBF/OSM file to postgresql database
load() {
if [ -n "$HSTORE" ]; then
HSTORE_OPT=--hstore
fi
PROCS=$((`nproc`-1))
osm2pgsql -d $DATABASE --slim $DROP_OPT $HSTORE_OPT --multi-geometry --number-processes $PROCS $OSM2PGSQL_EXTRA_OPTIONS --style $1 $2
}
# Remove slim tables from postgresql database
clean() {
psql -d gis -c 'drop table if exists planet_osm_nodes, planet_osm_ways, planet_osm_rels;'
psql -d gis -c 'vacuum freeze;'
}
# Create gzipped dump
dump() {
if [ -n "$1" ]; then
DUMP=$1
else
DUMP=db-$(date +%y%m%d-%H%M).sql.gz
fi
pg_dump -c -t 'planet_osm*' $DATABASE | gzip > $DUMP
}
# Send database over SSH, skipping dump step
transmit() {
pg_dump -c -t 'planet_osm*' $DATABASE | gzip | ssh $1 'gzip -dc | psql gis'
}
# Determine Linux type and version and call appropriate function
init_all() {
if [ -f /etc/fedora-release ]; then
VERSION=$(grep -o '[0-9]\+' /etc/fedora-release)
if [ "$VERSION" = "20" ]; then
install_f20
elif [ "$VERSION" = "19" ]; then
install_f19
else
echo "Unsupported Fedora version: $VERSION"
exit 1
fi
elif [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
LINUX=$(lsb_release -i | cut -d: -f2 | sed s/\\t//)
VERSION=$(lsb_release -r | cut -d: -f2 | sed s/\\t//)
if [ "$LINUX" = "Ubuntu" ]; then
if [ "$VERSION" = "14.04" ]; then
install_u1404
elif [ "$VERSION" = "12.04" ]; then
install_u1204
else
echo "Unsupported Ubuntu version: $VERSION"
exit 1
fi
else
echo "Unknown Linux: $LINUX $VERSION"
exit 1
fi
elif [ -f /etc/debian_version ]; then
VERSION=$(cut /etc/debian_version -d. -f1)
if [ "$VERSION" = "7" ]; then
install_deb7
else
echo "Unsupported Debian version: $VERSION"
exit 1
fi
else
echo "Unsupported Linux OS"
exit 1
fi
init_user
init_pgsql
echo
echo "Now relogin as $USER and load your OSM extract with $0 load."
}
init_user() {
useradd -m -s /bin/bash $USER
cp $0 /home/$USER
mv *.{style,osm,pbf}* /home/$USER
chown $USER /home/$USER/*
passwd $USER
}
init_pgsql() {
if [ -n "$HSTORE" ]; then
HSTORE_CMD='CREATE EXTENSION hstore;'
fi
su postgres -c "createuser -s $USER; createdb -E UTF8 -O $USER gis ; psql -d gis -c 'CREATE EXTENSION postgis;$HSTORE_CMD'"
}
build_osm2pgsql() {
mkdir -p $OSM2PGSQL_BUILD_PATH
git clone git://github.com/openstreetmap/osm2pgsql.git $OSM2PGSQL_BUILD_PATH
cd $OSM2PGSQL_BUILD_PATH
./autogen.sh
./configure
make
make install
cd -
}
# Enable overcommit (for faster importing)
overcommit() {
echo "vm.overcommit_memory=1" > /etc/sysctl.d/60-overcommit.conf
sysctl -p /etc/sysctl.d/60-overcommit.conf
}
# Install software on Fedora 20
install_f20() {
yum install -y postgresql postgresql-contrib postgresql-server postgis osm2pgsql screen
su postgres -c "initdb -E UTF8 -D /var/lib/pgsql/data"
systemctl enable postgresql
systemctl start postgresql
overcommit
}
# Install software on Fedora 19 - the same as for F20 apparently
install_f19() {
install_f20
}
# Install software on Ubuntu 14.04
install_u1404() {
add-apt-repository -y ppa:kakrueger/openstreetmap
apt-get update
apt-get --no-install-recommends install -y postgresql-9.3-postgis-2.1 osm2pgsql
# postgresql is automatically started
overcommit
}
debian_postgis_from_ppa() {
if ! grep postgresql.org /etc/apt/sources.list
then
echo "deb http://apt.postgresql.org/pub/repos/apt/ $1-pgdg main" >> /etc/apt/sources.list
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add -
fi
}
install_u1204() {
debian_postgis_from_ppa precise
apt-get update
apt-get --no-install-recommends install -y postgresql-9.3-postgis
# for building osm2pgsql from source
apt-get --no-install-recommends install -y build-essential libxml2-dev libgeos++-dev libpq-dev libbz2-dev proj libtool automake git libprotobuf-c0-dev protobuf-c-compiler
build_osm2pgsql
# postgresql is automatically started
overcommit
}
install_deb7() {
debian_postgis_from_ppa wheezy
apt-get update
apt-get --no-install-recommends install -y postgresql-9.3-postgis
# for building osm2pgsql from source
apt-get --no-install-recommends install -y build-essential libxml2-dev libgeos++-dev libpq-dev libbz2-dev libproj-dev libtool automake git libprotobuf-c0-dev protobuf-c-compiler
build_osm2pgsql
# postgresql is automatically started
overcommit
}
check_user() {
if [ -z "$1" ]; then
TARGET=$USER
else
TARGET=$1
fi
if [ "$(whoami)" != "$TARGET" ]; then
echo "Please run this task under user $TARGET"
exit 1
fi
}
case $1 in
init)
check_user root
init_all
;;
load|loadc)
check_user
if [ ! -f "$2" -o ! -f "$3" ]; then
echo "Please specify valid files for both parameters"
exit 1
fi
if [ "$1" == "loadc" ]; then
DROP_OPT=--drop
fi
load "$2" "$3"
;;
clear|clean)
check_user
clean
;;
dump)
check_user
dump $2
;;
transmit|send)
check_user
if [ -z "$2" ]; then
echo "Please specify user@ip for the first parameter"
exit 1
fi
transmit "$2" ;;
*)
echo "OSM Loader 1.0"
echo ""
echo "Usage: $0 init - install PostgreSQL and osm2pgsql, initialize database"
echo " $0 load <style> <file> - load OSM/PBF dump into the database"
echo " $0 loadc <style> <file> - load with --drop (eq. to clean option, but slightly faster)"
echo " $0 clean - drop slim tables (disables live updating)"
echo " $0 dump [file] - create a gzipped database dump (defaults to db-YYMMDD-HHMM.sql.gz)"
echo " $0 transmit user@ip - send database contents to a server via ssh, skipping dump step"
;;
esac