[GRASS-SVN] r53891 - grass/trunk/vector/v.mkgrid

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Nov 18 04:40:34 PST 2012


Author: marisn
Date: 2012-11-18 04:40:34 -0800 (Sun, 18 Nov 2012)
New Revision: 53891

Modified:
   grass/trunk/vector/v.mkgrid/main.c
Log:
Make some errors more fatal; Write attribute data in batches per 20 to speed up module 16x times

Modified: grass/trunk/vector/v.mkgrid/main.c
===================================================================
--- grass/trunk/vector/v.mkgrid/main.c	2012-11-18 12:35:58 UTC (rev 53890)
+++ grass/trunk/vector/v.mkgrid/main.c	2012-11-18 12:40:34 UTC (rev 53891)
@@ -28,6 +28,9 @@
 #include "grid_structs.h"
 #include "local_proto.h"
 
+/* Write attribute data in batches per 20 */
+#define BATCH_SIZE 20
+
 int main(int argc, char *argv[])
 {
 
@@ -36,7 +39,9 @@
 
     /* store filename and path  */
     char *dig_file;
+    
     char buf[2000];
+    int batch_fill;
 
     /* Other local variables */
     int attCount, nbreaks;
@@ -64,15 +69,14 @@
     module = G_define_module();
     G_add_keyword(_("vector"));
     G_add_keyword(_("geometry"));
-    module->description =
-	_("Creates a vector map of a user-defined grid.");
+    module->description = _("Creates a vector map of a user-defined grid.");
 
     vectname = G_define_standard_option(G_OPT_V_OUTPUT);
     vectname->key = "map";
 
     grid = G_define_option();
     grid->key = "grid";
-    grid->key_desc = "rows,columns";
+    grid->key_desc = _("rows,columns");
     grid->type = TYPE_INTEGER;
     grid->required = YES;
     grid->multiple = NO;
@@ -88,9 +92,9 @@
     position_opt->description = _("Where to place the grid");
     desc = NULL;
     G_asprintf(&desc,
-	       "region;%s;coor;%s",
-	       _("current region"),
-	       _("use 'coor' and 'box' options"));
+            "region;%s;coor;%s",
+            _("current region"),
+            _("use 'coor' and 'box' options"));
     position_opt->descriptions = desc;
 
     coord = G_define_option();
@@ -104,7 +108,7 @@
 
     box = G_define_option();
     box->key = "box";
-    box->key_desc = "width,height";
+    box->key_desc = _("width,height");
     box->type = TYPE_DOUBLE;
     box->required = NO;
     box->multiple = NO;
@@ -124,10 +128,10 @@
     breaks->required = NO;
     breaks->description =
 	_("Number of horizontal vertex points per grid cell");
-    breaks->options = "3-30";
+    breaks->options = "0-60";
     breaks->answer = "3";
 
-    points_fl = G_define_flag ();
+    points_fl = G_define_flag();
     points_fl->key = 'p';
     points_fl->description =
 	_("Create grid of points instead of areas and centroids");
@@ -158,11 +162,16 @@
     /* Position */
     if (position_opt->answer[0] == 'r') {	/* region */
 	if (coord->answer)
-	    G_warning("'coor' option ignored with 'position=region'");
+	    G_fatal_error(_("'coor' and 'position=region' are exclusive options"));
 
 	if (box->answer)
-	    G_warning("'box' option ignored with 'position=region'");
+	    G_fatal_error(_("'box' and 'position=region' are exclusive options"));
 
+	if (grid_info.angle != 0.0) {
+	    G_fatal_error(_("'angle' and 'position=region' are exclusive options"));
+	    grid_info.angle = 0.0;
+	}
+
 	grid_info.origin_x = window.west;
 	grid_info.origin_y = window.south;
 
@@ -171,11 +180,6 @@
 
 	G_debug(2, "x = %e y = %e l = %e w = %e", grid_info.origin_x,
 		grid_info.origin_y, grid_info.length, grid_info.width);
-
-	if (grid_info.angle != 0.0) {
-	    G_warning("'angle' ignored ");
-	    grid_info.angle = 0.0;
-	}
     }
     else {
 	if (!coord->answer)
@@ -256,7 +260,7 @@
 	G_fatal_error(_("Unable to grant privileges on table <%s>"),
 		      Fi->table);
 
-    if (! points_p) {
+    if (!points_p) {
 	/* create areas */
 	write_grid(&grid_info, &Map, nbreaks);
     }
@@ -266,6 +270,7 @@
 
     /* Write out centroids and attributes */
     attCount = 0;
+    batch_fill = 0;
     for (i = 0; i < grid_info.num_rows; ++i) {
 	for (j = 0; j < grid_info.num_cols; ++j) {
 	    double x, y;
@@ -284,28 +289,52 @@
 	    Vect_cat_set(Cats, 1, attCount + 1);
 	    Vect_write_line(&Map, point_type, Points, Cats);
 
-	    if (grid_info.num_rows < 27 && grid_info.num_cols < 27) {
+            if (batch_fill == 0) {
+                sprintf(buf, "insert into %s values ", Fi->table);
+                if (db_set_string(&sql, buf) != DB_OK)
+                    G_fatal_error(_("Unable to fill attribute table"));
+            }
+            
+            if (batch_fill > 0)
+                if (db_append_string(&sql, ", ") != DB_OK)
+                    G_fatal_error(_("Unable to fill attribute table"));
+            
+            if (grid_info.num_rows < 27 && grid_info.num_cols < 27) {
 		sprintf(buf,
-			"insert into %s values ( %d, %d, %d, '%c', '%c' )",
-			Fi->table, attCount + 1, grid_info.num_rows - i,
+			"( %d, %d, %d, '%c', '%c' )",
+			attCount + 1, grid_info.num_rows - i,
 			j + 1, 'A' + grid_info.num_rows - i - 1, 'A' + j);
 	    }
 	    else {
-		sprintf(buf, "insert into %s values ( %d, %d, %d )",
-			Fi->table, attCount + 1, i + 1, j + 1);
+		sprintf(buf, "( %d, %d, %d )",
+			attCount + 1, i + 1, j + 1);
 	    }
-	    db_set_string(&sql, buf);
+            if (db_append_string(&sql, buf) != DB_OK)
+                    G_fatal_error(_("Unable to fill attribute table"));
+	    batch_fill++;
+	    
+            if (batch_fill == BATCH_SIZE) {
+                G_debug(3, "SQL: %s", db_get_string(&sql));
 
-	    G_debug(3, "SQL: %s", db_get_string(&sql));
-
-	    if (db_execute_immediate(Driver, &sql) != DB_OK) {
-		G_fatal_error(_("Unable to insert new record: %s"),
-			      db_get_string(&sql));
-	    }
+                if (db_execute_immediate(Driver, &sql) != DB_OK) {
+                    G_fatal_error(_("Unable to insert new record: %s"),
+                                db_get_string(&sql));
+                }
+                batch_fill = 0;
+            }
 	    attCount++;
 	}
     }
+    
+    if (batch_fill > 0) {
+        G_debug(3, "SQL: %s", db_get_string(&sql));
 
+        if (db_execute_immediate(Driver, &sql) != DB_OK) {
+            G_fatal_error(_("Unable to insert new record: %s"),
+                db_get_string(&sql));
+        }
+    }
+
     db_close_database_shutdown_driver(Driver);
 
     Vect_build(&Map);



More information about the grass-commit mailing list