[GRASS-SVN] r61643 - in grass/branches/releasebranch_7_0: . vector/v.mkgrid

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 14 17:03:14 PDT 2014


Author: neteler
Date: 2014-08-14 17:03:13 -0700 (Thu, 14 Aug 2014)
New Revision: 61643

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


Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
   - /grass/trunk:60908,60986
   + /grass/trunk:60908,60986,61519

Modified: grass/branches/releasebranch_7_0/vector/v.mkgrid/local_proto.h
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.mkgrid/local_proto.h	2014-08-14 20:48:10 UTC (rev 61642)
+++ grass/branches/releasebranch_7_0/vector/v.mkgrid/local_proto.h	2014-08-15 00:03:13 UTC (rev 61643)
@@ -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_7_0/vector/v.mkgrid/main.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.mkgrid/main.c	2014-08-14 20:48:10 UTC (rev 61642)
+++ grass/branches/releasebranch_7_0/vector/v.mkgrid/main.c	2014-08-15 00:03:13 UTC (rev 61643)
@@ -6,8 +6,10 @@
  *               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>
+ *               Ivan Shevlakov: points support -p                
+ *               Luca Delucchi: lines support -l
  * PURPOSE:      
- * COPYRIGHT:    (C) 1999-2009 by the GRASS Development Team
+ * 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
@@ -47,8 +49,8 @@
     struct Map_info Map;
     struct Option *vectname, *grid, *coord, *box, *angle, *position_opt, *breaks;
     struct GModule *module;
-    struct Flag *points_fl;
-    int points_p;
+    struct Flag *points_fl, *line_fl;
+    int points_p, line_p, output_type;
     char *desc;
 
     struct line_pnts *Points;
@@ -132,9 +134,20 @@
     points_fl->description =
 	_("Create grid of points instead of areas and centroids");
 
+    line_fl = G_define_flag();
+    line_fl->key = 'l';
+    line_fl->description =
+	_("Create grid as lines, instead of areas");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+    line_p = line_fl->answer;
+    if (line_p) {
+	output_type = GV_LINE;
+    } else {
+	output_type = GV_BOUNDARY;
+    }
 
     points_p = points_fl->answer;
 
@@ -256,58 +269,63 @@
 
     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 */
-    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;
+    /* 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);
 
-	    sprintf(buf, "insert into %s values ", Fi->table);
-	    if (db_set_string(&sql, buf) != DB_OK)
-		G_fatal_error(_("Unable to fill attribute table"));
+	      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 (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 (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"));
 
-	    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));
-	    }
-	    attCount++;
-	}
+	      if (db_execute_immediate(Driver, &sql) != DB_OK) {
+		  G_fatal_error(_("Unable to insert new record: %s"),
+			      db_get_string(&sql));
+	      }
+	      attCount++;
+	  }
+      }
     }
     db_commit_transaction(Driver);
 

Modified: grass/branches/releasebranch_7_0/vector/v.mkgrid/write_grid.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.mkgrid/write_grid.c	2014-08-14 20:48:10 UTC (rev 61642)
+++ grass/branches/releasebranch_7_0/vector/v.mkgrid/write_grid.c	2014-08-15 00:03:13 UTC (rev 61643)
@@ -5,7 +5,7 @@
 #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;
@@ -65,7 +65,7 @@
 		       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;
@@ -95,7 +95,7 @@
 	    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;
@@ -117,7 +117,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;
 
@@ -132,8 +132,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