|
@@ -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,10 +77,8 @@ def main():
|
|
|
colnames = ['cat']
|
|
|
|
|
|
if option == 'coor':
|
|
|
- columns = ['dummy1', 'dummy2', 'dummy3']
|
|
|
extracolnames = ['x', 'y', 'z']
|
|
|
else:
|
|
|
- columns = ['dummy1']
|
|
|
extracolnames = [option]
|
|
|
|
|
|
if units in ['p', 'percent']:
|
|
@@ -92,18 +90,28 @@ def main():
|
|
|
|
|
|
# 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)
|
|
|
+ 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] == 'cat':
|
|
|
+ catcol = i
|
|
|
+ break
|
|
|
+ if catcol == -1:
|
|
|
+ # shouldn't happen, but let's do this
|
|
|
+ catcol = 0
|
|
|
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:
|
|
@@ -115,16 +123,15 @@ def main():
|
|
|
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,7 +140,7 @@ def main():
|
|
|
# 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:
|
|
|
records1 = []
|
|
|
p = grass.pipe_command('v.category', inp=mapname, layer=layer, option='print')
|
|
@@ -146,9 +153,8 @@ def main():
|
|
|
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:
|
|
@@ -179,9 +185,15 @@ def main():
|
|
|
# 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')
|