[GRASS-SVN] r61675 - grass/branches/releasebranch_6_4/vector/v.mkgrid

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Aug 18 07:25:39 PDT 2014


Author: neteler
Date: 2014-08-18 07:25:39 -0700 (Mon, 18 Aug 2014)
New Revision: 61675

Modified:
   grass/branches/releasebranch_6_4/vector/v.mkgrid/description.html
   grass/branches/releasebranch_6_4/vector/v.mkgrid/local_proto.h
   grass/branches/releasebranch_6_4/vector/v.mkgrid/main.c
   grass/branches/releasebranch_6_4/vector/v.mkgrid/write_grid.c
Log:
v.mkgrid: added support for line output through -l flag (trunk, r61519)

Modified: grass/branches/releasebranch_6_4/vector/v.mkgrid/description.html
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.mkgrid/description.html	2014-08-18 14:25:03 UTC (rev 61674)
+++ grass/branches/releasebranch_6_4/vector/v.mkgrid/description.html	2014-08-18 14:25:39 UTC (rev 61675)
@@ -15,8 +15,32 @@
 
 <h2>EXAMPLES</h2>
 
-Make a 4x3 grid, cells 20km a side, with lower left corner at 2716500,6447000:
+1) Create a grid in a latitude-longitude location (WGS84):
+
 <div class="code"><pre>
+# use g.region to easily calculate rows and column for 'grid':
+g.region n=90 s=-90 w=-180 e=180 res=10 -p
+projection: 3 (Latitude-Longitude)
+zone:       0
+datum:      wgs84
+ellipsoid:  wgs84
+north:      90N
+south:      90S
+west:       180W
+east:       180E
+nsres:      10
+ewres:      10
+rows:       18
+cols:       36
+cells:      648
+
+# create 10 degree size grid:
+v.mkgrid map=grid_10deg grid=18,36
+</pre></div>
+
+<p>
+2) Make a 4x3 grid, cells 20km a side, with lower left corner at 2716500,6447000:
+<div class="code"><pre>
 v.mkgrid map=coro_grid grid=4,3 position=coor coor=2716500,6447000 box=20000,20000
 </pre></div>
 

Modified: grass/branches/releasebranch_6_4/vector/v.mkgrid/local_proto.h
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.mkgrid/local_proto.h	2014-08-18 14:25:03 UTC (rev 61674)
+++ grass/branches/releasebranch_6_4/vector/v.mkgrid/local_proto.h	2014-08-18 14:25:39 UTC (rev 61675)
@@ -2,6 +2,6 @@
 void rotate(double *, double *, double, double, double);
 
 /* write_grid.c */
-int write_grid(struct grid_description *, struct Map_info *, int);
+int write_grid(struct grid_description *, struct Map_info *, int, int);
 int write_vect(double, double, double, double, struct Map_info *,
-	       struct line_pnts *);
+	       struct line_pnts *, int);

Modified: grass/branches/releasebranch_6_4/vector/v.mkgrid/main.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.mkgrid/main.c	2014-08-18 14:25:03 UTC (rev 61674)
+++ grass/branches/releasebranch_6_4/vector/v.mkgrid/main.c	2014-08-18 14:25:39 UTC (rev 61675)
@@ -6,12 +6,14 @@
  *               Upgrade to 5.7 Radim Blazek 10/2004
  *               Hamish Bowman <hamish_b yahoo.com>,
  *               Jachym Cepicky <jachym les-ejk.cz>, Markus Neteler <neteler itc.it>
- * PURPOSE:      
- * COPYRIGHT:    (C) 1999-2007 by the GRASS Development Team
+ *               Ivan Shevlakov: points support -p
+ *               Luca Delucchi: lines support -l, vertical breaks
+ * PURPOSE:
+ * COPYRIGHT:    (C) 1999-2014 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
- *               for details.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  *****************************************************************************/
 
@@ -19,10 +21,12 @@
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
+
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 #include <grass/Vect.h>
 #include <grass/glocale.h>
+
 #include "grid_structs.h"
 #include "local_proto.h"
 
@@ -43,10 +47,10 @@
     struct Cell_head window;
     struct Map_info Map;
     struct Option *vectname, *grid, *coord, *box, *angle, *position_opt, *breaks;
+    struct GModule *module;
     struct Flag *q;
-    struct Flag *points_fl;
-    int points_p;
-    struct GModule *module;
+    struct Flag *points_fl, *line_fl;
+    int points_p, line_p, output_type;
 
     struct line_pnts *Points;
     struct line_cats *Cats;
@@ -69,7 +73,7 @@
 
     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;
@@ -97,7 +101,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;
@@ -116,16 +120,20 @@
     breaks->type = TYPE_INTEGER;
     breaks->required = NO;
     breaks->description =
