[GRASS-SVN] r71060 - in grass-addons/grass7/vector: . v.what.rast.multi

svn_grass at osgeo.org svn_grass at osgeo.org
Sun May 7 22:10:21 PDT 2017


Author: pierreroudier
Date: 2017-05-07 22:10:21 -0700 (Sun, 07 May 2017)
New Revision: 71060

Added:
   grass-addons/grass7/vector/v.what.rast.multi/
   grass-addons/grass7/vector/v.what.rast.multi/Makefile
   grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.html
   grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.py
Log:
added new add-on to query several rasters at a time

Added: grass-addons/grass7/vector/v.what.rast.multi/Makefile
===================================================================
--- grass-addons/grass7/vector/v.what.rast.multi/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.what.rast.multi/Makefile	2017-05-08 05:10:21 UTC (rev 71060)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.what.rast.multi
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.html
===================================================================
--- grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.html	2017-05-08 05:10:21 UTC (rev 71060)
@@ -0,0 +1,60 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.what.rast.multi</em> retrieves raster value from a given set of raster map for each point or centroid stored in a given vector map. It can update a <b>column</b> in the linked vector attribute table with the retrieved raster cell value or print it. It is essentially a wrapper around <em>v.what.rast</em>.
+
+<p>The column type needs to be numeric (integer, float, double, ...). If the column doesn't exist in the vector attribute table than the module will create the new column of type corresponding with the input raster map.
+
+<p>
+If the <b>-p</b> flag is used, then the attribute table is not updated and the results are printed to standard output.
+<p>
+If the <b>-i</b> flag is used, then the value to be uploaded to the database is interpolated from the four nearest raster cells values using an inverse distance weighting method (IDW). This is useful for cases when the vector point density is much higher than the raster cell size.
+
+<h2>NOTES</h2>
+
+<p>
+Points and centroid with shared category number cannot be processed. To solved this, unique categories may be added with <em><a href="v.category.html">v.category</a></em> in a separate layer.
+<p>
+If multiple points have the same category, the attribute value is set to NULL. If the raster value is NULL, then attribute value is set to NULL.
+<p>
+<em>v.what.rast.multi</em> operates on the attribute table. To modify the vector geometry instead, use <em><a href="v.drape.html">v.drape</a></em>.
+<p>
+Categories and values are output unsorted with the print flag. To sort them pipe the output of this module into the UNIX <tt>sort</tt> tool (<tt>sort -n</tt>). If you need coordinates, after sorting use <em><a href="v.out.ascii.html">v.out.ascii</a></em> and the UNIX <tt>paste</tt> tool (<tt>paste -d'|'</tt>). In the case of a NULL result, a "<tt>*</tt>" will be printed in lieu of the value.
+<p>
+The interpolation flag is only useful for continuous value raster maps, if a categorical raster is given as input the results will be nonsense. Since the search window is limited to four raster cells there may still be raster cell-edge artifacts visible in the results, this compromise has been made for processing speed. If one or more of the nearest four raster cells is NULL, then only the raster cells containing values will be used in the weighted average.
+
+
+<h2>EXAMPLES</h2>
+
+<h3>Transferring raster values into existing attribute table of vector points map</h3>
+
+Reading values from raster map at position of vector points, writing these values into a column of the attribute table connected to the vector map:
+<p>
+
+<div class="code"><pre>
+# set computational region
+g.region raster=slope -p
+
+# work on copy of original geodetic points map
+g.copy vector=geodetic_pts,mygeodetic_pts
+
+# query raster cells
+v.what.rast.multi map=mygeodetic_pts raster=elev_state_500m,slope,aspect columns=elevation,slope,aspect
+
+# print results
+v.db.select map=mygeodetic_pts columns=elevatin,slope,aspect separator=comma where="SLOPE > 0"
+
+</pre></div>
+
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.what.rast.html">v.what.rast</a>,
+</em>
+
+
+<h2>AUTHORS</h2>
+Pierre Roudier<br>
+
+<p>
+<i>Last changed: $Date$</i>


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

