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

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Feb 19 16:15:14 PST 2017


Author: hcho
Date: 2017-02-19 16:15:14 -0800 (Sun, 19 Feb 2017)
New Revision: 70632

Modified:
   grass/trunk/scripts/v.report/v.report.py
Log:
v.report: Fix unit=percent

Modified: grass/trunk/scripts/v.report/v.report.py
===================================================================
--- grass/trunk/scripts/v.report/v.report.py	2017-02-19 23:40:29 UTC (rev 70631)
+++ grass/trunk/scripts/v.report/v.report.py	2017-02-20 00:15:14 UTC (rev 70632)
@@ -81,7 +81,7 @@
     else:
         extracolnames = [option]
 
-    if units in ['p', 'percent']:
+    if units == 'percent':
         unitsp = 'meters'
     elif units:
         unitsp = units
@@ -90,6 +90,7 @@
 
     # NOTE: we suppress -1 cat and 0 cat
     if isConnection:
+        f = grass.vector_db(map=mapname)[int(layer)]
         p = grass.pipe_command('v.db.select', quiet=True, map=mapname, layer=layer)
         records1 = []
         catcol = -1
@@ -97,12 +98,12 @@
             cols = line.rstrip('\r\n').split('|')
             if catcol == -1:
                 for i in range(0, len(cols)):
-                    if cols[i] == 'cat':
+                    if cols[i] == f['key']:
                         catcol = i
                         break
                 if catcol == -1:
-                    # shouldn't happen, but let's do this
-                    catcol = 0
+                    grass.fatal(_("There is a table connected to input vector map '%s', but "
+                                  "there is no key column '%s'.") % (mapname, f['key']))
                 continue
             if cols[catcol] == '-1' or cols[catcol] == '0':
                 continue
@@ -115,7 +116,6 @@
 
         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']))
@@ -142,6 +142,7 @@
         for r2 in records2:
             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:
@@ -172,15 +173,18 @@
     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']:



More information about the grass-commit mailing list