[GRASS-SVN] r49229 - grass/trunk/vector/v.voronoi

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 14 03:17:24 EST 2011


Author: martinl
Date: 2011-11-14 00:17:23 -0800 (Mon, 14 Nov 2011)
New Revision: 49229

Modified:
   grass/trunk/vector/v.voronoi/defs.h
   grass/trunk/vector/v.voronoi/main.c
   grass/trunk/vector/v.voronoi/sw_main.c
Log:
v.voronoi: clean up
	   check for points/centroids
	   use Vect_read_next_line() - major speed for OGR access


Modified: grass/trunk/vector/v.voronoi/defs.h
===================================================================
--- grass/trunk/vector/v.voronoi/defs.h	2011-11-14 08:16:12 UTC (rev 49228)
+++ grass/trunk/vector/v.voronoi/defs.h	2011-11-14 08:17:23 UTC (rev 49229)
@@ -4,3 +4,4 @@
 extern struct Map_info In, Out;
 extern int Type;
 extern int All;
+extern int Field;

Modified: grass/trunk/vector/v.voronoi/main.c
===================================================================
--- grass/trunk/vector/v.voronoi/main.c	2011-11-14 08:16:12 UTC (rev 49228)
+++ grass/trunk/vector/v.voronoi/main.c	2011-11-14 08:17:23 UTC (rev 49229)
@@ -9,7 +9,7 @@
  *               Glynn Clements <glynn gclements.plus.com>,  
  *               Markus Neteler <neteler itc.it>
  * PURPOSE:      produce a Voronoi diagram using vector points
- * COPYRIGHT:    (C) 1993-2006 by the GRASS Development Team
+ * COPYRIGHT:    (C) 1993-2006, 2001 by the GRASS Development Team
  *
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -98,16 +98,18 @@
 {
     int i;
     int **cats, *ncats, nfields, *fields;
-    struct Flag *line_flag;
-
-    /* struct Flag *all_flag; */
-    struct Option *in_opt, *out_opt;
-    struct Flag *table_flag;
+    struct {
+	struct Flag *line, *table;
+    } flag;
+    struct {
+	struct Option *in, *out, *field;
+    } opt;
     struct GModule *module;
     struct line_pnts *Points;
     struct line_cats *Cats;
     int node, nnodes;
     COOR *coor;
+    int verbose;
     int ncoor, acoor;
     int line, nlines, type, ctype, area, nareas;
     int err_boundaries, err_centr_out, err_centr_dupl, err_nocentr;
@@ -118,32 +120,27 @@
     G_add_keyword(_("vector"));
     G_add_keyword(_("geometry"));
     G_add_keyword(_("triangulation"));
-    module->description = _("Creates a Voronoi diagram from an input vector "
-			    "map containing points or centroids.");
+    module->description = _("Creates a Voronoi diagram in current region from "
+			    "an input vector map containing points or centroids.");
 
-    in_opt = G_define_standard_option(G_OPT_V_INPUT);
-    out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
+    opt.in = G_define_standard_option(G_OPT_V_INPUT);
+    opt.in->label = _("Name of input vector point map");
+    
+    opt.field = G_define_standard_option(G_OPT_V_FIELD_ALL);
+    
+    opt.out = G_define_standard_option(G_OPT_V_OUTPUT);
 
-    /*
-       all_flag = G_define_flag ();
-       all_flag->key = 'a';
-       all_flag->description = _("Use all points (do not limit to current region)");
-     */
-
-    line_flag = G_define_flag();
-    line_flag->key = 'l';
-    line_flag->description =
+    flag.line = G_define_flag();
+    flag.line->key = 'l';
+    flag.line->description =
 	_("Output tessellation as a graph (lines), not areas");
 
-    table_flag = G_define_flag();
-    table_flag->key = 't';
-    table_flag->description = _("Do not create attribute table");
-
+    flag.table = G_define_standard_flag(G_FLG_DB_TABLE);
+    
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
-
-
-    if (line_flag->answer)
+    
+    if (flag.line->answer)
 	Type = GV_LINE;
     else
 	Type = GV_BOUNDARY;
@@ -155,14 +152,10 @@
 
     /* open files */
     Vect_set_open_level(2);
-    Vect_open_old(&In, in_opt->answer, "");
+    Vect_open_old2(&In, opt.in->answer, "", opt.field->answer);
 
-    if (Vect_open_new(&Out, out_opt->answer, 0) < 0)
-	G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);
-
-    Vect_hist_copy(&In, &Out);
-    Vect_hist_command(&Out);
-
+    Field = Vect_get_field_number(&In, opt.field->answer);
+    
     /* initialize working region */
     G_get_window(&Window);
     Vect_region_box(&Window, &Box);
@@ -171,9 +164,14 @@
 
     freeinit(&sfl, sizeof(struct Site));
 
-    G_message(_("Reading sites..."));
+    G_message(_("Reading features..."));
     readsites();
 
+    Vect_open_new(&Out, opt.out->answer, 0);
+
+    Vect_hist_copy(&In, &Out);
+    Vect_hist_command(&Out);
+    
     siteidx = 0;
     geominit();
 
@@ -181,12 +179,15 @@
     plot = 0;
     debug = 0;
 
