[GRASS-SVN] r70696 - in grass-addons/grass7/raster: . r.object.spatialautocor

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Feb 28 05:28:37 PST 2017


Author: mlennert
Date: 2017-02-28 05:28:37 -0800 (Tue, 28 Feb 2017)
New Revision: 70696

Added:
   grass-addons/grass7/raster/r.object.spatialautocor/
   grass-addons/grass7/raster/r.object.spatialautocor/Makefile
   grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.html
   grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.py
Log:
r.object.spatialautocor: new addon to calculate global spatial autocorrelation for raster objects


Added: grass-addons/grass7/raster/r.object.spatialautocor/Makefile
===================================================================
--- grass-addons/grass7/raster/r.object.spatialautocor/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.object.spatialautocor/Makefile	2017-02-28 13:28:37 UTC (rev 70696)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.object.spatialautocor
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/grass7/raster/r.object.spatialautocor/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.html
===================================================================
--- grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.html	2017-02-28 13:28:37 UTC (rev 70696)
@@ -0,0 +1,46 @@
+<h2>DESCRIPTION</h2>
+
+<p><em>r.object.spatialautocor</em> calculates global spatial autocorrelation
+of the raster objects in the <em>object_map</em> based on the values in the
+<em>variable_map</em>. The user can choose between Moran's I or Geary's G
+indicator using the <em>method</em> parameter.
+
+<p>At this stage, neighborhood is simply defined by an adjancy matrix. The
+user can choose whether to also accept diagonal neighborhood by setting
+the <em>-d</em> flag.
+
+<h2>NOTES</h2>
+
+<p>
+The module depends on the addon <a href="https://grass.osgeo.org/grass70/manuals/addons/r.neighborhoodmatrix.html">r.neighborhoodmatrix</a> which needs to be installed.
+
+
+<h2>EXAMPLE</h2>
+
+<p>Calculate the spatial autocorrelation of altitude in the elevation map using
+individual patches in the landclass96 as objects:
+
+<p>
+<div class="code"><pre> 
+g.region rast=elevation
+r.clump landclass96 out=objects
+r.object.spatialautocor ob=objects var=elevation method=moran
+r.object.spatialautocor ob=objects var=elevation method=geary
+</pre></div>
+
+<h2>REFERENCES</h2>
+Moran, P.A.P., 1950. Notes on Continuous Stochastic Phenomena. Biometrika 37, 17–23. 
+<a href="https://dx.doi.org/10.2307%2F2332142">https://dx.doi.org/10.2307%2F2332142</a>
+<br><br>
+Geary, R.C., 1954. The Contiguity Ratio and Statistical Mapping. The Incorporated Statistician 5, 115.
+<a href="https://dx.doi.org/10.2307%2F2986645">https://dx.doi.org/10.2307%2F2986645</a>
+
+<h2>SEE ALSO</h2>
+
+<a href="https://grass.osgeo.org/grass70/manuals/addons/r.neighborhoodmatrix.html">r.neighborhoodmatrix</a><br>
+
+<h2>AUTHOR</h2> 
+
+Moritz Lennert
+
+<p><i>Last changed: $Date$</i>


