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

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