~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to scripts/v.db.addtable/v.db.addtable

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
 
3
 
############################################################################
4
 
#
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
9
 
#
10
 
#               This program is free software under the GNU General Public
11
 
#               License (>=v2). Read the file COPYING that comes with GRASS
12
 
#               for details.
13
 
#
14
 
#############################################################################
15
 
 
16
 
#%Module
17
 
#%  description: Creates and connects a new attribute table to a given layer of an existing vector map.
18
 
#%  keywords: vector, database, attribute table
19
 
#%End
20
 
#%option
21
 
#% key: map
22
 
#% type: string
23
 
#% gisprompt: old,vector,vector
24
 
#% description: Vector map for which to add new attribute table
25
 
#% required : yes
26
 
#% key_desc : name
27
 
#%end
28
 
#%option
29
 
#% key: table
30
 
#% type: string
31
 
#% description: Name of new attribute table (default: vector map name)
32
 
#% required : no
33
 
#%end
34
 
#%option
35
 
#% key: layer
36
 
#% type: integer
37
 
#% gisprompt: old_layer,layer,layer
38
 
#% description: Layer where to add new attribute table
39
 
#% answer: 1
40
 
#% required : no
41
 
#%end
42
 
#%option
43
 
#% key: columns
44
 
#% type: string
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
47
 
#% required : no
48
 
#% multiple : yes
49
 
#% key_desc : name type
50
 
#%end
51
 
 
52
 
 
53
 
if  [ -z "$GISBASE" ] ; then
54
 
    echo "You must be in GRASS GIS to run this program." 1>&2
55
 
    exit 1
56
 
fi
57
 
 
58
 
# save command line
59
 
if [ "$1" != "@ARGS_PARSED@" ] ; then
60
 
    CMDLINE=`basename "$0"`
61
 
    for arg in "$@" ; do
62
 
        CMDLINE="$CMDLINE \"$arg\""
63
 
    done
64
 
    export CMDLINE
65
 
    exec g.parser "$0" "$@"
66
 
fi
67
 
 
68
 
PROG=`basename "$0"`
69
 
 
70
 
#### check if we have awk
71
 
if [ ! -x "`which awk`" ] ; then
72
 
    g.message -e "awk required, please install awk or gawk first"
73
 
    exit 1
74
 
fi
75
 
 
76
 
# setting environment, so that awk works properly in all languages
77
 
unset LC_ALL
78
 
LC_NUMERIC=C
79
 
export LC_NUMERIC
80
 
 
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?}
89
 
 
90
 
LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
91
 
 
92
 
 
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
97
 
   MAP_MAPSET="$MAPSET"
98
 
fi
99
 
if [ ! "$file" ] || [ "$MAP_MAPSET" != "$MAPSET" ] ; then
100
 
   g.message -e  "Vector map <$GIS_OPT_MAP> not found in current mapset"
101
 
   exit 1
102
 
else
103
 
   MAP_NAME=`echo "$GIS_OPT_MAP" | cut -f1 -d'@'` 
104
 
fi
105
 
 
106
 
 
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" 
110
 
       table="$MAP_NAME"
111
 
    else
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"
115
 
    fi
116
 
else
117
 
    g.message "Using user specified table name: $GIS_OPT_TABLE"
118
 
    table="$GIS_OPT_TABLE"
119
 
fi
120
 
 
121
 
# we use the DB settings of layer 1 to find out default connection
122
 
QUERYLAYER=1
123
 
 
124
 
# check if DB parameters are set, and if not set them.
125
 
db.connect -c
126
 
 
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..."
132
 
fi
133
 
database=`db.connect -p | grep '^database' | cut -d':' -f2-`
134
 
driver=`db.connect -p | grep '^driver' | cut -d':' -f2`
135
 
 
136
 
 
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>" 
142
 
     exit 1
143
 
  fi
144
 
fi
145
 
 
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:
150
 
 
151
 
   if [ "$GIS_OPT_COLUMNS" = "cat integer" ] ; then
152
 
      COLUMN_DEF="cat integer"
153
 
   else
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, /'`
159
 
   fi
160
 
   g.message "Creating table with columns ($COLUMN_DEF)"
161
 
 
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/'
168
 
       fi
169
 
   fi
170
 
 
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."
175
 
     exit 1
176
 
   fi
177
 
fi
178
 
 
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
182
 
 
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"
187
 
 
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"
191
 
fi
192
 
 
193
 
# write cmd history:
194
 
v.support map="$GIS_OPT_MAP" cmdhist="${CMDLINE}"
195
 
 
196
 
exit 0
197