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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 25 03:24:24 PST 2014


Author: mmetz
Date: 2014-11-25 03:24:24 -0800 (Tue, 25 Nov 2014)
New Revision: 62929

Modified:
   grass/trunk/vector/v.mkgrid/main.c
   grass/trunk/vector/v.mkgrid/v.mkgrid.html
Log:
v.mkgrid: make grid option optional for position=region, update examples

Modified: grass/trunk/vector/v.mkgrid/main.c
===================================================================
--- grass/trunk/vector/v.mkgrid/main.c	2014-11-25 11:14:53 UTC (rev 62928)
+++ grass/trunk/vector/v.mkgrid/main.c	2014-11-25 11:24:24 UTC (rev 62929)
@@ -51,7 +51,7 @@
     struct Option *vectname, *grid, *coord, *box, *angle, *position_opt,
                   *breaks, *type_opt;
     struct GModule *module;
-    struct Flag *hex_flag, *hs_flag;
+    struct Flag *hex_flag, *ha_flag;
     int otype, ptype, ltype;
     char *desc;
 
@@ -80,7 +80,7 @@
     grid->key = "grid";
     grid->key_desc = _("rows,columns");
     grid->type = TYPE_INTEGER;
-    grid->required = YES;
+    grid->required = NO;
     grid->multiple = NO;
     grid->description = _("Number of rows and columns in grid");
 
@@ -131,7 +131,7 @@
     breaks->description =
 	_("Number of vertex points per grid cell");
     breaks->options = "0-60";
-    breaks->answer = "3";
+    breaks->answer = "0";
 
     type_opt = G_define_standard_option(G_OPT_V_TYPE);
     type_opt->options = "point,line,area";
@@ -144,10 +144,10 @@
     hex_flag->description =
 	_("Create hexagons (default: rectangles)");
 
-    hs_flag = G_define_flag();
-    hs_flag->key = 's';
-    hs_flag->description =
-	_("Enforce symmetric hexagons");
+    ha_flag = G_define_flag();
+    ha_flag->key = 'a';
+    ha_flag->description =
+	_("Allow asymmetric hexagons");
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
@@ -179,10 +179,6 @@
      */
     dig_file = G_store(vectname->answer);
 
-    /* Number of row and cols */
-    grid_info.num_rows = atoi(grid->answers[0]);
-    grid_info.num_cols = atoi(grid->answers[1]);
-
     grid_info.angle = M_PI / 180 * atof(angle->answer);
     set_angle(grid_info.angle);
 
@@ -193,21 +189,51 @@
 	if (coord->answer)
 	    G_fatal_error(_("'coor' and 'position=region' are exclusive options"));
 
-	if (box->answer)
-	    G_fatal_error(_("'box' and 'position=region' are exclusive options"));
+	if (box->answer && grid->answer)
+	    G_fatal_error(_("'box' and 'grid' are exclusive options for 'position=region'"));
 
 	grid_info.west = window.west;
 	grid_info.south = window.south;
 	grid_info.east = window.east;
 	grid_info.north = window.north;
 
-	grid_info.width = (window.east - window.west) / grid_info.num_cols;
-	grid_info.height = (window.north - window.south) / grid_info.num_rows;
+	grid_info.num_rows = window.rows;
+	grid_info.num_cols = window.cols;
 
+	grid_info.width = window.ew_res;
+	grid_info.height = window.ns_res;
+
+	if (grid->answer) {
+	    grid_info.num_rows = atoi(grid->answers[0]);
+	    grid_info.num_cols = atoi(grid->answers[1]);
+
+	    grid_info.width = (grid_info.east - grid_info.west) / grid_info.num_cols;
+	    grid_info.height = (grid_info.north - grid_info.south) / grid_info.num_rows;
+	}
+	else if (box->answer) {
+	    if (!G_scan_resolution
+		(box->answers[0], &(grid_info.width), window.proj))
+		G_fatal_error(_("Invalid width"));
+	    if (!G_scan_resolution
+		(box->answers[1], &(grid_info.height), window.proj))
+		G_fatal_error(_("Invalid height"));
+
+	    /* register to lower left corner as for position=coor */
+	    grid_info.num_cols = (grid_info.east - grid_info.west + 
+	                         grid_info.width / 2.0) / grid_info.width;
+	    grid_info.num_rows = (grid_info.north - grid_info.south + 
+	                         grid_info.height / 2.0) / grid_info.height;
+	    grid_info.north = grid_info.south + grid_info.num_rows * grid_info.height;
+	    grid_info.east = grid_info.west + grid_info.num_cols * grid_info.width;
+	}
+
 	G_debug(2, "x = %e y = %e w = %e h = %e", grid_info.west,
 		grid_info.south, grid_info.width, grid_info.height);
     }
     else {
+	if (!grid->answer)
+	    G_fatal_error(_("'grid' option missing"));
+
 	if (!coord->answer)
 	    G_fatal_error(_("'coor' option missing"));
 
@@ -228,6 +254,9 @@
 	    (box->answers[1], &(grid_info.height), window.proj))
 	    G_fatal_error(_("Invalid height"));
 
+	grid_info.num_rows = atoi(grid->answers[0]);
+	grid_info.num_cols = atoi(grid->answers[1]);
+
 	grid_info.east = grid_info.west + grid_info.width * grid_info.num_cols;
 	grid_info.north = grid_info.south + grid_info.height * grid_info.num_rows;
     }
