[GRASS-SVN] r57247 - grass-addons/grass7/vector/v.area.weigh

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jul 22 12:34:25 PDT 2013


Author: mmetz
Date: 2013-07-22 12:34:25 -0700 (Mon, 22 Jul 2013)
New Revision: 57247

Added:
   grass-addons/grass7/vector/v.area.weigh/Makefile
   grass-addons/grass7/vector/v.area.weigh/v.area.weigh.html
   grass-addons/grass7/vector/v.area.weigh/v.area.weigh.py
Log:
v.area.weigh: mass-preserving area interpolation with known weights

Added: grass-addons/grass7/vector/v.area.weigh/Makefile
===================================================================
--- grass-addons/grass7/vector/v.area.weigh/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.area.weigh/Makefile	2013-07-22 19:34:25 UTC (rev 57247)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM=v.area.weigh
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/grass7/vector/v.area.weigh/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/grass7/vector/v.area.weigh/v.area.weigh.html
===================================================================
--- grass-addons/grass7/vector/v.area.weigh/v.area.weigh.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.area.weigh/v.area.weigh.html	2013-07-22 19:34:25 UTC (rev 57247)
@@ -0,0 +1,49 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.area.weigh</em> rasterizes vector areas using known cell weights. 
+In the output raster, the sum off all cells falling into a given area 
+is identical to the area attribute value used to rasterize the area.
+
+<p>
+<em>v.area.weigh</em> can add spatial detail to larger areas, if more 
+detailed information is available from other sources. For example, 
+population counts are typically available for administrative areas such 
+as provinces, counties or countries. At the same time, the location of
+urban areas might be known and can be used to refine spatial detail
+using <em>v.area.weigh</em>.
+
+<!--
+TODO: add reasonable example
+
+<h2>EXAMPLE</h2>
+
+<div class="code"><pre>
+# set computational region to raster ...
+g.region -p rast=...
+
+# extract urban areas
+r.mapcalc "urban = if(...)"
+
+# smooth a bit
+r.neighbors input=urban output=urban_ngbr5 size=5 method=average
+
+# refine 
+v.area.weigh vector=... column=... weight=urban_ngbr5 out=...
+</pre></div>
+
+-->
+
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.surf.mass.html">v.surf.mass</a>,
+<a href="v.to.rast.html">v.to.rast</a>, 
+<a href="r.statistics2.html">r.statistics2</a>
+</em>
+
+<h2>AUTHOR</h2>
+
+Markus Metz
+
+<p><i>Last changed: $Date$</i>


Property changes on: grass-addons/grass7/vector/v.area.weigh/v.area.weigh.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass-addons/grass7/vector/v.area.weigh/v.area.weigh.py
===================================================================
--- grass-addons/grass7/vector/v.area.weigh/v.area.weigh.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.area.weigh/v.area.weigh.py	2013-07-22 19:34:25 UTC (rev 57247)
@@ -0,0 +1,107 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:	v.area.weigh
+# AUTHOR(S):	Markus Metz
+# PURPOSE:	Rasterize vector areas using cell weights
+# COPYRIGHT:	(C) 2013 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.
+#
+#############################################################################
+
+#%module
+#% description: Rasterize vector areas using weights
+#% keywords: vector
+#% keywords: interpolation
+#% keywords: raster
+#%end
+#%option G_OPT_V_INPUT
+#% key: vector
+#%end
+#%option G_OPT_V_FIELD
+#%end
+#%option G_OPT_DB_COLUMN
+#% description: Name of attribute column to use as area values (must be numeric)
+#%end
+#%option G_OPT_R_INPUT
+#% key: weight
+#% description: Name of input raster with weights per cell
+#%end
+#%option G_OPT_R_OUTPUT
+#%end
+
+import sys
+import os
+import atexit
+import grass.script as grass
+
+
+def cleanup():
+    if rastertmp1:
+	grass.run_command('g.remove', rast = rastertmp1, quiet = True)
+    if rastertmp2:
+	grass.run_command('g.remove', rast = rastertmp2, quiet = True)
+    if rastertmp3:
+	grass.run_command('g.remove', rast = rastertmp3, quiet = True)
+
+def main():
+    global tmp, tmpname, rastertmp1, rastertmp2, rastertmp3
+    rastertmp1 = False
+    rastertmp2 = False
+    rastertmp3 = False
+
+    #### setup temporary files
+    tmp = grass.tempfile()
+    # we need a random name
+    tmpname = grass.basename(tmp)
+
+    vector = options['vector']
+    layer = options['layer']
+    column = options['column']
+    weight = options['weight']
+    output = options['output']
+
+
+    # is column numeric?
+    coltype = grass.vector_columns(vector, layer)[column]['type']
+    
+    if coltype not in ('INTEGER', 'DOUBLE PRECISION'):
+	grass.fatal(_("Column must be numeric"))
+    
+    # rasterize with cats (will be base layer)
+    rastertmp1 = "%s_%s_1" % (vector, tmpname)
+    if grass.run_command('v.to.rast', input = vector, output = rastertmp1,
+			 use = 'cat', quiet = True) != 0:
+	grass.fatal(_("An error occurred while converting vector to raster"))
+
+    # rasterize with column
+    rastertmp2 = "%s_%s_2" % (vector, tmpname)
+    if grass.run_command('v.to.rast', input = vector, output = rastertmp2,
+			 use = 'attr', layer = layer, attrcolumn = column, quiet = True) != 0:
+	grass.fatal(_("An error occurred while converting vector to raster"))
+
+    # zonal statistics
+    rastertmp3 = "%s_%s_3" % (vector, tmpname)
+    if grass.run_command('r.statistics2', base = rastertmp1, cover = weight,
+                         method = 'sum', output = rastertmp3, quiet = True) != 0:
+	grass.fatal(_("An error occurred while calculating zonal statistics"))
+
+    # weighted interpolation
+    exp = "$output = if($sumweight == 0, if(isnull($area_val), null(), 0), double($area_val) * $weight / $sumweight)"
+
+    grass.mapcalc(exp,
+                  output = output,
+		  sumweight = rastertmp3,
+		  area_val = rastertmp2,
+		  weight = weight)
+
+    sys.exit(0)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    main()


Property changes on: grass-addons/grass7/vector/v.area.weigh/v.area.weigh.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list