-	_("Number of horizontal vertex points per grid cell");
-    breaks->options = "3-30";
+	_("Number of vertex points per grid cell");
+    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");
 
-    /* please, remove before GRASS 7 released */
+    line_fl = G_define_flag();
+    line_fl->key = 'l';
+    line_fl->description =
+	_("Create grid as lines, instead of areas");
+
     q = G_define_flag();
     q->key = 'q';
     q->description = _("Quiet; No chatter");
@@ -135,13 +143,18 @@
 	exit(EXIT_FAILURE);
 
 
-    /* please, remove before GRASS 7 released */
     if (q->answer) {
 	putenv("GRASS_VERBOSE=0");
 	G_warning(_("The '-q' flag is superseded and will be removed "
 		    "in future. Please use '--quiet' instead."));
     }
 
+    line_p = line_fl->answer;
+    if (line_p) {
+	output_type = GV_LINE;
+    } else {
+	output_type = GV_BOUNDARY;
+    }
 
     points_p = points_fl->answer;
 
@@ -165,11 +178,14 @@
     /* 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.origin_x = window.west;
 	grid_info.origin_y = window.south;
 
@@ -178,11 +194,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)
@@ -263,55 +274,67 @@
 	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);
+	write_grid(&grid_info, &Map, nbreaks, output_type);
     }
 
     /* Create a grid of label points at the centres of the grid cells */
     G_verbose_message(_("Creating centroids..."));
 
     /* Write out centroids and attributes */
-    attCount = 0;
-    for (i = 0; i < grid_info.num_rows; ++i) {
-	for (j = 0; j < grid_info.num_cols; ++j) {
-	    double x, y;
-	    const int point_type = points_p ? GV_POINT : GV_CENTROID;
+    /* If the output id is lines it skips to add centroids and attributes
+       TODO decide what to write in the attribute table
+     */
+    if (!line_p) {
+      db_begin_transaction(Driver);
+      attCount = 0;
+      for (i = 0; i < grid_info.num_rows; ++i) {
+	  for (j = 0; j < grid_info.num_cols; ++j) {
+	      double x, y;
+	      const int point_type = points_p ? GV_POINT : GV_CENTROID;
 
-	    x = grid_info.origin_x + (0.5 + j) * grid_info.length;
-	    y = grid_info.origin_y + (0.5 + i) * grid_info.width;
+	      x = grid_info.origin_x + (0.5 + j) * grid_info.length;
+	      y = grid_info.origin_y + (0.5 + i) * grid_info.width;
 
-	    rotate(&x, &y, grid_info.origin_x, grid_info.origin_y,
-		   grid_info.angle);
+	      rotate(&x, &y, grid_info.origin_x, grid_info.origin_y,
+		    grid_info.angle);
 
-	    Vect_reset_line(Points);
-	    Vect_reset_cats(Cats);
+	      Vect_reset_line(Points);
+	      Vect_reset_cats(Cats);
 
-	    Vect_append_point(Points, x, y, 0.0);
-	    Vect_cat_set(Cats, 1, attCount + 1);
-	    Vect_write_line(&Map, point_type, Points, Cats);
+	      Vect_append_point(Points, x, y, 0.0);
+	      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) {
-		sprintf(buf,
-			"insert into %s values ( %d, %d, %d, '%c', '%c' )",
-			Fi->table, 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);
-	    }
-	    db_set_string(&sql, buf);
+	      sprintf(buf, "insert into %s values ", Fi->table);
+	      if (db_set_string(&sql, buf) != DB_OK)
+		  G_fatal_error(_("Unable to fill attribute table"));
 
-	    G_debug(3, "SQL: %s", db_get_string(&sql));
+	      if (grid_info.num_rows < 27 && grid_info.num_cols < 27) {
+		  sprintf(buf,
+			  "( %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, "( %d, %d, %d )",
+			  attCount + 1, i + 1, j + 1);
+	      }
+	      if (db_append_string(&sql, buf) != DB_OK)
+		      G_fatal_error(_("Unable to fill attribute table"));
 
-	    if (db_execute_immediate(Driver, &sql) != DB_OK) {
-		G_fatal_error(_("Unable to insert new record: %s"),
+	      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));
-	    }
-	    attCount++;
-	}
+	      }
+	      attCount++;
+	  }
+      }
     }
+    db_commit_transaction(Driver);
 
     db_close_database_shutdown_driver(Driver);
 

Modified: grass/branches/releasebranch_6_4/vector/v.mkgrid/write_grid.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.mkgrid/write_grid.c	2014-08-18 14:25:03 UTC (rev 61674)
+++ grass/branches/releasebranch_6_4/vector/v.mkgrid/write_grid.c	2014-08-18 14:25:39 UTC (rev 61675)
@@ -5,13 +5,13 @@
 #include "grid_structs.h"
 #include "local_proto.h"
 
