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

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