[GRASS-SVN] r67380 - grass/trunk/vector/v.what.rast

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Dec 26 04:35:21 PST 2015


Author: martinl
Date: 2015-12-26 04:35:21 -0800 (Sat, 26 Dec 2015)
New Revision: 67380

Modified:
   grass/trunk/vector/v.what.rast/main.c
   grass/trunk/vector/v.what.rast/v.what.rast.html
Log:
v.what.rast: create a new column if not exists


Modified: grass/trunk/vector/v.what.rast/main.c
===================================================================
--- grass/trunk/vector/v.what.rast/main.c	2015-12-25 22:32:25 UTC (rev 67379)
+++ grass/trunk/vector/v.what.rast/main.c	2015-12-26 12:35:21 UTC (rev 67380)
@@ -9,7 +9,7 @@
  *
  *  PURPOSE:      Query raster map
  *                
- *  COPYRIGHT:    (C) 2001-2013 by the GRASS Development Team
+ *  COPYRIGHT:    (C) 2001-2015 by the GRASS Development Team
  * 
  *                This program is free software under the GNU General
  *                Public License (>=v2).  Read the file COPYING that
@@ -38,7 +38,7 @@
     DCELL *dcell_row, *prev_d_row, *next_d_row;
     int width;
     int row, col;
-    char buf[2000];
+    char buf[DB_SQL_MAX];
     struct
     {
 	struct Option *vect, *rast, *field, *type, *col, *where;
@@ -162,20 +162,31 @@
      */
 
     if (!print_flag->answer) {
-	/* Check column type */
 	col_type = db_column_Ctype(driver, Fi->table, opt.col->answer);
 
-	if (col_type == -1)
-	    G_fatal_error(_("Column <%s> not found"), opt.col->answer);
+	if (col_type == -1) {
+            /* column doesn't exist, create it */
+            G_important_message(_("Column <%s> not found in the table <%s>. Creating..."),
+                                opt.col->answer, Fi->table);
+            sprintf(buf, "ALTER TABLE \"%s\" ADD COLUMN \"%s\" %s",
+                    Fi->table, opt.col->answer,
+                    out_type == CELL_TYPE ? "INTEGER" : "DOUBLE PRECISION");
+            db_set_string(&stmt, buf);
+            if (db_execute_immediate(driver, &stmt) != DB_OK)
+                G_fatal_error(_("Unable to add column <%s> to table <%s>"),
+                              opt.col->answer, Fi->table);
+        }
+        else {
+            /* check column type */
+            if (col_type != DB_C_TYPE_INT && col_type != DB_C_TYPE_DOUBLE)
+                G_fatal_error(_("Column type not supported"));
 
-	if (col_type != DB_C_TYPE_INT && col_type != DB_C_TYPE_DOUBLE)
-	    G_fatal_error(_("Column type not supported"));
+            if (out_type == CELL_TYPE && col_type == DB_C_TYPE_DOUBLE)
+                G_warning(_("Raster type is integer and column type is float"));
 
-	if (out_type == CELL_TYPE && col_type == DB_C_TYPE_DOUBLE)
-	    G_warning(_("Raster type is integer and column type is float"));
-
-	if (out_type != CELL_TYPE && col_type == DB_C_TYPE_INT)
-	    G_warning(_("Raster type is float and column type is integer, some data lost!!"));
+            if (out_type != CELL_TYPE && col_type == DB_C_TYPE_INT)
+                G_warning(_("Raster type is float and column type is integer, some data lost!!"));
+        }
     }
 
     vtype = Vect_option_to_types(opt.type);

Modified: grass/trunk/vector/v.what.rast/v.what.rast.html
===================================================================
--- grass/trunk/vector/v.what.rast/v.what.rast.html	2015-12-25 22:32:25 UTC (rev 67379)
+++ grass/trunk/vector/v.what.rast/v.what.rast.html	2015-12-26 12:35:21 UTC (rev 67380)
@@ -2,33 +2,41 @@
 
 <em>v.what.rast</em> retrieves raster value from a given 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. The column type
-needs to be numeric (integer, float, double, ...).
+vector attribute table with the retrieved raster cell value or print it.
+
+<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 correspoding 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 <tt>stdout</tt>.
+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>v.category</em> in
-a separate layer.
+To solved this, unique categories may be added
+with <em><a href="v.category.html">v.category</a></em> in a separate
+layer.
 
-<h2>NOTES</h2>
-
+<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</em> operates on the attribute table. To modify the vector
-geometry instead, use <em>v.drape</em>.
+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>v.out.ascii</em> and the UNIX <tt>paste</tt> tool
+<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>
@@ -57,8 +65,7 @@
 # set computational region to raster map to be queried
 g.region raster=elev_state_500m -p
 
-# add new column to existing table
-v.db.addcolumn map=mygeodetic_pts column="height double precision"
+# query raster cells (a new column will be added to existing table)
 v.what.rast map=mygeodetic_pts raster=elev_state_500m column=height
 
 # compare official geodetic heights to those of elevation model



More information about the grass-commit mailing list