Changeset 67380


Ignore:
Timestamp:
Dec 26, 2015, 4:35:21 AM (9 years ago)
Author:
martinl
Message:

v.what.rast: create a new column if not exists

Location:
grass/trunk/vector/v.what.rast
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • grass/trunk/vector/v.what.rast/main.c

    r67261 r67380  
    1010 *  PURPOSE:      Query raster map
    1111 *               
    12  *  COPYRIGHT:    (C) 2001-2013 by the GRASS Development Team
     12 *  COPYRIGHT:    (C) 2001-2015 by the GRASS Development Team
    1313 *
    1414 *                This program is free software under the GNU General
     
    3939    int width;
    4040    int row, col;
    41     char buf[2000];
     41    char buf[DB_SQL_MAX];
    4242    struct
    4343    {
     
    163163
    164164    if (!print_flag->answer) {
    165         /* Check column type */
    166165        col_type = db_column_Ctype(driver, Fi->table, opt.col->answer);
    167166
    168         if (col_type == -1)
    169             G_fatal_error(_("Column <%s> not found"), opt.col->answer);
    170 
    171         if (col_type != DB_C_TYPE_INT && col_type != DB_C_TYPE_DOUBLE)
    172             G_fatal_error(_("Column type not supported"));
    173 
    174         if (out_type == CELL_TYPE && col_type == DB_C_TYPE_DOUBLE)
    175             G_warning(_("Raster type is integer and column type is float"));
    176 
    177         if (out_type != CELL_TYPE && col_type == DB_C_TYPE_INT)
    178             G_warning(_("Raster type is float and column type is integer, some data lost!!"));
     167        if (col_type == -1) {
     168            /* column doesn't exist, create it */
     169            G_important_message(_("Column <%s> not found in the table <%s>. Creating..."),
     170                                opt.col->answer, Fi->table);
     171            sprintf(buf, "ALTER TABLE \"%s\" ADD COLUMN \"%s\" %s",
     172                    Fi->table, opt.col->answer,
     173                    out_type == CELL_TYPE ? "INTEGER" : "DOUBLE PRECISION");
     174            db_set_string(&stmt, buf);
     175            if (db_execute_immediate(driver, &stmt) != DB_OK)
     176                G_fatal_error(_("Unable to add column <%s> to table <%s>"),
     177                              opt.col->answer, Fi->table);
     178        }
     179        else {
     180            /* check column type */
     181            if (col_type != DB_C_TYPE_INT && col_type != DB_C_TYPE_DOUBLE)
     182                G_fatal_error(_("Column type not supported"));
     183
     184            if (out_type == CELL_TYPE && col_type == DB_C_TYPE_DOUBLE)
     185                G_warning(_("Raster type is integer and column type is float"));
     186
     187            if (out_type != CELL_TYPE && col_type == DB_C_TYPE_INT)
     188                G_warning(_("Raster type is float and column type is integer, some data lost!!"));
     189        }
    179190    }
    180191
  • grass/trunk/vector/v.what.rast/v.what.rast.html

    r66157 r67380  
    33<em>v.what.rast</em> retrieves raster value from a given raster map for each point
    44or centroid stored in a given vector map. It can update a <b>column</b> in the linked
    5 vector attribute table with the retrieved raster cell value or print it. The column type
    6 needs to be numeric (integer, float, double, ...).
     5vector attribute table with the retrieved raster cell value or print it.
     6
     7<p>The column type needs to be numeric (integer, float, double,
     8...). If the column doesn't exist in the vector attribute table than
     9the module will create the new column of type correspoding with the
     10input raster map.
     11
    712<p>
    813If the <b>-p</b> flag is used, then the attribute table is not updated
    9 and the results are printed to <tt>stdout</tt>.
     14and the results are printed to standard output.
    1015<p>
    1116If the <b>-i</b> flag is used, then the value to be uploaded to the database
     
    1318distance weighting method (IDW). This is useful for cases when the vector
    1419point density is much higher than the raster cell size.
    15 <p>
    16 Points and centroid with shared category number cannot be processed.
    17 To solved this, unique categories may be added with <em>v.category</em> in
    18 a separate layer.
    1920
    2021<h2>NOTES</h2>
    2122
     23<p>
     24Points and centroid with shared category number cannot be processed.
     25To solved this, unique categories may be added
     26with <em><a href="v.category.html">v.category</a></em> in a separate
     27layer.
     28
     29<p>
    2230If multiple points have the same category, the attribute value is set to NULL.
    2331If the raster value is NULL, then attribute value is set to NULL.
    2432<p>
    2533<em>v.what.rast</em> operates on the attribute table. To modify the vector
    26 geometry instead, use <em>v.drape</em>.
     34geometry instead, use <em><a href="v.drape.html">v.drape</a></em>.
    2735<p>
    2836Categories and values are output unsorted with the print flag. To sort them
    2937pipe the output of this module into the UNIX <tt>sort</tt> tool
    3038(<tt>sort&nbsp;-n</tt>). If you need coordinates, after sorting use
    31 <em>v.out.ascii</em> and the UNIX <tt>paste</tt> tool
     39<em><a href="v.out.ascii.html">v.out.ascii</a></em> and the UNIX <tt>paste</tt> tool
    3240(<tt>paste&nbsp;-d'|'</tt>). In the case of a NULL result, a "<tt>*</tt>"
    3341will be printed in lieu of the value.
     
    5866g.region raster=elev_state_500m -p
    5967
    60 # add new column to existing table
    61 v.db.addcolumn map=mygeodetic_pts column="height double precision"
     68# query raster cells (a new column will be added to existing table)
    6269v.what.rast map=mygeodetic_pts raster=elev_state_500m column=height
    6370
Note: See TracChangeset for help on using the changeset viewer.