3
############################################################################
6
# AUTHOR(S): Markus Neteler, converted to Python by Glynn Clements
7
# Bug fixed by Huidae Cho <grass4u gmail.com>
8
# PURPOSE: Reports geometry statistics for vector maps
9
# COPYRIGHT: (C) 2005, 2007-2009 by MN and the GRASS Development Team
11
# This program is free software under the GNU General Public
12
# License (>=v2). Read the file COPYING that comes with GRASS
15
#############################################################################
18
#% description: Reports geometry statistics for vector maps.
21
#% keyword: statistics
25
#%option G_OPT_V_FIELD
26
#% guisection: Selection
31
#% description: Value to calculate
32
#% options: area,length,coor
35
#%option G_OPT_M_UNITS
36
#% options: miles,feet,meters,kilometers,acres,hectares,percent
41
#% description: Sort the result
43
#% descriptions: asc;Sort in ascending order;desc;Sort in descending order
48
import grass.script as grass
60
mapname = options['map']
61
option = options['option']
62
layer = options['layer']
63
units = options['units']
65
nuldev = file(os.devnull, 'w')
67
if not grass.find_file(mapname, 'vector')['file']:
68
grass.fatal(_("Vector map <%s> not found") % mapname)
70
if int(layer) in grass.vector_db(mapname):
71
colnames = grass.vector_columns(mapname, layer, getDict=False, stderr=nuldev)
78
columns = ['dummy1','dummy2','dummy3']
79
extracolnames = ['x','y','z']
82
extracolnames = [option]
84
if units in ['p','percent']:
91
# NOTE: we suppress -1 cat and 0 cat
93
p = grass.pipe_command('v.db.select', quiet = True, flags='c', map = mapname, layer = layer)
96
cols = line.rstrip('\r\n').split('|')
99
records1.append([int(cols[0])] + cols[1:])
101
if p.returncode != 0:
106
if len(records1) == 0:
108
f = grass.vector_db(map = mapname)[int(layer)]
109
grass.fatal(_("There is a table connected to input vector map '%s', but"
110
"there are no categories present in the key column '%s'. Consider using"
111
"v.to.db to correct this.") % (mapname, f['key']))
115
#fetch the requested attribute sorted by cat:
116
p = grass.pipe_command('v.to.db', flags = 'p',
118
map = mapname, option = option, columns = columns,
119
layer = layer, units = unitsp)
121
for line in p.stdout:
122
fields = line.rstrip('\r\n').split('|')
123
if fields[0] in ['cat', '-1', '0']:
125
records2.append([int(fields[0])] + fields[1:-1] + [float(fields[-1])])
130
# len(records1) may not be the same as len(records2) because
131
# v.db.select can return attributes that are not linked to features.
134
records3.append(filter(lambda r1: r1[0] == r2[0], records1)[0] + r2[1:])
137
p = grass.pipe_command('v.category', inp = mapname, layer = layer, option = 'print')
138
for line in p.stdout:
139
field = int(line.rstrip())
141
records1.append(field)
144
records1 = uniq(records1)
147
p = grass.pipe_command('v.to.db', flags = 'p',
148
map = mapname, option = option, columns = columns,
149
layer = layer, units = unitsp)
151
for line in p.stdout:
152
fields = line.split('|')
153
if fields[0] in ['cat', '-1', '0']:
155
records3.append([int(fields[0])] + fields[1:])
160
sys.stdout.write('|'.join(colnames + extracolnames) + '\n')
162
#make and print the table:
163
numcols = len(colnames) + len(extracolnames)
165
# calculate percents if requested
166
if units != '' and units in ['p','percent']:
167
# calculate total area value
170
areatot += float(r[-1])
172
# calculate area percentages
173
records4 = [float(r[-1]) * 100 / areatot for r in records3]
174
records3 = [r1 + [r4] for r1, r4 in zip(records1, records4)]
178
if options['sort'] == 'asc':
179
records3.sort(key = lambda r: r[-1])
181
records3.sort(key = lambda r: r[-1], reverse = True)
184
sys.stdout.write('|'.join(map(str,r)) + '\n')
186
if __name__ == "__main__":
187
options, flags = grass.parser()