[GRASS-SVN] r67007 - grass/trunk/vector/v.db.select

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 2 07:47:13 PST 2015


Author: lucadelu
Date: 2015-12-02 07:47:13 -0800 (Wed, 02 Dec 2015)
New Revision: 67007

Modified:
   grass/trunk/vector/v.db.select/main.c
   grass/trunk/vector/v.db.select/v.db.select.html
Log:
v.db.select: added group option to use GROUP BY SQL clause

Modified: grass/trunk/vector/v.db.select/main.c
===================================================================
--- grass/trunk/vector/v.db.select/main.c	2015-12-02 10:49:00 UTC (rev 67006)
+++ grass/trunk/vector/v.db.select/main.c	2015-12-02 15:47:13 UTC (rev 67007)
@@ -2,13 +2,14 @@
 /***************************************************************
  *
  * MODULE:       v.db.select
- * 
+ *
  * AUTHOR(S):    Radim Blazek
  *               OGR support by Martin Landa <landa.martin gmail.com>
  *               -f flag by Huidae Cho <grass4u gmail.com>
- *               
+ *               group option by Luca Delucchi <lucadeluge gmail.com>
+ *
  * PURPOSE:      Print vector attributes
- *               
+ *
  * COPYRIGHT:    (C) 2005-2009, 2011-2014 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
@@ -32,7 +33,7 @@
 {
     struct GModule *module;
     struct Option *map_opt, *field_opt, *fs_opt, *vs_opt, *nv_opt, *col_opt,
-	*where_opt, *file_opt;
+	*where_opt, *file_opt, *group_opt;
     struct Flag *c_flag, *v_flag, *r_flag, *f_flag;
     dbDriver *driver;
     dbString sql, value_string;
@@ -63,10 +64,16 @@
 
     col_opt = G_define_standard_option(G_OPT_DB_COLUMNS);
     col_opt->guisection = _("Selection");
-    
+
     where_opt = G_define_standard_option(G_OPT_DB_WHERE);
     where_opt->guisection = _("Selection");
-    
+
+    group_opt = G_define_option();
+    group_opt->key = "group";
+    group_opt->required = NO;
+    group_opt->description = _("GROUP BY conditions of SQL statement without 'group by' keyword");
+    group_opt->guisection = _("Selection");
+
     fs_opt = G_define_standard_option(G_OPT_F_SEP);
     fs_opt->guisection = _("Format");
 
@@ -79,12 +86,12 @@
     nv_opt = G_define_standard_option(G_OPT_M_NULL_VALUE);
     nv_opt->guisection = _("Format");
 
-    file_opt = G_define_standard_option(G_OPT_F_OUTPUT); 
+    file_opt = G_define_standard_option(G_OPT_F_OUTPUT);
     file_opt->key = "file";
-    file_opt->required = NO; 
-    file_opt->description = 
-	_("Name for output file (if omitted or \"-\" output to stdout)"); 
-    
+    file_opt->required = NO;
+    file_opt->description =
+	_("Name for output file (if omitted or \"-\" output to stdout)");
+
     r_flag = G_define_flag();
     r_flag->key = 'r';
     r_flag->description =
@@ -112,12 +119,12 @@
 	exit(EXIT_FAILURE);
 
     /* set input vector map name and mapset */
-    if (file_opt->answer && strcmp(file_opt->answer, "-") != 0) { 
-	if (NULL == freopen(file_opt->answer, "w", stdout)) { 
-	    G_fatal_error(_("Unable to open file <%s> for writing"), file_opt->answer); 
-	} 
-    } 
-    
+    if (file_opt->answer && strcmp(file_opt->answer, "-") != 0) {
+	if (NULL == freopen(file_opt->answer, "w", stdout)) {
+	    G_fatal_error(_("Unable to open file <%s> for writing"), file_opt->answer);
+	}
+    }
+
     min_box = line_box = NULL;
     list_lines = NULL;
 
@@ -137,7 +144,7 @@
         vs = G_option_to_separator(vs_opt);
     else
         vs = NULL;
-    
+
     db_init_string(&sql);
     db_init_string(&value_string);
 
@@ -186,6 +193,15 @@
 	G_free(buf);
     }
 
+    if (group_opt->answer) {
+        char *buf = NULL;
+
+        buf = G_malloc((strlen(group_opt->answer) + 8));
+        sprintf(buf, " GROUP BY %s", group_opt->answer);
+        db_append_string(&sql, buf);
+        G_free(buf);
+    }
+
     if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK)
 	G_fatal_error(_("Unable to open select cursor"));
 

Modified: grass/trunk/vector/v.db.select/v.db.select.html
===================================================================
--- grass/trunk/vector/v.db.select/v.db.select.html	2015-12-02 10:49:00 UTC (rev 67006)
+++ grass/trunk/vector/v.db.select/v.db.select.html	2015-12-02 15:47:13 UTC (rev 67007)
@@ -65,6 +65,67 @@
 ...
 </pre></div>
 
+<h3>Select features with distinct road names</h3>
+<div class="code"><pre>
+v.db.select map=roadsmajor columns=ROAD_NAME group=ROAD_NAME
+ROAD_NAME
+
+I-40
+I-440
+I-540
+NC-231
+NC-39
+NC-42
+...
+</pre></div>
+
+It is also possible to combine with <em>where</em> option
+<div class="code"><pre>
+v.db.select map=roadsmajor columns=ROAD_NAME,MULTILANE group=ROAD_NAME where='ROAD_NAME is not null'
+ROAD_NAME|MULTILANE
+I-40|yes
+I-440|yes
+I-540|yes
+NC-231|no
+NC-39|no
+NC-42|no
+NC-50|no
+NC-54|no
+NC-55|no
+NC-96|no
+NC-97|no
+NC-98|no
+US-1|
+US-401|no
+US-64|yes
+US-70|yes
+</pre></div>
+
+It can also use more columns in <em>group</em> option
+<div class="code"><pre>
+v.db.select map=roadsmajor columns=ROAD_NAME,MULTILANE group=ROAD_NAME,MULTILANE where='ROAD_NAME is not null'
+ROAD_NAME|MULTILANE
+I-40|yes
+I-440|yes
+I-540|yes
+NC-231|no
+NC-39|no
+NC-42|no
+NC-50|no
+NC-54|no
+NC-55|no
+NC-96|no
+NC-97|no
+NC-98|no
+US-1|
+US-1|yes
+US-401|no
+US-401|yes
+US-64|yes
+US-70|yes
+</pre></div>
+
+
 <h2>SEE ALSO</h2>
 
 <em>
@@ -75,6 +136,8 @@
 
 Radim Blazek, ITC-Irst, Trento, Italy<br>
 Minimal region extent added by Martin Landa,
-FBK-irst (formerly ITC-irst), Trento, Italy (2008/08)
+FBK-irst (formerly ITC-irst), Trento, Italy (2008/08)<br>
+Group option added by Luca Delucchi,
+Fondazione Edmund Mach, Trento, Italy (2015/12)
 
 <p><i>Last changed: $Date$</i>



More information about the grass-commit mailing list