[GRASS-SVN] r70639 - grass/branches/releasebranch_7_2/scripts/v.report
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Feb 20 00:00:39 PST 2017
Author: hcho
Date: 2017-02-20 00:00:39 -0800 (Mon, 20 Feb 2017)
New Revision: 70639
Modified:
grass/branches/releasebranch_7_2/scripts/v.report/v.report.py
Log:
v.report: backport r70632 from trunk; various bug fixes
Modified: grass/branches/releasebranch_7_2/scripts/v.report/v.report.py
===================================================================
--- grass/branches/releasebranch_7_2/scripts/v.report/v.report.py 2017-02-20 05:27:54 UTC (rev 70638)
+++ grass/branches/releasebranch_7_2/scripts/v.report/v.report.py 2017-02-20 08:00:39 UTC (rev 70639)
@@ -4,9 +4,9 @@
#
# MODULE: v.report
# AUTHOR(S): Markus Neteler, converted to Python by Glynn Clements
-# Bug fixed by Huidae Cho <grass4u gmail.com>
+# Bug fixes, sort for coor by Huidae Cho <grass4u gmail.com>
# PURPOSE: Reports geometry statistics for vector maps
-# COPYRIGHT: (C) 2005, 2007-2009 by MN and the GRASS Development Team
+# COPYRIGHT: (C) 2005, 2007-2017 by MN and the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
@@ -77,13 +77,11 @@
colnames = ['cat']
if option == 'coor':
- columns = ['dummy1', 'dummy2', 'dummy3']
extracolnames = ['x', 'y', 'z']
else:
- columns = ['dummy1']
extracolnames = [option]
- if units in ['p', 'percent']:
+ if units == 'percent':
unitsp = 'meters'
elif units:
unitsp = units
@@ -92,22 +90,32 @@
# NOTE: we suppress -1 cat and 0 cat
if isConnection:
- p = grass.pipe_command('v.db.select', quiet=True, flags='c', map=mapname, layer=layer)
+ f = grass.vector_db(map=mapname)[int(layer)]
+ p = grass.pipe_command('v.db.select', quiet=True, map=mapname, layer=layer)
records1 = []
+ catcol = -1
for line in p.stdout:
cols = line.rstrip('\r\n').split('|')
- if cols[0] == '0':
+ if catcol == -1:
+ for i in range(0, len(cols)):
+ if cols[i] == f['key']:
+ catcol = i
+ break
+ if catcol == -1:
+ grass.fatal(_("There is a table connected to input vector map '%s', but "
+ "there is no key column '%s'.") % (mapname, f['key']))
continue
- records1.append([int(cols[0])] + cols[1:])
+ if cols[catcol] == '-1' or cols[catcol] == '0':
+ continue
+ records1.append(cols[:catcol] + [int(cols[catcol])] + cols[(catcol+1):])
p.wait()
if p.returncode != 0:
sys.exit(1)
- records1.sort()
+ records1.sort(key=lambda r: r[catcol])
if len(records1) == 0:
try:
- f = grass.vector_db(map=mapname)[int(layer)]
grass.fatal(_("There is a table connected to input vector map '%s', but "
"there are no categories present in the key column '%s'. Consider using "
"v.to.db to correct this.") % (mapname, f['key']))
@@ -115,16 +123,15 @@
pass
# fetch the requested attribute sorted by cat:
- p = grass.pipe_command('v.to.db', flags='p',
- quiet=True,
- map=mapname, option=option, columns=columns,
+ p = grass.pipe_command('v.to.db', flags='p', quiet=True,
+ map=mapname, option=option,
layer=layer, units=unitsp)
records2 = []
for line in p.stdout:
fields = line.rstrip('\r\n').split('|')
if fields[0] in ['cat', '-1', '0']:
continue
- records2.append([int(fields[0])] + fields[1:-1] + [float(fields[-1])])
+ records2.append([int(fields[0])] + fields[1:])
p.wait()
records2.sort()
@@ -133,8 +140,9 @@
# v.db.select can return attributes that are not linked to features.
records3 = []
for r2 in records2:
- records3.append(filter(lambda r1: r1[0] == r2[0], records1)[0] + r2[1:])
+ records3.append(filter(lambda r1: r1[catcol] == r2[0], records1)[0] + r2[1:])
else:
+ catcol = 0
records1 = []
p = grass.pipe_command('v.category', inp=mapname, layer=layer, option='print')
for line in p.stdout:
@@ -146,9 +154,8 @@
records1 = uniq(records1)
# make pre-table
- p = grass.pipe_command('v.to.db', flags='p',
- quiet=True,
- map=mapname, option=option, columns=columns,
+ p = grass.pipe_command('v.to.db', flags='p', quiet=True,
+ map=mapname, option=option,
layer=layer, units=unitsp)
records3 = []
for line in p.stdout:
@@ -166,22 +173,31 @@
numcols = len(colnames) + len(extracolnames)
# calculate percents if requested
- if units != '' and units in ['p', 'percent']:
- # calculate total area value
- areatot = 0
+ if units == 'percent' and option != 'coor':
+ # calculate total value
+ total = 0
for r in records3:
- areatot += float(r[-1])
+ total += float(r[-1])
- # calculate area percentages
- records4 = [float(r[-1]) * 100 / areatot for r in records3]
- records3 = [r1 + [r4] for r1, r4 in zip(records1, records4)]
+ # calculate percentages
+ records4 = [float(r[-1]) * 100 / total for r in records3]
+ if type(records1[0]) == int:
+ records3 = [[r1] + [r4] for r1, r4 in zip(records1, records4)]
+ else:
+ records3 = [r1 + [r4] for r1, r4 in zip(records1, records4)]
# sort results
if options['sort']:
if options['sort'] == 'asc':
- records3.sort(key=lambda r: r[-1])
+ if options['option'] == 'coor':
+ records3.sort(key=lambda r: (float(r[-3]), float(r[-2]), float(r[-1])))
+ else:
+ records3.sort(key=lambda r: float(r[-1]))
else:
- records3.sort(key=lambda r: r[-1], reverse=True)
+ if options['option'] == 'coor':
+ records3.sort(key=lambda r: (float(r[-3]), float(r[-2]), float(r[-1])), reverse=True)
+ else:
+ records3.sort(key=lambda r: float(r[-1]), reverse=True)
for r in records3:
sys.stdout.write('|'.join(map(str, r)) + '\n')
More information about the grass-commit
mailing list