3
############################################################################
5
# MODULE: v.db.addtable
6
# AUTHOR(S): Markus Neteler
7
# PURPOSE: interface to db.execute to creates and add a new table to given vector map
8
# COPYRIGHT: (C) 2005, 2007 by Markus Neteler & the GRASS Development Team
10
# This program is free software under the GNU General Public
11
# License (>=v2). Read the file COPYING that comes with GRASS
14
#############################################################################
17
#% description: Creates and connects a new attribute table to a given layer of an existing vector map.
18
#% keywords: vector, database, attribute table
23
#% gisprompt: old,vector,vector
24
#% description: Vector map for which to add new attribute table
31
#% description: Name of new attribute table (default: vector map name)
37
#% gisprompt: old_layer,layer,layer
38
#% description: Layer where to add new attribute table
45
#% description: Name and type of the new column(s) (types depend on database backend, but all support VARCHAR(), INT, DOUBLE PRECISION and DATE)
46
#% answer: cat integer
49
#% key_desc : name type
53
if [ -z "$GISBASE" ] ; then
54
echo "You must be in GRASS GIS to run this program." 1>&2
59
if [ "$1" != "@ARGS_PARSED@" ] ; then
60
CMDLINE=`basename "$0"`
62
CMDLINE="$CMDLINE \"$arg\""
65
exec g.parser "$0" "$@"
70
#### check if we have awk
71
if [ ! -x "`which awk`" ] ; then
72
g.message -e "awk required, please install awk or gawk first"
76
# setting environment, so that awk works properly in all languages
81
### setup enviro vars ###
82
MAPSET=`g.gisenv get=MAPSET`
83
LOCATION_NAME=`g.gisenv get=LOCATION_NAME`
84
GISDBASE=`g.gisenv get=GISDBASE`
85
# the following line simply complains if any of these vars are unset.
86
# It could be removed, as `` will at least set to "", but is left
87
# here for historical-instructive purposes
88
: ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
90
LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
93
# does map exist in CURRENT mapset?
94
eval `g.findfile element=vector file="$GIS_OPT_MAP" mapset="$MAPSET"`
95
MAP_MAPSET=`echo "$GIS_OPT_MAP" | grep '@' | cut -f2 -d'@'`
96
if [ -z "$MAP_MAPSET" ] ; then
99
if [ ! "$file" ] || [ "$MAP_MAPSET" != "$MAPSET" ] ; then
100
g.message -e "Vector map <$GIS_OPT_MAP> not found in current mapset"
103
MAP_NAME=`echo "$GIS_OPT_MAP" | cut -f1 -d'@'`
107
if [ -z "$GIS_OPT_TABLE" ] ; then
108
if [ "$GIS_OPT_LAYER" -eq 1 ] ; then
109
g.message "Using vector map name as table name: $MAP_NAME"
112
# two avoid tables with identical names on higher layers
113
g.message "Using vector map name extended by layer number as table name: ${MAP_NAME}_$GIS_OPT_LAYER"
114
table="${MAP_NAME}_$GIS_OPT_LAYER"
117
g.message "Using user specified table name: $GIS_OPT_TABLE"
118
table="$GIS_OPT_TABLE"
121
# we use the DB settings of layer 1 to find out default connection
124
# check if DB parameters are set, and if not set them.
127
#check if anything is connected to layer 1:
128
v.db.connect map="$GIS_OPT_MAP" -gl layer=1 2> /dev/null | grep -w '^1' > /dev/null
129
if [ $? -ne 0 ] ; then
130
# nothing defined for layer 1
131
g.message "Creating new DB connection based on default mapset settings..."
133
database=`db.connect -p | grep '^database' | cut -d':' -f2-`
134
driver=`db.connect -p | grep '^driver' | cut -d':' -f2`
137
#maybe there is already a table linked to the selected layer?
138
if [ "$GIS_OPT_LAYER" -eq 1 ] ; then
139
v.db.connect "$GIS_OPT_MAP" -gl layer="$GIS_OPT_LAYER" 2> /dev/null | grep -w "^$GIS_OPT_LAYER" > /dev/null
140
if [ $? -eq 0 ] ; then
141
g.message -e "There is already a table linked to layer <$GIS_OPT_LAYER>"
146
#maybe there is already a table with that name?
147
db.tables database="$database" driver="$driver" 2> /dev/null | grep "^$table$"
148
if [ $? -ne 0 ] ; then
149
#if not existing, create it:
151
if [ "$GIS_OPT_COLUMNS" = "cat integer" ] ; then
152
COLUMN_DEF="cat integer"
154
COLUMN_DEF=`echo "cat integer, $GIS_OPT_COLUMNS" | sed \
155
-e 's/cat integer, cat integer,/cat integer, /' \
156
-e 's/cat integer, cat INTEGER,/cat integer, /' \
157
-e 's/cat integer, cat int,/cat integer, /' \
158
-e 's/cat integer, cat INT,/cat integer, /'`
160
g.message "Creating table with columns ($COLUMN_DEF)"
162
# take care if the DBF directory is missing (heck, the DBF driver should take care!)
163
if [ "$driver" = "dbf" ] ; then
164
if test ! -d "${LOCATION}/dbf" ; then
165
g.message "Creating missing DBF directory in MAPSET <$MAPSET>"
166
mkdir -p "${LOCATION}/dbf"
167
db.connect driver=dbf database='$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/'
171
echo "CREATE TABLE $table ($COLUMN_DEF)" | \
172
db.execute database="$database" driver="$driver"
173
if [ $? -ne 0 ] ; then
174
g.message -e "Cannot continue."
179
# connect the map to the DB:
180
v.db.connect map="$GIS_OPT_MAP" database="${database}" driver="$driver" \
181
layer="$GIS_OPT_LAYER" table="$table" key=cat -o
183
# finally we have to add cats into the attribute DB to make modules such as v.what.rast happy:
184
# (creates new row for each vector line):
185
v.to.db "$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" option=cat \
186
col=cat qlayer="$GIS_OPT_LAYER"
188
if [ -z "$GRASS_VERBOSE" ] || [ "$GRASS_VERBOSE" -gt 0 ] ; then
189
g.message "Current attribute table links:"
190
v.db.connect -p "$GIS_OPT_MAP"
194
v.support map="$GIS_OPT_MAP" cmdhist="${CMDLINE}"