-    G_message(_("Voronoi triangulation..."));
+    G_message(_("Processing Voronoi triangulation..."));
     voronoi(triangulate, nextone);
 
     /* Close free ends by current region */
+    verbose = G_verbose();
+    G_set_verbose(0);
     Vect_build_partial(&Out, GV_BUILD_BASE);
-
+    G_set_verbose(verbose);
+    
     ncoor = 0;
     acoor = 100;
     coor = (COOR *) G_malloc(sizeof(COOR) * acoor);
@@ -256,14 +257,14 @@
 	fields[i] = Vect_cidx_get_field_number(&In, i);
     }
 
-    if (line_flag->answer)
+    if (flag.line->answer)
 	ctype = GV_POINT;
     else
 	ctype = GV_CENTROID;
 
     nlines = Vect_get_num_lines(&In);
 
-    G_message(_("Writing sites to output..."));
+    G_important_message(_("Writing features..."));
 
     for (line = 1; line <= nlines; line++) {
 
@@ -291,13 +292,12 @@
 	    }
 	    if (f > -1) {
 		cats[f][ncats[f]] = Cats->cat[i];
-		ncats[f]++;
-	    }
+		ncats[f]++;	    }
 	}
     }
 
     /* Copy tables */
-    if (!(table_flag->answer)) {
+    if (!(flag.table->answer)) {
 	int ttype, ntabs = 0;
 	struct field_info *IFi, *OFi;
 
@@ -325,13 +325,14 @@
 	else
 	    ttype = GV_1TABLE;
 
+	G_message(_("Writing attributes..."));
 	for (i = 0; i < nfields; i++) {
 	    int ret;
 
 	    if (fields[i] == 0)
 		continue;
 
-	    G_message(_("Layer %d"), fields[i]);
+	    G_debug(1, "Layer %d", fields[i]);
 
 	    /* Make a list of categories */
 	    IFi = Vect_get_field(&In, fields[i]);
@@ -364,7 +365,9 @@
     Vect_close(&In);
 
     /* cleaning part 1: count errors */
+    G_set_verbose(0);
     Vect_build_partial(&Out, GV_BUILD_CENTROIDS);
+    G_set_verbose(verbose);
     err_boundaries = err_centr_out = err_centr_dupl = err_nocentr = 0;
     nlines = Vect_get_num_lines(&Out);
     for (line = 1; line <= nlines; line++) {

Modified: grass/trunk/vector/v.voronoi/sw_main.c
===================================================================
--- grass/trunk/vector/v.voronoi/sw_main.c	2011-11-14 08:16:12 UTC (rev 49228)
+++ grass/trunk/vector/v.voronoi/sw_main.c	2011-11-14 08:17:23 UTC (rev 49229)
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <grass/gis.h>
 #include <grass/vector.h>
+#include <grass/glocale.h>
 #include "sw_defs.h"
 #include "defs.h"
 
@@ -31,6 +32,7 @@
 struct Map_info In, Out;
 int Type;
 int All;
+int Field;
 
 /* sort sites on y, then x, coord */
 int scomp(const void *v1, const void *v2)
@@ -67,10 +69,8 @@
 void removeDuplicates()
 {
     int i, j;
-    int foundDupe;
 
     i = j = 1;
-    foundDupe = 0;
     while (i < nsites)
 	if (mode3d) {
 	    if (sites[i].coord.x == sites[i - 1].coord.x &&
@@ -106,25 +106,28 @@
 /* read all sites, sort, and compute xmin, xmax, ymin, ymax */
 int readsites(void)
 {
-    int nlines, line;
+    int nlines, ltype;
     struct line_pnts *Points;
-
+    struct line_cats *Cats;
+    
     Points = Vect_new_line_struct();
+    Cats = Vect_new_cats_struct();
+    
+    nlines = Vect_get_num_primitives(&In, GV_POINTS);
 
-    nlines = Vect_get_num_lines(&In);
-
     nsites = 0;
     sites = (struct Site *)G_malloc(nlines * sizeof(struct Site));
 
-    for (line = 1; line <= nlines; line++) {
-	int type;
+    Vect_set_constraint_type(&In, GV_POINTS);
+    Vect_set_constraint_field(&In, Field);
+    
+    while(TRUE) {
+	ltype = Vect_read_next_line(&In, Points, Cats);
+	if(ltype == -2)
+	    break;
 
-	G_percent(line, nlines, 2);
-
-	type = Vect_read_line(&In, Points, NULL, line);
-	if (!(type & GV_POINTS))
-	    continue;
-
+	G_percent(Vect_get_next_line_id(&In), nlines, 2);
+		
 	if (!All) {
 	    if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
 		continue;
@@ -156,6 +159,13 @@
 
 	nsites++;
     }
+
+    if(nsites < 1) {
+	const char *name = Vect_get_full_name(&In);
+	Vect_close(&In);
+	G_fatal_error(_("No points/centroids found in <%s>"), name);
+    }
+
     if (nsites < nlines)
 	sites =
 	    (struct Site *)G_realloc(sites,
@@ -173,6 +183,10 @@
 
     qsort(sites, nsites, sizeof(struct Site), scomp);
     removeDuplicates();
+
+    Vect_destroy_line_struct(Points);
+    Vect_destroy_cats_struct(Cats);
+
     return 0;
 }
 



More information about the grass-commit mailing list