[GRASS-SVN] r70631 - grass/trunk/scripts/v.report

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Feb 19 15:40:29 PST 2017


Author: hcho
Date: 2017-02-19 15:40:29 -0800 (Sun, 19 Feb 2017)
New Revision: 70631

Modified:
   grass/trunk/scripts/v.report/v.report.py
Log:
v.report: Sort numerically (all options are numbers); Sort coor by (x, y, z); Fix non-first cat column

Modified: grass/trunk/scripts/v.report/v.report.py
===================================================================
--- grass/trunk/scripts/v.report/v.report.py	2017-02-19 23:24:26 UTC (rev 70630)
+++ grass/trunk/scripts/v.report/v.report.py	2017-02-19 23:40:29 UTC (rev 70631)
@@ -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 @@
         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 @@
 
     # 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 @@
                 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 @@
         # 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 @@
         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 @@
     # 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