[GRASS-SVN] r71443 - grass-addons/grass7/vector/v.neighborhoodmatrix

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Aug 28 09:26:09 PDT 2017


Author: mlennert
Date: 2017-08-28 09:26:09 -0700 (Mon, 28 Aug 2017)
New Revision: 71443

Modified:
   grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html
   grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py
Log:
v.neighborhoodmatrix: add option to identify polygons through an arbitrary attribute column, instead of only category value


Modified: grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html
===================================================================
--- grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html	2017-08-28 04:50:26 UTC (rev 71442)
+++ grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.html	2017-08-28 16:26:09 UTC (rev 71443)
@@ -5,26 +5,33 @@
 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.
+By default, neighbors are identified by the category numbers. Using the
+<em>idcolumn</em> parameter the user can define a column to use as identifier
+of the polygons.
+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 the layer containing the
 polygons is empty and available for adding categories to boundaries.
 
+The module uses <em>v.to.db</em> to determine neighborhood relations.
+
 <h2>EXAMPLE</h2>
 
-Send the neighborhood matrix of the counties in the boundary_county map of the North Carolina dataset to standard output:
+Send the neighborhood matrix of the census tracts of the North Carolina dataset
+to standard output identifying each tract by its category value:
 
 <div class="code"><pre>
-v.neighborhoodmatrix in=boundary_county sep=,
+v.neighborhoodmatrix in=census_wake2000
 </pre></div>
 
-Idem, but sending the output to a file
+Idem, but identifying each tract by its STFID code and sending the output to a 
+file:
 
 <div class="code"><pre>
-v.neighborhoodmatrix in=boundary_county sep=, output=county_neighbors.csv
+v.neighborhoodmatrix in=census_wake2000 idcolumn=STFID output=census_neighbors.csv
 </pre></div>
 
 <h2>SEE ALSO</h2>

Modified: grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py
===================================================================
--- grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py	2017-08-28 04:50:26 UTC (rev 71442)
+++ grass-addons/grass7/vector/v.neighborhoodmatrix/v.neighborhoodmatrix.py	2017-08-28 16:26:09 UTC (rev 71443)
@@ -26,6 +26,11 @@
 #% answer: 1
 #% required: no
 #%end
+#%option G_OPT_DB_COLUMN
+#% key: idcolumn
+#% description: Name of column containing polygon ids
+#% required: no
+#%end
 #%option G_OPT_F_OUTPUT
 #% description: Name for output file (if omitted or "-" output to stdout)
 #% required: no
@@ -52,6 +57,7 @@
     input = options['input']
     player = int(options['player'])
     output = options['output']
+    idcolumn = options['idcolumn'] if options['idcolumn'] else False
     sep = separator(options['separator'])
     bidirectional = flags['b']
     global tempmapname
@@ -69,7 +75,7 @@
     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])])
+            temp_neighbors.append([int(line.split('|')[1]), int(line.split('|')[2])])
 
     #temp_neighbors.sort()
 
@@ -77,21 +83,47 @@
     if bidirectional:
         neighbors_reversed=[]
         for pair in temp_neighbors:
-                neighbors_reversed.append([pair[1], pair[0]])
+            neighbors_reversed.append([pair[1], pair[0]])
         temp_neighbors += neighbors_reversed
 
     #uniqify the list of integer pairs
     neighbors = [list(x) for x in set(tuple(x) for x in temp_neighbors)]
     neighbors.sort()
 
+    currentcat=''
     if output and output != '-':
         out=open(output, 'w')
-        for pair in neighbors:
-               	out.write(str(pair[0]) + sep + str(pair[1])+'\n')
+    for pair in neighbors:
+        if idcolumn:
+            # While pair[0] stays the same we don't have to call v.db.select
+            # again and again to get the id
+            if currentcat != pair[0]:
+                currentcat = pair[0]
+                fromid=gscript.read_command('v.db.select',
+                                 map=input,
+                                 column=idcolumn,
+                                 where="cat=%d" % pair[0],
+                                 layer=player,
+                                 flags="c",
+                                 quiet=True).rstrip()
+            toid=gscript.read_command('v.db.select',
+                             map=input,
+                             column=idcolumn,
+                             where="cat=%d" % pair[1],
+                             layer=player,
+                             flags="c",
+                             quiet=True).rstrip()
+            if output and output != '-':
+                out.write(fromid + sep + toid + '\n')
+            else:
+                print(fromid + sep + toid)
+        else:
+            if output and output != '-':
+                out.write(str(pair[0]) + sep + str(pair[1])+'\n')
+            else:
+                print(str(pair[0]) + sep + str(pair[1]))
+    if output and output != '-':
         out.close()
-    else:
-        for pair in neighbors:
-               	print(str(pair[0]) + sep + str(pair[1]))
 
 
 



More information about the grass-commit mailing list