[GRASS-SVN] r57948 - grass/trunk/scripts/v.report
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Oct 6 09:59:58 PDT 2013
Author: martinl
Date: 2013-10-06 09:59:58 -0700 (Sun, 06 Oct 2013)
New Revision: 57948
Modified:
grass/trunk/scripts/v.report/v.report.py
Log:
v.report: replace '-s/r' flags by 'sort' option
fix sorting itself
Modified: grass/trunk/scripts/v.report/v.report.py
===================================================================
--- grass/trunk/scripts/v.report/v.report.py 2013-10-06 13:16:40 UTC (rev 57947)
+++ grass/trunk/scripts/v.report/v.report.py 2013-10-06 16:59:58 UTC (rev 57948)
@@ -19,17 +19,10 @@
#% keywords: geometry
#% keywords: statistics
#%end
-#%flag
-#% key: r
-#% description: Reverse sort the result
-#%end
-#%flag
-#% key: s
-#% description: Sort the result
-#%end
#%option G_OPT_V_MAP
#%end
#%option G_OPT_V_FIELD
+#% guisection: Selection
#%end
#%option
#% key: option
@@ -41,6 +34,13 @@
#%option G_OPT_M_UNITS
#% options: miles,feet,meters,kilometers,acres,hectares,percent
#%end
+#%option
+#% key: sort
+#% type: string
+#% description: Sort the result
+#% options: asc,desc
+#% descriptions: asc;Sort in ascending order;desc;Sort in descending order
+#%end
import sys
import os
@@ -56,9 +56,6 @@
return result
def main():
- if flags['r'] and flags['s']:
- grass.fatal(_("Either -r or -s flag"))
-
mapname = options['map']
option = options['option']
layer = options['layer']
@@ -70,6 +67,7 @@
grass.fatal(_("Vector map '%s' not found in mapset search path.") % mapname)
colnames = grass.vector_columns(mapname, layer, getDict = False, stderr = nuldev)
+
if not colnames:
colnames = ['cat']
@@ -88,7 +86,6 @@
unitsp = None
# NOTE: we suppress -1 cat and 0 cat
-
if colnames:
p = grass.pipe_command('v.db.select', quiet = True, flags='c', map = mapname, layer = layer)
records1 = []
@@ -122,7 +119,7 @@
fields = line.rstrip('\r\n').split('|')
if fields[0] in ['cat', '-1', '0']:
continue
- records2.append([int(fields[0])] + fields[1:])
+ records2.append([int(fields[0])] + fields[1:-1] + [float(fields[-1])])
p.wait()
records2.sort()
@@ -169,13 +166,13 @@
records4 = [float(r[-1]) * 100 / areatot for r in records3]
records3 = [r1 + [r4] for r1, r4 in zip(records1, records4)]
- if flags['s']:
- # sort
- records3.sort(key = lambda r: (r[0], r[-1]))
- elif flags['r']:
- # reverse sort
- records3.sort(key = lambda r: (r[0], r[-1]), reverse = True)
-
+ # sort results
+ if options['sort']:
+ if options['sort'] == 'asc':
+ records3.sort(key = lambda r: r[-1])
+ else:
+ records3.sort(key = lambda r: r[-1], reverse = True)
+
for r in records3:
sys.stdout.write('|'.join(map(str,r)) + '\n')
More information about the grass-commit
mailing list