[GRASS-SVN] r68377 - in grass-addons/grass7/vector: . v.sort.points

svn_grass at osgeo.org svn_grass at osgeo.org
Wed May 4 10:56:44 PDT 2016


Author: mlennert
Date: 2016-05-04 10:56:44 -0700 (Wed, 04 May 2016)
New Revision: 68377

Added:
   grass-addons/grass7/vector/v.sort.points/
   grass-addons/grass7/vector/v.sort.points/Makefile
   grass-addons/grass7/vector/v.sort.points/v.sort.points.html
   grass-addons/grass7/vector/v.sort.points/v.sort.points.py
Log:
v.sort.points: first commit, module to sort points according to a numeric attribute column


Added: grass-addons/grass7/vector/v.sort.points/Makefile
===================================================================
--- grass-addons/grass7/vector/v.sort.points/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.sort.points/Makefile	2016-05-04 17:56:44 UTC (rev 68377)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.sort.points
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/vector/v.sort.points/v.sort.points.html
===================================================================
--- grass-addons/grass7/vector/v.sort.points/v.sort.points.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.sort.points/v.sort.points.html	2016-05-04 17:56:44 UTC (rev 68377)
@@ -0,0 +1,24 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.sort.points</em> takes an <b>input</b> point vector map, sorts 
+the points by the values in the sort <b>column</b> and then writes
+the result to the <b>output</b> map.
+<p>
+This is useful to display symbols in sizes proportionate to that same
+column without having big symbols hide small symbols.
+<p>
+By setting the <b>r</b> flag, the user can also chose to reverse the 
+sorting order putting points with the highest value in front.
+
+<h2>EXAMPLE</h2>
+
+<div class="code"><pre>
+d.vect -r map=schools_wake at PERMANENT type=point,line,boundary,area,face width=1 icon=basic/circle size=1 size_column=CAPACITYTO
+v.sort.points input=schools_wake output=sorted_schools column=CAPACITYTO
+d.vect -r map=sorted_schools at user1 type=point,line,boundary,area,face width=1 icon=basic/circle size=1 size_column=CAPACITYTO
+</pre></div>
+
+<h2>AUTHOR</h2>
+Moritz Lennert
+
+<p><i>Last changed: $Date: 2016-05-04 19:42:00 +0100 (mer 04 avr 2016) $</i>

Added: grass-addons/grass7/vector/v.sort.points/v.sort.points.py
===================================================================
--- grass-addons/grass7/vector/v.sort.points/v.sort.points.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.sort.points/v.sort.points.py	2016-05-04 17:56:44 UTC (rev 68377)
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+#
+#########################################################################
+#
+# MODULE:       v.sort.points
+#
+# AUTHOR(S):    Moritz Lennert
+#
+# PURPOSE:      Takes a point map and creates a new point map with the points
+#               sorted according to a chosen numeric column with largest 
+#               values first
+#
+# DATE:         Wed May  4 18:44:05 2016
+#
+#########################################################################
+
+#%module
+#% description: Sorts a vector point map according to a numeric column
+#%end
+#%option G_OPT_V_INPUT
+#% required: yes
+#%end
+#%option G_OPT_V_OUTPUT
+#% required: yes
+#%end
+#%option G_OPT_DB_COLUMN
+#% description: Name of attribute column used for sorting
+#% required: yes
+#%end
+#%flag
+#% key: r
+#% description: do not reverse sort
+#%end
+
+import sys
+import os
+
+import grass.script as gscript
+
+def num(s):
+    try:
+        return int(s)
+    except ValueError:
+	try:
+	    return float(s)
+	except:
+	    return s
+
+def main():
+    options, flags = gscript.parser()
+    inputmap = options['input']
+    outputmap = options['output']
+    sort_column = options['column']
+    reverse = True
+    if flags['r']:
+        reverse = False
+
+    columns = gscript.vector_columns(inputmap)
+    sort_index = columns[sort_column]['index']+2
+    sorted_cols = sorted(columns.iteritems(), key=lambda (x, y): y['index'])
+    column_def="x DOUBLE PRECISION, y DOUBLE PRECISION, cat INTEGER"
+    colnames = []
+    for colcount in range(1,len(sorted_cols)):
+        name = sorted_cols[colcount][0]
+        type = sorted_cols[colcount][1]['type']
+	if name == sort_column and (type != 'INTEGER' and type != 'DOUBLE PRECISION'):
+	    gscript.fatal('Sort column must be numeric')
+        colnames.append(name)
+        column_def += ", %s %s" % (name, type)
+
+    inpoints=gscript.read_command('v.out.ascii',
+                            in_=inputmap,
+                            columns=colnames,
+                            quiet=True)
+
+    points=[]
+    for line in inpoints.splitlines():
+        data = [num(x) for x in line.split('|')]
+        points.append(data)
+
+    points_sorted=sorted(points, key=lambda x: x[sort_index], reverse=reverse)
+
+    outpoints = ""
+    for list in points_sorted:
+        outpoints+="|".join([str(x) for x in list])+"\n"
+
+    gscript.write_command('v.in.ascii',
+                        input='-',
+                        stdin=outpoints,
+                        output=outputmap,
+                        x=1,
+                        y=2,
+                        cat=3,
+                        columns=column_def,
+                        quiet=True)
+
+    gscript.run_command('v.db.dropcolumn',
+                        map=outputmap,
+                        columns='x,y',
+                        quiet=True)
+
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main())


Property changes on: grass-addons/grass7/vector/v.sort.points/v.sort.points.py
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list