Added: grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.py
===================================================================
--- grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.py	                        (rev 0)
+++ grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.py	2017-05-08 05:10:21 UTC (rev 71060)
@@ -0,0 +1,157 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE:       v.what.rast.multi
+# AUTHOR(S):    Pierre Roudier
+# PURPOSE:      Uploads values of multiple rasters at positions of vector
+#               points to the table.
+# COPYRIGHT:    (C) 2017 by Pierre Roudier, and the GRASS Development Team
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+############################################################################
+
+#%module
+#% description: Uploads values of multiple rasters at positions of vector
+# points to the table.
+#% keyword: vector, sampling, raster, position, querying, attribute table,
+# surface information
+#%end
+
+#%flag
+#% key: i
+#% description: Interpolate values from the nearest four cells
+#%end
+
+#%option
+#% key: map
+#% type: string
+#% required: yes
+#% multiple: no
+#% key_desc: name
+#% label: Name of vector points map for which to edit attributes
+#% description: Or data source for direct OGR access
+#% gisprompt: old,vector,vector
+#%end
+
+#%option
+#% key: layer
+#% type: string
+#% required: no
+#% multiple: no
+#% label: Layer number or name
+#% description: Vector features can have category values in different layers.
+# This number determines which layer to use. When used with direct OGR access
+# this is the layer name.
+#% answer: 1
+#% gisprompt: old,layer,layer
+#%end
+
+#%option
+#% key: type
+#% type: string
+#% required: no
+#% multiple: yes
+#% options: point,centroid
+#% description: Input feature type
+#% answer: point
+#%end
+
+#%option
+#% key: raster
+#% type: string
+#% required: yes
+#% multiple: yes
+#% key_desc: name
+#% description: Name of existing raster map to be queried
+#% gisprompt: old,cell,raster
+#%end
+
+#%option
+#% key: columns
+#% type: string
+#% required: no
+#% multiple: no
+#% key_desc: name
+#% description: Names of attribute columns to be updated with the query result
+#% gisprompt: old,dbcolumn,dbcolumn
+#%end
+
+#%option
+#% key: where
+#% type: string
+#% required: no
+#% multiple: no
+#% key_desc: sql_query
+#% label: WHERE conditions of SQL statement without 'where' keyword
+#% description: Example: income < 1000 and population >= 10000
+#%end
+
+import sys
+import os
+import grass.script as grass
+from grass.pygrass.modules.shortcuts import vector as v
+
+if not "GISBASE" in os.environ:
+    grass.message("You must be in GRASS GIS to run this program.")
+    sys.exit(1)
+
+
+def main():
+
+    # Get options
+    vmap = options["map"]  # Vector points
+    layer = options["layer"]
+    vtype = options["type"]
+    rasters = options["raster"].split(',')  # List of rasters to sample
+    columns = options["columns"].split(',')  # List of column names
+    where = options["where"]
+
+    # If length(columns) != length(rasters), throw error
+    if columns != ['']:
+        if len(columns) != len(rasters):
+            grass.fatal(_(
+            "The number of rasters and the number of column names do not match"
+            ))
+
+    # Get flags
+    if flags["i"]:
+        fl = "i"
+    else:
+        fl = ""
+
+    # For each raster
+    for i in range(len(rasters)):
+
+        r = rasters[i]
+
+        if columns != ['']:
+            c = columns[i]
+        else:
+            c = r
+
+        # Sample using v.what.rast
+        v.what_rast(
+          map=vmap,
+          layer=layer,
+          type=vtype,
+          raster=r,
+          column=c,
+          where=where,
+          flags=flags
+        )
+
+    return 0
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())


Property changes on: grass-addons/grass7/vector/v.what.rast.multi/v.what.rast.multi.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list