@@ -269,7 +298,7 @@
 	                  (grid_info.num_cols + 1.0 / 3.0);
 	grid_info.crad = grid_info.cstep / 1.5;
 
-	if (hs_flag->answer) {
+	if (!ha_flag->answer || grid_info.width == grid_info.height) {
 	    if (grid_info.rrad > grid_info.crad) {
 		grid_info.rrad = grid_info.crad;
 		grid_info.rstep = grid_info.rrad * sqrt(3.0) / 2.0;
@@ -279,9 +308,17 @@
 		grid_info.cstep = grid_info.crad * 1.5;
 	    }
 	}
+	else {
+	    if (grid_info.width != grid_info.height) {
+		G_important_message(_("The hexagons will be asymmetrical."));
+	    }
+	}
 
 	grid_info.num_vect_rows = (grid_info.north - grid_info.south) / 
-	     grid_info.rstep - 0.5;
+	     grid_info.rstep;
+	if (grid_info.north - grid_info.rstep * (grid_info.num_vect_rows + 1) < 
+	    grid_info.south)
+	    grid_info.num_vect_rows--;
 	grid_info.num_vect_cols = (grid_info.east - grid_info.west -
 	     grid_info.crad * 0.5) / grid_info.cstep;
 
@@ -292,6 +329,13 @@
 	    G_fatal_error(_("Please use a higher resolution or a larger region"));
 	}
 
+	if ((int)(grid_info.num_vect_rows / 2.0 + 0.5) != grid_info.num_rows)
+	    G_message(_("The number of rows has been adjusted from %d to %d"),
+	              grid_info.num_rows, (int)(grid_info.num_vect_rows / 2.0 + 0.5));
+	if (grid_info.num_vect_cols != grid_info.num_cols)
+	    G_message(_("The number of columns has been adjusted from %d to %d"),
+	              grid_info.num_cols, grid_info.num_vect_cols);
+
 	sprintf(buf, "create table %s ( %s integer)", Fi->table, Fi->key);
 
 	db_set_string(&sql, buf);
@@ -328,6 +372,10 @@
 	}
     }
     else {
+	if (grid_info.width != grid_info.height) {
+	    G_important_message(_("The rectangles will be asymmetrical."));
+	}
+
 	/*
 	 * vector rows are the actual number of rows of vectors to make up the
 	 * entire grid.   ditto for cols.

Modified: grass/trunk/vector/v.mkgrid/v.mkgrid.html
===================================================================
--- grass/trunk/vector/v.mkgrid/v.mkgrid.html	2014-11-25 11:14:53 UTC (rev 62928)
+++ grass/trunk/vector/v.mkgrid/v.mkgrid.html	2014-11-25 11:24:24 UTC (rev 62929)
@@ -11,15 +11,15 @@
 <b>type=area</b> option. 
 <p>
 Grid lines created with the <b>type=line</b> option will be identical to 
-the edges of each grid cell area, like boundaries with the default 
+the edges of each grid cell, like boundaries with the default 
 <b>type=area</b> option. 
 <p>
 The resultant grid can be rotated around the origin (center of the 
 grid) with the <b>angle</b> option.
 <p>
 Optionally hexagons can be created with the <b>-h</b> flag. Hexagons 
-are by default asymmetric. Symmetric hexagons can be created with the 
-<b>-s</b> flag.
+are by default symmetric. Asymmetric hexagons can be allowed with the 
+<b>-a</b> flag.
 <p>
 This module is NOT to be used to generate a vector map of USGS 
 quadrangles, because USGS quads are not exact rectangles.
@@ -31,7 +31,7 @@
 
 To be run in a latitude-longitude location (WGS84)
 <div class="code"><pre>
-# use g.region to easily calculate rows and column for 'grid':
+# set the region:
 g.region n=90 s=-90 w=-180 e=180 res=10 -p
 projection: 3 (Latitude-Longitude)
 zone:       0
@@ -48,7 +48,10 @@
 cells:      648
 
 # create 10 degree size grid:
-v.mkgrid map=grid_10deg grid=18,36
+v.mkgrid map=grid_10deg
+
+# create 20 degree size grid:
+v.mkgrid map=grid_20deg box=20,20
 </pre></div>
 
 <h3>Creating a grid in a metric projection</h3>
@@ -77,9 +80,8 @@
 based on the current region extent defined by the "elevation" map:
 
 <div class="code"><pre>
-g.region rast=elevation -p
-# we use the row/column/resolution information to determine the grid= values:
-v.mkgrid type=point map=pointpattern grid=13,15 position=region breaks=1
+g.region rast=elevation res=1000 -pa
+v.mkgrid type=point map=pointpattern
 </pre></div>
 <p>
 
@@ -91,14 +93,14 @@
 
 <div class="code"><pre>
 # create first set of points, covering extent of "elevation" raster map
-g.region rast=elevation -p
-v.mkgrid type=point map=pointpattern1 grid=13,15 position=region breaks=1
+g.region rast=elevation res=1000 -pa
+v.mkgrid type=point map=pointpattern1
 
 # shift grid by half point distance (map units)
 g.region n=n+500 w=w+500 e=e+500 s=s+500 -p
 
 # create second set of points
-v.mkgrid type=point map=pointpattern2 grid=13,15 position=region breaks=1
+v.mkgrid type=point map=pointpattern2
 
 # merge into final point pattern
 v.patch input=pointpattern1,pointpattern2 output=pointpattern3



More information about the grass-commit mailing list