[GRASS-SVN] r59581 - in grass-addons/grass7/vector: . v.neighborhoodmatrix

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Apr 5 09:43:49 PDT 2014


Author: mlennert
Date: 2014-04-05 09:43:49 -0700 (Sat, 05 Apr 2014)
New Revision: 59581

Added:
   grass-addons/grass7/vector/v.neighborhoodmatrix/
   grass-addons/grass7/vector/v.neighborhoodmatrix/Makefile
   grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html
   grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py
Log:
new module to calculate neighborhood matrix

Added: grass-addons/grass7/vector/v.neighborhoodmatrix/Makefile
===================================================================
--- grass-addons/grass7/vector/v.neighborhoodmatrix/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.neighborhoodmatrix/Makefile	2014-04-05 16:43:49 UTC (rev 59581)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.neighborhoodmatrix
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html
===================================================================
--- grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html	2014-04-05 16:43:49 UTC (rev 59581)
@@ -0,0 +1,39 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.neighborhoodmatrix</em> identifies all adjacency relations between
+polygons in a vector map and exports these as a 2xn matrix where n is the 
+number of neighborhood relations with each relation only listed in one 
+direction (i.e. if a is neighbor of b, the list will contain a,b, but 
+not b,a) unless the <em>b</em> flag is specified.
+It uses <em>v.to.db</em> to determine neighborhood relations.
+If a path to an output file is specified, the matrix will be written to that file, otherwise it will be sent to standard output.
+
+<h2>NOTES</h2>
+
+Currently the module assumes that the layer above that containing the
+polygons is empty and available for adding categories to boundaries.
+
+<h2>EXAMPLE</h2>
+
+Send the neighborhood matrix of the counties in the boundary_county map of the North Carolina dataset:
+
+<div class="code"><pre>
+v.neighborhoodmatrix in=boundary_county sep=,
+</pre></div>
+
+Idem, but sending the output to a file
+
+<div class="code"><pre>
+v.neighborhoodmatrix in=boundary_county sep=, output=county_neighbors.csv
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.to.db.html">v.to.db</a>,
+</em>
+
+<h2>AUTHOR</h2>
+ Moritz Lennert
+
+<p><i>Last changed: $Date$</i>

Added: grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py
===================================================================
--- grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py	2014-04-05 16:43:49 UTC (rev 59581)
@@ -0,0 +1,102 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:       v.neighborhoodmatrix
+# AUTHOR:       Moritz Lennert
+# PURPOSE:      Exports a csv file with the neighborhood matrix of polygons
+#
+# COPYRIGHT:    (c) 2014 Moritz Lennert, 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
+#               for details.
+#
+#############################################################################
+
+#%module
+#% description: Exports the neighborhood matrix of polygons in a vector map
+#% keywords: vector
+#% keywords: neighborhood matrix
+#%end
+#%option G_OPT_V_INPUT
+#%end
+#%option
+#% key: player
+#% type: integer
+#% description: Layer in map where polygons are to be found
+#% answer: 1
+#% required: no
+#%end
+#%option G_OPT_F_OUTPUT
+#% description: Name for output file (if omitted or "-" output to stdout)
+#% required: no
+#%end
+#%option G_OPT_F_SEP
+#%end
+#%flag
+#% key: b
+#% description: create bidirectional matrix (same neighborhood relation repeated twice)
+#%end
+
+import sys
+from grass.script import core as grass
+
+
+def main():
+    # if no output filename, output to stdout
+    input = options['input']
+    player = int(options['player'])
+    output = options['output']
+    sep = options['separator']
+    bidirectional = flags['b']
+    tempmapname='neighborhoodmatrix_tempmap'
+    #TODO: automatically determine the first available layer in file
+    blayer = player+1
+
+    grass.run_command('v.category', input=input, output=tempmapname,
+            option='add', layer=blayer, type='boundary', quiet=True)
+    vtodb_results=grass.read_command('v.to.db', flags='p', map=tempmapname,
+            type='boundary', option='sides', layer=blayer, qlayer=player, quiet=True)
+    grass.run_command('g.remove', vect=tempmapname, quiet=True)
+
+    temp_neighbors=[]
+    for line in vtodb_results.splitlines():
+        if line.split('|')[1]<>'-1' and line.split('|')[2]<>'-1':
+                temp_neighbors.append([int(line.split('|')[1]), int(line.split('|')[2])])
+
+    n = len(temp_neighbors)
+    t=list(temp_neighbors)
+    t.sort()
+    assert n > 0
+    last = t[0]
+    lasti = i = 1
+    while i < n:
+        if t[i] != last:
+                t[lasti] = last = t[i]
+                lasti += 1
+        i += 1
+    neighbors=t[:lasti]
+
+    if bidirectional:
+        neighbors_reversed=[]
+        for pair in neighbors:
+                neighbors_reversed.append([pair[1], pair[0]])
+        neighbors += neighbors_reversed
+
+    neighbors.sort()
+
+    if output and output != '-':
+        out=open(output, 'w')
+        for pair in neighbors:
+               	out.write(str(pair[0]) + sep + str(pair[1])+'\n')
+        out.close()
+    else:
+        for pair in neighbors:
+               	print(str(pair[0]) + sep + str(pair[1]))
+
+
+
+    sys.exit()
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()


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



More information about the grass-commit mailing list