-int write_grid(struct grid_description *grid_info, struct Map_info *Map, int nbreaks)
+int write_grid(struct grid_description *grid_info, struct Map_info *Map, int nbreaks, int out_type)
 {
 
     int i, k, j;
-    int rows, cols, x_cols;
+    int rows, cols;
     int num_v_rows, num_v_cols;
-    double x, y, x_len;
+    double x, y, x_len, y_len;
     double sx, sy;
     double width, length;
     double next_x, next_y;
@@ -35,12 +35,11 @@
      * to make sure that each section of the grid
      * line is less than half way around the globe
      */
-    x_len = length / (1. * nbreaks);
-    x_cols = cols * (1. * nbreaks);
+     x_len = length / (1. * nbreaks + 1);
+     y_len = width / (1. * nbreaks + 1);
 
     /* write out all the vector lengths (x vectors) of the entire grid  */
     G_verbose_message(_("Writing out vector rows..."));
-
     y = grid_info->origin_y;
     for (i = 0; i < num_v_rows; ++i) {
 	double startx;
@@ -50,8 +49,9 @@
 
 	for (k = 0; k < cols; k++) {
 	    x = startx;
-	    for (j = 0; j < nbreaks; j++) {
-		if (j < nbreaks -1)
+            j = 0;
+	    do {
+		if (j < nbreaks)
 		    next_x = x + x_len;
 		else
 		    next_x = startx + length;
@@ -65,11 +65,12 @@
 		       angle);
 		rotate(&next_x, &dum, grid_info->origin_x,
 		       grid_info->origin_y, angle);
-		write_vect(x, y, next_x, dum, Map, Points);
+		write_vect(x, y, next_x, dum, Map, Points, out_type);
 
 		y = sy;
 		x = next_x = snext_x;
-	    }
+                j++;
+	    } while (j <= nbreaks);
 	    startx += length;
 	}
 	y += width;
@@ -78,27 +79,37 @@
     /* write out all the vector widths (y vectors) of the entire grid  */
     G_verbose_message(_("Writing out vector columns..."));
     x = grid_info->origin_x;
-    for (k = 0; k < num_v_cols; ++k) {
-	y = grid_info->origin_y;
-	G_percent(k, num_v_cols, 2);
+    for (i = 0; i < num_v_cols; ++i) {
+        double starty;
+	starty = grid_info->origin_y;
+	G_percent(i, num_v_cols, 2);
 
-	for (i = 0; i < rows; ++i) {
-	    next_y = y + width;
+	for (k = 0; k < rows; k++) {
+	  y = starty;
+	  j = 0;
+	  do {
+	      if (j < nbreaks)
+		  next_y = y + y_len;
+	      else
+		  next_y = starty + width;
 
-	    sx = x;
-	    sy = y;
-	    snext_y = next_y;
-	    dum = x;
-	    rotate(&x, &y, grid_info->origin_x, grid_info->origin_y, angle);
-	    rotate(&dum, &next_y, grid_info->origin_x, grid_info->origin_y,
-		   angle);
+	      sx = x;
+	      sy = y;
+	      snext_y = next_y;
+	      dum = x;
+	      rotate(&x, &y, grid_info->origin_x, grid_info->origin_y, angle);
+	      rotate(&dum, &next_y, grid_info->origin_x, grid_info->origin_y,
+		    angle);
 
-	    write_vect(x, y, dum, next_y, Map, Points);
+	      write_vect(x, y, dum, next_y, Map, Points, out_type);
 
-	    x = sx;
-	    y = next_y = snext_y;
+	      x = sx;
+	      y = next_y = snext_y;
+	      j++;
+	  } while (j <= nbreaks);
+	  /* To get exactly the same coordinates as above, y+=width is wrong */
+	  starty += width;
 	}
-	/* To get exactly the same coordinates as above, x+=length is wrong */
 	x += length;
     }
 
@@ -114,7 +125,7 @@
 #define  NUM_POINTS  2
 
 int write_vect(double x1, double y1, double x2, double y2,
-	       struct Map_info *Map, struct line_pnts *Points) /* new with Vlib */
+	       struct Map_info *Map, struct line_pnts *Points, int out_type)
 {
     static struct line_cats *Cats = NULL;
 
@@ -129,8 +140,7 @@
 
     if (0 > Vect_copy_xyz_to_pnts(Points, xarray, yarray, NULL, NUM_POINTS))
 	G_fatal_error(_("Out of memory"));
+    Vect_write_line(Map, out_type, Points, Cats);
 
-    Vect_write_line(Map, GV_BOUNDARY, Points, Cats);
-
     return 0;
 }



More information about the grass-commit mailing list