Property changes on: grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.py
===================================================================
--- grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.py	2017-02-28 13:28:37 UTC (rev 70696)
@@ -0,0 +1,171 @@
+#!/usr/bin/env python
+#
+############################################################################
+#
+# MODULE:	r.object.spatialautocor
+# AUTHOR(S):	Moritz Lennert
+#
+# PURPOSE:	Calculates global spatial autocorrelation on raster objects
+# COPYRIGHT:	(C) 1997-2017 by 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.
+#
+#############################################################################
+# References:
+# 
+#Moran, P.A.P., 1950. Notes on Continuous Stochastic Phenomena. Biometrika 37,
+# 17-23. https://dx.doi.org/10.2307%2F2332142
+#Geary, R.C., 1954. The Contiguity Ratio and Statistical Mapping. The
+# Incorporated Statistician 5, 115. https://dx.doi.org/10.2307%2F2986645
+#############################################################################
+
+#%Module
+#% description: Spatial autocorrelation of raster objects
+#% keyword: raster
+#% keyword: statistics
+#% keyword: spatial autocorrelation
+#%end
+#
+#%option G_OPT_R_INPUT
+#% key: object_map
+#% description: Raster input map with objects
+#% required : yes
+#%end
+#
+#%option G_OPT_R_INPUT
+#% key: variable_map
+#% description: Raster input map with variable
+#% required : yes
+#%end
+#
+#%option
+#% key: method
+#% type: string
+#% description: Method for spatial autocorrelation
+#% options: moran,geary
+#% multiple: no
+#% required: yes
+#%end
+#
+#%flag
+#% key: d
+#% description: Also take into account diagonal neighbors
+#%end
+
+from __future__ import print_function
+import grass.script as gscript
+
+# check requirements
+def check_progs():
+   found_missing = False
+   for prog in ['r.neighborhoodmatrix']:
+       if not gscript.find_program(prog, '--help'):
+
+           found_missing = True
+           gscript.warning(_("'%s' required. Please install '%s' first using 'g.extension %s'") % (prog, prog, prog))
+   if found_missing:
+       gscript.fatal(_("An ERROR occurred running i.segment.uspo"))
+
+
+def get_nb_matrix (mapname, diagonal):
+    """ Create a dictionary with neighbors per segment"""
+
+    if diagonal:
+        res = gscript.read_command('r.neighborhoodmatrix',
+                                   input_=mapname,
+                                   output='-',
+                                   sep='comma',
+                                   flags='d',
+                                   quiet=True)
+    else:
+        res = gscript.read_command('r.neighborhoodmatrix',
+                                   input_=mapname,
+                                   output='-',
+                                   sep='comma',
+                                   quiet=True)
+
+    neighbordict = {}
+    for line in res.splitlines():
+        n1=line.split(',')[0]
+        n2=line.split(',')[1]
+        if n1 in neighbordict:
+            neighbordict[n1].append(n2)
+        else:
+            neighbordict[n1] = [n2]
+
+    return neighbordict
+
+
+def get_autocorrelation (mapname, raster, neighbordict, method):
+    """ Calculate either Moran's I or Geary's C for values of the given raster """
+
+    raster_vars = gscript.parse_command('r.univar',
+			  		map_=raster,
+			  		flags='g',
+			  		quiet=True)
+    global_mean = float(raster_vars['mean'])
+
+    univar_res = gscript.read_command('r.univar',
+			 	      flags='t',
+			 	      map_=raster,
+			 	      zones=mapname,
+			 	      out='-',
+			 	      sep='comma',
+                                      quiet=True)
+
+    means = {}
+    mean_diffs = {}
+    firstline = True
+    for line in univar_res.splitlines():
+	l = line.split(',')
+	if firstline:
+	    i = l.index('mean')
+	    firstline = False
+	else:
+	    means[l[0]] = float(l[i])
+	    mean_diffs[l[0]] = float(l[i]) - global_mean
+    
+    sum_sq_mean_diffs = sum(x**2 for x in mean_diffs.values())
+
+    total_nb_neighbors = 0
+    for region in neighbordict:
+	total_nb_neighbors += len(neighbordict[region])
+    
+    N = len(means)
+    sum_products = 0
+    sum_squared_differences = 0
+    for region in neighbordict:
+	region_value = means[region] - global_mean
+	neighbors = neighbordict[region]
+	nb_neighbors = len(neighbors)
+	for neighbor in neighbors:
+	    neighbor_value = means[neighbor] - global_mean
+	    sum_products += region_value * neighbor_value
+            sum_squared_differences = ( means[region] - means[neighbor] ) ** 2
+
+    if method == 'moran':
+        autocor = ( ( float(N) / total_nb_neighbors ) * (float(sum_products)  /  sum_sq_mean_diffs ) )
+    elif method == 'geary':
+        autocor = ( float(N - 1) /  ( 2 * total_nb_neighbors ) ) * ( float(sum_squared_differences)  / sum_sq_mean_diffs )
+
+    return autocor
+
+def main():
+
+    check_progs()
+
+    object_map = options['object_map']
+    variable_map = options['variable_map']
+    method = options['method']
+    diagonal = flags['d']
+
+    nb_matrix = get_nb_matrix(object_map, diagonal)
+    autocor = get_autocorrelation (object_map, variable_map, nb_matrix, method)
+    print("%.7f" % autocor)
+
+
+if __name__ == "__main__":
+    options, flags = gscript.parser()
+    main()


Property changes on: grass-addons/grass7/raster/r.object.spatialautocor/r.object.spatialautocor.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list