[GRASS-SVN] r58214 - grass/trunk/vector/v.what.rast
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Nov 13 19:16:22 PST 2013
Author: hamish
Date: 2013-11-13 19:16:22 -0800 (Wed, 13 Nov 2013)
New Revision: 58214
Modified:
grass/trunk/vector/v.what.rast/main.c
grass/trunk/vector/v.what.rast/v.what.rast.html
Log:
add flag to allow printing to stdout
Modified: grass/trunk/vector/v.what.rast/main.c
===================================================================
--- grass/trunk/vector/v.what.rast/main.c 2013-11-14 02:19:23 UTC (rev 58213)
+++ grass/trunk/vector/v.what.rast/main.c 2013-11-14 03:16:22 UTC (rev 58214)
@@ -2,12 +2,14 @@
*
* MODULE: v.what.rast
*
- * AUTHOR(S): Radim Blazek (using r.what)
- * Michael Shapiro, U.S. Army Construction Engineering Research Laboratory (r.what)
- *
+ * AUTHOR(S): Radim Blazek (adapted from r.what)
+ * Michael Shapiro, U.S. Army Construction Engineering
+ * Research Laboratory (r.what)
+ * Hamish Bowman, University of Otago, NZ (interpolation)
+ *
* PURPOSE: Query raster map
*
- * COPYRIGHT: (C) 2001, 2011 by the GRASS Development Team
+ * COPYRIGHT: (C) 2001-2013 by the GRASS Development Team
*
* This program is free software under the GNU General
* Public License (>=v2). Read the file COPYING that
@@ -43,6 +45,7 @@
{
struct Option *vect, *rast, *field, *col, *where;
} opt;
+ struct Flag *print_flag;
int Cache_size;
struct order *cache;
int cur_row;
@@ -87,19 +90,28 @@
opt.rast->description = _("Name of existing raster map to be queried");
opt.col = G_define_standard_option(G_OPT_DB_COLUMN);
- opt.col->required = YES;
+ opt.col->required = NO; /* YES, but suppress_required only for this option */
opt.col->description =
_("Name of attribute column to be updated with the query result");
opt.where = G_define_standard_option(G_OPT_DB_WHERE);
+ print_flag = G_define_flag();
+ print_flag->key = 'p';
+ print_flag->description =
+ _("Print categories and values instead of updating the database");
+
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
+
db_init_string(&stmt);
Points = Vect_new_line_struct();
Cats = Vect_new_cats_struct();
+ if (!print_flag->answer && !opt.col->answer)
+ G_fatal_error(_("Required parameter <%s> not set"), opt.col->key);
+
G_get_window(&window);
Vect_region_box(&window, &box); /* T and B set to +/- PORT_DOUBLE_MAX */
@@ -109,6 +121,7 @@
field = Vect_get_field_number(&Map, opt.field->answer);
+ /* FIXME: if print flag is used then a database doesn't need to exist */
Fi = Vect_get_field(&Map, field);
if (Fi == NULL)
G_fatal_error(_("Database connection not defined for layer %d"),
@@ -136,20 +149,22 @@
G_fatal_error ( "Cannot read category file");
*/
- /* Check column type */
- col_type = db_column_Ctype(driver, Fi->table, opt.col->answer);
+ 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)
+ G_fatal_error(_("Column <%s> not found"), opt.col->answer);
- 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!!"));
+ }
/* Read vector points to cache */
Cache_size = Vect_get_num_primitives(&Map, GV_POINTS);
@@ -216,6 +231,7 @@
cache[++i] = cache[j];
else
cache[i].count++;
+
point_cnt = i + 1;
G_debug(1, "%d vector points left after removal of duplicates",
@@ -264,98 +280,136 @@
} /* point loop */
Rast_close(fd);
- /* Update table from cache */
- G_debug(1, "Updating db table");
- /* select existing categories to array (array is sorted) */
- select = db_select_int(driver, Fi->table, Fi->key, NULL, &catexst);
+ if (print_flag->answer) {
+ dupl_cnt = 0;
- db_begin_transaction(driver);
+ G_message("%s|value", Fi->key);
- norec_cnt = update_cnt = upderr_cnt = dupl_cnt = 0;
+ for (point = 0; point < point_cnt; point++) {
+ if (cache[point].count > 1) {
+ G_warning(_("Multiple points (%d) of category %d, value set to 'NULL'"),
+ cache[point].count, cache[point].cat); /* TODO: improve message */
+ dupl_cnt++;
+ }
- G_message("Update vector attributes...");
- for (point = 0; point < point_cnt; point++) {
- if (cache[point].count > 1) {
- G_warning(_("More points (%d) of category %d, value set to 'NULL'"),
- cache[point].count, cache[point].cat);
- dupl_cnt++;
+ fprintf(stdout, "%d|", cache[point].cat);
+
+ if (out_type == CELL_TYPE) {
+ if (cache[point].count > 1 ||
+ Rast_is_c_null_value(&cache[point].value)) {
+ fprintf(stdout, "*");
+ }
+ else
+ fprintf(stdout, "%d", cache[point].value);
+ }
+ else { /* FCELL or DCELL */
+ if (cache[point].count > 1 ||
+ Rast_is_d_null_value(&cache[point].dvalue)) {
+ fprintf(stdout, "*");
+ }
+ else
+ fprintf(stdout, "%.*g", width, cache[point].dvalue);
+ }
+ fprintf(stdout, "\n");
}
+ }
+ else {
+ /* Update table from cache */
+ G_debug(1, "Updating db table");
- G_percent(point, point_cnt, 2);
+ /* select existing categories to array (array is sorted) */
+ select = db_select_int(driver, Fi->table, Fi->key, NULL, &catexst);
- /* category exist in DB ? */
- cex =
- (int *)bsearch((void *)&(cache[point].cat), catexst, select,
- sizeof(int), srch_cat);
- if (cex == NULL) { /* cat does not exist in DB */
- norec_cnt++;
- G_warning(_("No record for category %d in table <%s>"),
- cache[point].cat, Fi->table);
- continue;
- }
+ db_begin_transaction(driver);
- sprintf(buf, "update %s set %s = ", Fi->table, opt.col->answer);
+ norec_cnt = update_cnt = upderr_cnt = dupl_cnt = 0;
- db_set_string(&stmt, buf);
+ G_message("Update vector attributes...");
+ for (point = 0; point < point_cnt; point++) {
+ if (cache[point].count > 1) {
+ G_warning(_("Multiple points (%d) of category %d, value set to 'NULL'"),
+ cache[point].count, cache[point].cat);
+ dupl_cnt++;
+ }
- if (out_type == CELL_TYPE) {
- if (cache[point].count > 1 ||
- Rast_is_c_null_value(&cache[point].value)) {
- sprintf(buf, "NULL");
+ G_percent(point, point_cnt, 2);
+
+ /* category exist in DB ? */
+ cex =
+ (int *)bsearch((void *)&(cache[point].cat), catexst, select,
+ sizeof(int), srch_cat);
+ if (cex == NULL) { /* cat does not exist in DB */
+ norec_cnt++;
+ G_warning(_("No record for category %d in table <%s>"),
+ cache[point].cat, Fi->table);
+ continue;
}
- else {
- sprintf(buf, "%d ", cache[point].value);
+
+ sprintf(buf, "update %s set %s = ", Fi->table, opt.col->answer);
+
+ db_set_string(&stmt, buf);
+
+ if (out_type == CELL_TYPE) {
+ if (cache[point].count > 1 ||
+ Rast_is_c_null_value(&cache[point].value)) {
+ sprintf(buf, "NULL");
+ }
+ else
+ sprintf(buf, "%d ", cache[point].value);
}
- }
- else { /* FCELL or DCELL */
- if (cache[point].count > 1 ||
- Rast_is_d_null_value(&cache[point].dvalue)) {
- sprintf(buf, "NULL");
+ else { /* FCELL or DCELL */
+ if (cache[point].count > 1 ||
+ Rast_is_d_null_value(&cache[point].dvalue)) {
+ sprintf(buf, "NULL");
+ }
+ else
+ sprintf(buf, "%.*g", width, cache[point].dvalue);
}
+ db_append_string(&stmt, buf);
+
+ sprintf(buf, " where %s = %d", Fi->key, cache[point].cat);
+ db_append_string(&stmt, buf);
+ /* user provides where condition: */
+ if (opt.where->answer) {
+ sprintf(buf, " AND %s", opt.where->answer);
+ db_append_string(&stmt, buf);
+ }
+ G_debug(3, db_get_string(&stmt));
+
+ /* Update table */
+ if (db_execute_immediate(driver, &stmt) == DB_OK) {
+ update_cnt++;
+ }
else {
- sprintf(buf, "%.*g", width, cache[point].dvalue);
+ upderr_cnt++;
}
}
- db_append_string(&stmt, buf);
+ G_percent(1, 1, 1);
- sprintf(buf, " where %s = %d", Fi->key, cache[point].cat);
- db_append_string(&stmt, buf);
- /* user provides where condition: */
- if (opt.where->answer) {
- sprintf(buf, " AND %s", opt.where->answer);
- db_append_string(&stmt, buf);
- }
- G_debug(3, db_get_string(&stmt));
+ G_debug(1, "Committing DB transaction");
+ db_commit_transaction(driver);
- /* Update table */
- if (db_execute_immediate(driver, &stmt) == DB_OK) {
- update_cnt++;
- }
- else {
- upderr_cnt++;
- }
+ G_free(catexst);
+ db_close_database_shutdown_driver(driver);
+ db_free_string(&stmt);
}
- G_percent(1, 1, 1);
- G_debug(1, "Committing DB transaction");
- db_commit_transaction(driver);
-
- G_free(catexst);
- db_close_database_shutdown_driver(driver);
- db_free_string(&stmt);
-
/* Report */
G_verbose_message(_("%d categories loaded from table"), select);
G_verbose_message(_("%d categories loaded from vector"), point_cnt);
- G_verbose_message(_("%d categories from vector missing in table"),
- norec_cnt);
+ if (!print_flag->answer)
+ G_verbose_message(_("%d categories from vector missing in table"),
+ norec_cnt);
if (dupl_cnt > 0)
G_message(_("%d duplicate categories in vector"), dupl_cnt);
- if (upderr_cnt > 0)
- G_warning(_("%d update errors"), upderr_cnt);
- G_done_msg(_("%d records updated."), update_cnt);
+ if (!print_flag->answer) {
+ if (upderr_cnt > 0)
+ G_warning(_("%d update errors"), upderr_cnt);
+ G_done_msg(_("%d records updated."), update_cnt);
+ }
+
exit(EXIT_SUCCESS);
}
Modified: grass/trunk/vector/v.what.rast/v.what.rast.html
===================================================================
--- grass/trunk/vector/v.what.rast/v.what.rast.html 2013-11-14 02:19:23 UTC (rev 58213)
+++ grass/trunk/vector/v.what.rast/v.what.rast.html 2013-11-14 03:16:22 UTC (rev 58214)
@@ -1,17 +1,28 @@
<h2>DESCRIPTION</h2>
-<em>v.what.rast</em> reads raster value for each point in the vector and updates <b>col</b>
-column in vector attribute table by this value. The column should be type
-number (integer, float, double, ... ).
+<em>v.what.rast</em> reads the raster value for each point in the vector map
+and updates <b>column</b> in the vector attribute table by this value. The
+column should be type number (integer, float, double, ... ).
<br>
-If more points have the same category, attribute value is set to NULL.
-If raster values is NULL, attribute value is set to NULL.
+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>
+If the <b>-p</b> flag is used, then the attribute table is not updated
+and the results are printed to <tt>stdout</tt>.
+
<h2>NOTES</h2>
<em>v.what.rast</em> operates on the attribute table. To modify the vector
geometry instead, use <em>v.drape</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
+(<tt>paste -d'|'</tt>).
+
<h2>EXAMPLES</h2>
A) Reading values from raster map at position of vector points, writing these values
@@ -44,6 +55,7 @@
v.univar map=pnts column=heights type=point
</pre></div>
+
<h2>SEE ALSO</h2>
<em>
@@ -55,6 +67,7 @@
<a href="v.what.vect.html">v.what.vect</a>
</em>
+
<h2>AUTHOR</h2>
Radim Blazek
More information about the grass-commit
mailing list