source: subversion/applications/utils/export/osm2pgsql/mapnik-osm-updater.sh @ 12445

Last change on this file since 12445 was 12445, checked in by joerg, 11 years ago

also include spatial_ref_sys.sql, check for more possible postgis versions

  • Property svn:executable set to *
File size: 23.9 KB
Line 
1#!/bin/bash
2
3export osm_username="osm"
4export database_name="gis"
5export planet_dir="/home/$osm_username/osm/planet"
6export planet_file="$planet_dir/planet.osm.bz2"
7export sql_dump="$planet_dir/planet.osm.sql.bz2"
8export log_dir=/var/log
9
10export osm2pgsql_cmd=`which osm2pgsql`
11test -x "$osm2pgsql_cmd" || osm2pgsql_cmd="$HOME/svn.openstreetmap.org/applications/utils/export/osm2pgsql/osm2pgsql"
12
13export gpsdrive_poitypes_cmd=`which gpsdrive-update-mapnik-db`
14test -x "$gpsdrive_poitypes_cmd" || gpsdrive_poitypes_cmd=`which gpsdrive-update-mapnik-poitypes`
15test -x "$gpsdrive_poitypes_cmd" || gpsdrive_poitypes_cmd="`dirname $0`/gpsdrive-update-mapnik-poitypes"
16test -x "$gpsdrive_poitypes_cmd" || gpsdrive_poitypes_cmd="`dirname $0`/../../gpsdrive-update-mapnik-poitypes.pl"
17
18osm_planet_mirror_cmd=`which osm-planet-mirror`
19test -x "$osm_planet_mirror_cmd" || osm_planet_mirror_cmd="`dirname $0`/../../planet-mirror/planet-mirror.pl"
20test -x "$osm_planet_mirror_cmd" || osm_planet_mirror_cmd="$HOME/svn.openstreetmap.org/applications/utils/planet-mirror/planet-mirror.pl"
21test -x "$osm_planet_mirror_cmd" || osm_planet_mirror_cmd="`dirname ../../planet-mirror/planet-mirror.pl`"
22
23test -n "$1" || help=1
24quiet=" -q "
25verbose=1
26
27for arg in "$@" ; do
28    case $arg in
29        --all-planet) # Do all the creation steps listed below from planet file
30            create_osm_user=1
31            mirror=1
32            check_newer_planet=
33            drop=1
34            create_db=1
35            db_table_create=1
36            create_db=1
37            create_db_user=1
38            db_add_900913=1
39            db_add_spatial_ref_sys=1
40            grant_all_rights_to_user_osm=1
41            planet_fill=1
42            db_add_gpsdrive_poitypes=1
43            create_db_users=${create_db_users:-*}
44            grant_db_users=${grant_db_users:-*}
45            ;;
46
47        --all-planet-geofabrik=*) #     Use Planet Extract from Frederics GeoFabrik.de Page as planet File and import
48                #               Use ? for a list of possible files
49                #               Example: europe/germany/baden-wuerttemberg
50            dir_country=${arg#*=}
51            country=`basename $dir_country`
52            planet_file="$planet_dir/${country}.osm.bz2"
53            mirror_geofabrik=${dir_country}
54            create_osm_user=1
55            mirror=
56            check_newer_planet=
57            drop=1
58            create_db=1
59            db_table_create=1
60            db_add_900913=1
61            db_add_spatial_ref_sys=1
62            create_db_user=1
63            grant_all_rights_to_user_osm=1
64            planet_fill=1
65            db_add_gpsdrive_poitypes=1
66            create_db_users=${create_db_users:-*}
67            grant_db_users=${grant_db_users:-*}
68            ;;
69
70        --all-planet-update) #  Do all the creation steps listed below from planet file with up to date cheching
71            create_osm_user=1
72            mirror=1
73            check_newer_planet=1
74            drop=1
75            create_db=1
76            create_db_user=1
77            grant_all_rights_to_user_osm=1
78            planet_fill=1
79            db_add_gpsdrive_poitypes=1
80            create_db_users=${create_db_users:-*}
81            grant_db_users=${grant_db_users:-*}
82            ;;
83
84        --all-from-dump) #      Do all the creation steps listed below
85                #       from planet-dump file
86                #       !!! all-from-dump is not completely tested yet
87            create_osm_user=1
88            mirror_dump=1
89            drop=1
90            create_db=1
91            create_db_user=1
92            grant_all_rights_to_user_osm=1
93            create_db_users=${create_db_users:-*}
94            fill_from_dump="$sql_dump"
95            grant_db_users=${grant_db_users:-*}
96            db_add_gpsdrive_poitypes=1
97            ;;
98
99        --all-create) #         Do all the creation steps listed below only no data
100                      # import and no planet mirroring
101            create_osm_user=1
102            drop=1
103            create_db=1
104            create_db_user=1
105            grant_all_rights_to_user_osm=1
106            create_db_users=${create_db_users:-*}
107            grant_db_users=${grant_db_users:-*}
108            ;;
109
110        --create-osm-user) #    create the osm-user needed
111                #       This means creating a user 'osm' and his home directory
112                #       with useradd, mkdir, chmod and chown
113            create_osm_user=1
114            ;;
115       
116        --mirror) #             mirror planet File (http://planet.openstreetmap.org/)
117            mirror=1
118            ;;
119
120        --no-mirror) #          do not mirror planet File
121            mirror=
122            ;;
123
124        --check-newer-planet) # Check if Planet File is newer then stampfile.
125            #           If yes: Continue
126            check_newer_planet=1
127            ;;
128
129        --drop) #               drop the old Database (gis) and Database-user (osm)
130            drop=1
131            ;;
132
133        --create-db) #          create the database (gis)
134            #           with this command only the database is created,
135            #           but no tables inside it
136            create_db=1
137            ;;
138       
139        --create-db-user) #     create the database-user (osm)
140            create_db_user=1
141            ;;
142       
143        --grant-all2osm-user) # grant all rights for the database to the DB-User osm
144            grant_all_rights_to_user_osm=1
145            ;;
146
147        --create-db-users=*) #Create a Database user for all users specified.
148            #           To create a db-user for all available system-user
149            #           specify *. (Except root))
150            create_db_users=${arg#*=}
151            ;;
152       
153        --grant-db-users=*) #   Grant database-users all rights (including write, ...)
154            #           to the gis Database !!! This has to be changed in the
155            #           future, normally only the osm user needs update rights
156            grant_db_users=${arg#*=}
157            ;;
158
159        --add-gpsdrive-types) # add GpsDrive POI-Types to points table
160            db_add_gpsdrive_poitypes=1
161            ;;
162
163        --planet-fill) #        fill database from planet File
164            planet_fill=1
165            ;;
166
167        --mirror-dump) #        mirror the planet.sql dump File
168            mirror_dump=1
169            ;;
170
171        --no-mirror-dump) #     Do not mirror the planet.sql dump File
172            mirror_dump=
173            ;;
174
175        --fill-from-dump=*) #   fill database from Dump File
176            fill_from_dump=${arg#*=}
177            ;;
178
179        --mapnik-dump=*) #      Dump Content of Mapnik Database to a File (.sql|.sql.bz))
180            postgis_mapnik_dump=${arg#*=}
181            ;;
182       
183        --db-table-create) #    Create tables in Database with osm2pgsql
184            db_table_create=1
185            ;;
186
187        --db-add-srid-900913) # Add SRID 900913
188            db_add_900913=1
189            ;;
190
191        --db-add-spatial_ref_sys) #     Add SRIDs to spatial_ref_sys
192            db_add_spatial_ref_sys=1
193            ;;
194
195        --count-db) #           Count entries in Database. This is to check
196            #           if the database really contains entries
197            #           if you set an  empty user with the option osm_username=''
198            #           the current user is used
199            count_db=1
200            ;;
201
202        -h)
203            help=1
204            ;;
205
206        --help)
207            help=1
208            ;;
209
210        -help)
211            help=1
212            ;;
213
214        --debug) #              switch on debugging
215            debug=1
216            verbose=1
217            quiet=""
218            ;;
219
220        -debug)
221            debug=1
222            verbose=1
223            quiet=""
224            ;;
225       
226
227        --nv) #                 be a little bit less verbose
228            verbose=''
229            ;;
230
231        --planet-dir=*) #       define Directory for Planet-File
232            planet_dir=${arg#*=}
233            planet_file="$planet_dir/planet.osm.bz2"
234            ;;
235
236        --planet-file=*) #      define Planet-File including Directory
237            planet_file=${arg#*=}
238            ;;
239       
240        --osm-username=*) #     Define username to use for DB creation and planet
241            #           download
242            #           !! You shouldn't use your username or root as the
243            #           !! download and install user.
244            #           This username is the download and install user.
245            #           The osm-user normally only should have the planet files
246            #           in hishome directory and nothing else. By default
247            #           the osm-username is 'osm'
248            osm_username=${arg#*=}
249
250            if [ "$osm_username" = "$USER" ] ; then
251                echo 
252                echo "!!!!!! ERROR: Don't use your own login account as the osm_username!!" 1>&2
253                echo 
254                exit 1
255            fi
256
257            if [ "$osm_username" = "root" ] ; then
258                echo 
259                echo "!!!!!! ERROR: Don't use the root account as the osm_username!!" 1>&2
260                echo 
261                exit 1
262            fi
263
264            planet_dir="/home/$osm_username/osm/planet"
265            planet_file="$planet_dir/planet.osm.bz2"
266            ;;
267       
268        --osm2pgsql-cmd=*) #    The path to the osm2pgsql command
269            #           It can be found at
270            #           svn.openstreetmap.org/applications/utils/export/osm2pgsql/
271            #           and has to be compiled. Alternatively you can install
272            #           the Debian Package openstreetmap-utils
273            osm2pgsql_cmd=${arg#*=}
274                if ! [ -x "$osm2pgsql_cmd" ]; then
275                    echo "!!!!!! ERROR: Cannot execute '$osm2pgsql_cmd'" 1>&2
276                    exit -1
277                fi
278                ;;
279
280        --database-name=*) #    use this name for the database default is 'gis'
281            database_name=${arg#*=}
282            ;;
283
284        *)
285            echo ""
286            echo "!!!!!!!!! Unknown option $arg"
287            echo ""
288            help=1
289            ;;
290    esac
291done
292
293if [ -n "$help" ] ; then
294    # extract options from case commands above
295    options=`grep -E -e esac -e '\s*--.*\).*#' $0 | sed '/esac/,$d;s/.*--/ [--/; s/=\*)/=val]/; s/)[\s ]/]/; s/#.*\s*//; s/[\n/]//g;'`
296    options=`for a in $options; do echo -n " $a" ; done`
297    echo "$0 $options"
298    echo "
299!!! Warning: This Script is for now a quick hack to make setting up
300!!! Warning: My databases easier. Please check if it really works for you!!
301!!! Warning: Especially when using different Database names or username, ...
302!!! Warning: not every combination of values except the default is tested.
303
304    This script tries to install the mapnik database.
305    For this it first creates a new user osm on the system
306    and mirrors the current planet to his home directory.
307    Then this planet is imported into the postgis Database from a
308    newly created user named osm
309
310    This script uses sudo. So you either have to have sudo right or you'll
311    have to start the script as root. The users needed will be postgres and osm
312    "
313    # extract options + description from case commands above
314    grep -E  -e esac -e '--.*\).*#' -e '^[\t\s  ]+#' $0 | \
315        grep -v /bin/bash | sed '/esac/,$d;s/.*--/  --/;s/=\*)/=val/;s/)//;s/#//;' 
316    exit;
317fi
318
319
320if [ -n "$osm_username" ] ; then
321    sudo_cmd="sudo -u $osm_username"
322else
323    sudo_cmd=''
324fi
325
326export import_stamp_file=${log_dir}/osm2pgsql_postgis-$database_name.stamp
327export import_log=${log_dir}/osm2pgsql_postgis-$database_name.log
328
329
330if [ -n "$debug" ] ; then
331        echo "Planet File: `ls -l $planet_file`"
332        echo "Import Stamp : `ls -l $import_stamp_file`"
333fi
334
335
336############################################
337# Create a user on the system
338############################################
339if [ -n "$create_osm_user" ] ; then
340    test -n "$verbose" && echo "----- Check if we already have an user '$osm_username'"
341   
342    if ! id "$osm_username" >/dev/null; then
343        echo "create '$osm_username' User"
344        useradd "$osm_username"
345    fi
346   
347    mkdir -p "/home/$osm_username/osm/planet"
348    # The user itself should be allowed to read/write all his own files
349    # in the ~/osm/ Directory
350    chown "$osm_username" "/home/$osm_username"
351    chown -R "$osm_username" "/home/$osm_username/osm"
352    chmod +rwX "/home/$osm_username"
353    chmod -R +rwX "/home/$osm_username/osm"
354
355    # Everyone on the system is allowed to read the planet.osm Files
356    chmod -R a+rX "/home/$osm_username/osm"
357fi
358
359
360############################################
361# Mirror the planet-dump File for Europe
362############################################
363if [ -n "$mirror_geofabrik" ] ; then
364    geofabrik_basedir="http://download.geofabrik.de/osm"
365    if [ "$mirror_geofabrik" = "?" ]; then
366        echo "Possible Values are:"
367        for sub_dir in "" "europe/" "europe/germany/"; do
368            wget -q  --level=0 -O - "$geofabrik_basedir/$sub_dir" | grep 'OpenStreetMap data' | \
369                perl -ne 'm/.*href="([^"]+)\.osm.bz2"/;print "  '$sub_dir'$1\n"'
370        done
371        exit 1 
372    fi
373    planet_source_file="${geofabrik_basedir}/${mirror_geofabrik}.osm.bz2"
374    test -n "$verbose" && echo "----- Mirroring planet File $planet_source_file"
375    wget -v --mirror "$planet_source_file" \
376        --no-directories --directory-prefix=$planet_dir/
377fi
378
379
380############################################
381# Mirror the newest planet File from planet.openstreetmap.org
382############################################
383if [ -n "$mirror" ] ; then
384    test -n "$verbose" && echo "----- Mirroring planet File"
385    if ! [ -x "$osm_planet_mirror_cmd" ]; then
386        echo "!!!!!! ERROR: Cannot execute '$osm_planet_mirror_cmd'" 1>&2
387        exit -1
388    fi
389    if ! $sudo_cmd $osm_planet_mirror_cmd -v -v --planet-dir=$planet_dir ; then 
390        echo "!!!!!! ERROR: Cannot Mirror Planet File" 1>&2
391        exit 1
392    fi
393    if ! [ -s $planet_file ] ; then
394        echo "!!!!!! ERROR: File $planet_file is missing"
395        exit -1
396    fi
397
398
399fi
400
401############################################
402# Check if Planet File is newer than import Stamp
403############################################
404if [ -n "$check_newer_planet" ] ; then
405    if [ "$planet_file" -nt "$import_stamp_file" ] ; then
406        if [ -n "$verbose" ] ; then
407            echo "----- New File needs updating"
408            echo "Planet File: `ls -l $planet_file`"
409            echo "Import Stamp : `ls -l $import_stamp_file`"
410        fi
411    else
412        echo "Postgis Database already Up To Date"
413        echo "`ls -l $import_stamp_file`"
414        exit 0
415    fi
416fi
417
418############################################
419# Drop the old Database and Database-user
420############################################
421if [ -n "$drop" ] ; then
422    test -n "$verbose" && echo "----- Drop complete Database '$database_name' and user '$osm_username'"
423    echo "CHECKPOINT" | sudo -u postgres psql $quiet
424    sudo -u postgres dropdb $quiet -Upostgres   "$database_name"
425    sudo -u postgres dropuser $quiet -Upostgres "$osm_username"
426fi
427
428############################################
429# Create db
430############################################
431if [ -n "$create_db" ] ; then
432    test -n "$verbose" && echo
433    test -n "$verbose" && echo "----- Create Database '$database_name'"
434    if ! sudo -u postgres createdb -Upostgres  $quiet  -EUTF8 "$database_name"; then
435        echo "!!!!!! ERROR: Creation of '$database_name' Failed"
436        exit -1
437    fi
438    if ! sudo -u postgres createlang plpgsql "$database_name"; then
439        echo "!!!!!! ERROR: Creation Failed"
440        exit -1
441    fi
442
443    lwpostgis="/usr/share/postgresql-8.4-postgis/lwpostgis.sql"
444    test -s $lwpostgis || lwpostgis="/usr/share/postgresql-8.3-postgis/lwpostgis.sql"
445    test -s $lwpostgis || lwpostgis="/usr/share/postgresql-8.2-postgis/lwpostgis.sql"
446    test -s $lwpostgis || lwpostgis="`ls /usr/share/postgresql-*-postgis/lwpostgis.sql| sort -n | head 1`"
447    if [ ! -s $lwpostgis ] ; then
448        echo "!!!!!! ERROR: Cannot find $lwpostgis"
449        exit -1
450    fi
451    if sudo -u postgres psql $quiet -Upostgres "$database_name" <${lwpostgis} ; then
452        echo "Enabling spacial Extentions done with '$lwpostgis'"
453    else
454        echo "!!!!!! ERROR: Creation with '$lwpostgis' Failed"
455        exit -1
456    fi
457fi
458
459############################################
460# Create db-user
461############################################
462if [ -n "$create_db_user" ] ; then
463    test -n "$verbose" && echo "----- Create Database-user '$osm_username'"
464    sudo -u postgres createuser -Upostgres  $quiet -S -D -R "$osm_username"  || exit -1
465fi
466
467if [ -n "$grant_all_rights_to_user_osm" ] ; then
468    test -n "$verbose" && echo 
469    test -n "$verbose" && echo "----- Grant rights on Database '$database_name' for '$osm_username'"
470    (
471        echo "GRANT ALL ON SCHEMA PUBLIC TO \"$osm_username\";" 
472        echo "GRANT ALL on geometry_columns TO \"$osm_username\";"
473        echo "GRANT ALL on spatial_ref_sys TO \"$osm_username\";" 
474        echo "GRANT ALL ON SCHEMA PUBLIC TO \"$osm_username\";" 
475    ) | sudo -u postgres psql $quiet -Upostgres "$database_name"
476fi
477
478############################################
479# Create a Database user for all users specified (*) or available on the system. Except root
480############################################
481if [ -n "$create_db_users" ] ; then
482
483    if [ "$create_db_users" = "*" ] ; then
484        echo "Create DB User for every USER"
485        create_db_users=''
486        # try to see if all users above uid=1000 are interesting
487        all_users=`cat /etc/passwd | sed 's/:/ /g' | while read user pwd uid rest ; do test "$uid" -ge "1000" || continue; echo $user; done`
488        echo "all_users: $all_users"
489        for user in $all_users ; do 
490            echo $user | grep -q -e root && continue
491            echo $user | grep -q -e "$osm_username" && continue
492            echo $user | grep -q -e "nobody" && continue
493            echo "$create_db_users" | grep -q  " $user " && continue
494            create_db_users=" $create_db_users $user "
495        done
496    fi
497
498# This is not good; this probably broke my postgres installation
499# dpkg  --purge postgresql-8.2
500# Stopping PostgreSQL 8.2 database server: main* Error: The cluster is owned by user id 107 which does not exist any more
501# apt-get -f install postgresql-8.2
502# Starting PostgreSQL 8.2 database server: main* Error: The cluster is owned by user id 107 which does not exist any more
503#if false ; then
504    for user in $create_db_users; do
505            echo "      Create DB User for $user"
506        sudo -u postgres createuser $quiet -Upostgres --no-superuser --no-createdb --no-createrole "$user"
507    done
508#fi
509
510fi
511
512############################################
513# Create Database tables with osm2pgsql
514############################################
515if [ -n "$db_table_create" ] ; then
516    if ! [ -x "$osm2pgsql_cmd" ]; then
517        echo "!!!!!! ERROR: Cannot execute '$osm2pgsql_cmd'" 1>&2
518        exit -1
519    fi
520    echo ""
521    echo "--------- Unpack and import $planet_file"
522    cd /usr/share/openstreetmap/
523    $sudo_cmd $osm2pgsql_cmd --create "$database_name"
524fi
525
526
527############################################
528# Add SRID spatial_ref_sys
529############################################
530if [ -n "$db_add_spatial_ref_sys" ] ; then
531    test -s "$srid_spatial_ref_sys" || srid_spatial_ref_sys="/usr/share/postgresql-8.4-postgis/spatial_ref_sys.sql"
532    test -s "$srid_spatial_ref_sys" || srid_spatial_ref_sys="/usr/share/postgresql-8.3-postgis/spatial_ref_sys.sql"
533    test -s "$srid_spatial_ref_sys" || srid_spatial_ref_sys="/usr/share/postgresql-8.2-postgis/spatial_ref_sys.sql"
534    test -s "$srid_spatial_ref_sys" || srid_spatial_ref_sys="/usr/share/postgresql-8.*-postgis/spatial_ref_sys.sql"
535    test -s "$srid_spatial_ref_sys" || srid_spatial_ref_sys="/usr/share/postgresql-*-postgis/spatial_ref_sys.sql"
536    if [ ! -s $srid_spatial_ref_sys ] ; then
537        echo "!!!!!! ERROR: Cannot find $srid_spatial_ref_sys"
538        exit -1
539    fi
540    if sudo -u postgres psql $quiet -Upostgres "$database_name" <${srid_spatial_ref_sys} ; then
541        echo "Adding  '$srid_spatial_ref_sys'"
542    else
543        echo "!!!!!! ERROR: Creation Failed"
544        exit -1
545    fi
546fi
547
548
549############################################
550# Add SRID 900913
551############################################
552if [ -n "$db_add_900913" ] ; then
553
554    test -s "$srid_900913" || srid_900913="`dirname $0`/900913.sql"
555    test -s "$srid_900913" || srid_900913="$HOME/svn.openstreetmap.org/applications/utils/export/osm2pgsql/900913.sql"
556    test -s "$srid_900913" || srid_900913="/usr/share/mapnik/900913.sql"
557    if [ ! -s $srid_900913 ] ; then
558        echo "!!!!!! ERROR: Cannot find $srid_900913"
559        exit -1
560    fi
561    if sudo -u postgres psql $quiet -Upostgres "$database_name" <${srid_900913} ; then
562        echo "Adding  '$srid_900913'"
563    else
564        echo "!!!!!! ERROR: Creation Failed"
565        exit -1
566    fi
567fi
568
569
570############################################
571# Grant all rights on the gis Database to all system users or selected users in the system
572############################################
573if [ -n "$grant_db_users" ] ; then
574
575    if [ "$grant_db_users" = "*" ] ; then
576        echo "-------- GRANT Rights to every USER"
577        grant_db_users=''
578        for user in `users` ; do 
579            echo "$user" | grep -q "root" && continue
580            echo " $grant_db_users " | grep -q " $user " && continue
581            grant_db_users="$grant_db_users $user"
582        done
583    fi
584
585    test -n "$verbose" && echo "Granting rights to users: '$grant_db_users'"
586
587    for user in $grant_db_users; do
588        echo "Granting all rights to user '$user' for Database '$database_name'"
589        (
590            echo "GRANT ALL on geometry_columns TO \"$user\";"
591            echo "GRANT ALL ON SCHEMA PUBLIC TO \"$user\";"
592            echo "GRANT ALL on spatial_ref_sys TO \"$user\";"
593            echo "GRANT ALL on TABLE planet_osm_line TO \"$user\";"
594            echo "GRANT ALL on TABLE planet_osm_point TO \"$user\";"
595            echo "GRANT ALL on TABLE planet_osm_roads TO \"$user\";"
596            echo "GRANT ALL on TABLE planet_osm_polygon TO \"$user\";"
597            )| sudo -u postgres psql $quiet -Upostgres "$database_name" || true
598    done
599fi
600
601
602############################################
603# Fill Database from planet File
604############################################
605if [ -n "$planet_fill" ] ; then
606    if ! [ -x "$osm2pgsql_cmd" ]; then
607        echo "!!!!!! ERROR: Cannot execute '$osm2pgsql_cmd'" 1>&2
608        exit -1
609    fi
610    echo ""
611    echo "--------- Unpack and import $planet_file"
612    echo "Import started: `date`" >>"$import_log"
613    cd /usr/share/openstreetmap/
614    $sudo_cmd $osm2pgsql_cmd --database "$database_name" $planet_file
615    rc=$?
616    if [ "$rc" -gt "0" ]; then
617        echo "`date`: Import With Error $rc:" >> "$import_log"
618        echo "`ls -l $planet_file` import --> rc($rc)" >> "$import_log"
619        echo "!!!!!!!! ERROR while running '$sudo_cmd $osm2pgsql_cmd --database "$database_name" $planet_file'"
620        echo "Creation with for Database "$database_name" from planet-file '$planet_file' with '$osm2pgsql_cmd' Failed"
621        echo "see Logfile for more Information:"
622        echo "less $import_log"
623        exit -1
624    fi
625    echo "`date`: Import Done: `ls -l $planet_file` import --> $rc" >> "$import_log"
626    echo "`date`: `ls -l $planet_file` import --> $rc" >>$import_stamp_file
627    touch --reference=$planet_file $import_stamp_file
628fi
629
630
631############################################
632# Add GpsDrive POI-Types to points Table
633############################################
634if [ -n "$db_add_gpsdrive_poitypes" ] ; then
635    if ! [ -x "$gpsdrive_poitypes_cmd" ]; then
636        echo "!!!!!! ERROR: Cannot execute gpsdrive_poitypes: '$gpsdrive_poitypes_cmd'" 1>&2
637        exit -1
638    fi
639    echo ""
640    echo "--------- Adding GpsDrive POI-Types to Database"
641    sudo -u postgres $gpsdrive_poitypes_cmd
642    rc=$?
643    if [ "$rc" -gt "0" ]; then
644        echo "!!!!!!! ERROR: cannot add poi types"
645        exit -1
646    fi
647fi
648
649
650############################################
651# Dump the complete Database
652############################################
653if [ -n "$postgis_mapnik_dump" ] ; then
654        # get Database Content with Dump
655    postgis_mapnik_dump_dir=`dirname $postgis_mapnik_dump`
656        mkdir -p "$postgis_mapnik_dump_dir"
657        case "$postgis_mapnik_dump" in
658            *.bz2)
659                $sudo_cmd pg_dump --data-only -U "$osm_username" "$database_name" | bzip2 >"$postgis_mapnik_dump"
660                ;;
661            *.gz)
662                $sudo_cmd pg_dump --data-only -U "$osm_username" "$database_name" | gzip >"$postgis_mapnik_dump"
663                ;;
664            *)
665                $sudo_cmd pg_dump --data-only -U "$osm_username" "$database_name" >"$postgis_mapnik_dump"
666                ;;
667        esac
668    if [ "$?" -gt "0" ]; then
669        echo "Error While dumping Database"
670    fi
671fi
672
673############################################
674# Mirror the planet-dump File from planet.openstreetmap.de
675############################################
676if [ -n "$mirror_dump" ] ; then
677    test -n "$verbose" && echo "----- Mirroring planet-dump File"
678    wget -v --mirror http://planet.openstreetmap.de/planet.osm.sql.bz2 \
679        --no-directories --directory-prefix=$planet_dir/
680fi
681
682
683############################################
684# Fill Database from Dump File
685############################################
686if [ -n "$fill_from_dump" ] ; then
687    echo ""
688    echo "--------- Import from Dump '$fill_from_dump'"
689    sudo -u postgres createdb -T template0 $database_name
690    case "$fill_from_dump" in
691        *.bz2)
692            test -n "$verbose" && echo "Uncompress File ..."
693            bzip2 -dc "$fill_from_dump" | $sudo_cmd psql $quiet "$database_name"
694            ;;
695        *.gz)
696            test -n "$verbose" && echo "Uncompress File ..."
697            gzip -dc "$fill_from_dump" | $sudo_cmd psql $quiet "$database_name"
698            ;;
699        *)
700            test -n "$verbose" && echo "Import uncompressed File ..."
701            $sudo_cmd psql $quiet "$database_name" <"$fill_from_dump"
702            ;;
703    esac
704    if [ "$?" -gt "0" ]; then
705        echo "Error While reding Dump into Database"
706    fi
707fi
708
709
710############################################
711# Check number of entries in Database
712############################################
713if [ -n "$count_db" ] ; then
714    echo ""
715    echo "--------- Check Number of lines in Database '$database_name'"
716
717    # Get the Table names
718    if [ -n "$osm_username" ]; then
719        table_owner=" AND tableowner ='$osm_username' ";
720    fi
721    table_names=`echo "SELECT tablename from pg_catalog.pg_tables where schemaname = 'public' $tableowner;" | \
722        $sudo_cmd psql   "$database_name" -h /var/run/postgresql | grep -E -e '^ planet'`
723
724    echo "Counting entries in all Tables (" $table_names ")"
725    for table in $table_names; do
726        echo -n "Table $table   = "
727        echo "SELECT COUNT(*) from $table;" | \
728            $sudo_cmd psql  gis -h /var/run/postgresql | grep -v -e count -e '------' -e '1 row' | head -1
729    done
730fi
731
Note: See TracBrowser for help on using the repository browser.