[GRASS-SVN] r71967 - grass/trunk/raster/r.path

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Dec 22 11:55:21 PST 2017


Author: mmetz
Date: 2017-12-22 11:55:21 -0800 (Fri, 22 Dec 2017)
New Revision: 71967

Modified:
   grass/trunk/raster/r.path/local.h
   grass/trunk/raster/r.path/main.c
   grass/trunk/raster/r.path/r.path.html
   grass/trunk/raster/r.path/r_path_with_bitmask.png
Log:
r.path: remove -b flag, add option to define direction format, update manual

Modified: grass/trunk/raster/r.path/local.h
===================================================================
--- grass/trunk/raster/r.path/local.h	2017-12-22 09:25:13 UTC (rev 71966)
+++ grass/trunk/raster/r.path/local.h	2017-12-22 19:55:21 UTC (rev 71967)
@@ -4,7 +4,6 @@
 #define OUT_CPY 3
 #define OUT_ACC 4
 
-struct metrics
-{
-    double ew_res, ns_res, diag_res;
-};
+#define DIR_DEG 1
+#define DIR_DEG45 2
+#define DIR_BIT 3

Modified: grass/trunk/raster/r.path/main.c
===================================================================
--- grass/trunk/raster/r.path/main.c	2017-12-22 09:25:13 UTC (rev 71966)
+++ grass/trunk/raster/r.path/main.c	2017-12-22 19:55:21 UTC (rev 71967)
@@ -106,14 +106,17 @@
     int i, j, have_points = 0;
     int nrows, ncols;
     int npoints;
-    int out_id, dir_id;
+    int out_id, dir_id, dir_format;
+    struct FPRange drange;
+    DCELL dmin, dmax;
     char map_name[GNAME_MAX], out_name[GNAME_MAX], dir_name[GNAME_MAX];
     char *tempfile1, *tempfile2;
     struct History history;
 
     struct Cell_head window;
-    struct Option *opt1, *opt2, *coordopt, *vpointopt, *opt3, *opt4;
-    struct Flag *flag1, *flag2, *flag3, *flag4;
+    struct Option *opt1, *dfopt, *opt2, *coordopt, *vpointopt,
+                  *opt3, *opt4;
+    struct Flag *flag1, *flag2, *flag3;
     struct GModule *module;
     void *dir_buf;
 
@@ -126,6 +129,7 @@
     struct line_pnts *Points;
     struct line_cats *Cats;
     struct Map_info vout, *pvout;
+    char *desc = NULL;
 
     G_gisinit(argv[0]);
 
@@ -137,10 +141,24 @@
 	_("Traces paths from starting points following input directions.");
 
     opt1 = G_define_standard_option(G_OPT_R_INPUT);
-    opt1->description = _("Name of input direction");
+    opt1->label = _("Name of input direction");
     opt1->description =
 	_("Direction in degrees CCW from east, or bitmask encoded (-b flag)");
-    
+
+    dfopt = G_define_option();
+    dfopt->type = TYPE_STRING;
+    dfopt->key = "format";
+    dfopt->label = _("Format of the input direction map");
+    dfopt->required = YES;
+    dfopt->answer = "auto";
+    G_asprintf(&desc,
+           "auto;%s;degree;%s;45degree;%s;bitmask;%s",
+           _("auto-detect direction format"),
+           _("degrees CCW from East"),
+           _("degrees CCW from East divided by 45 (e.g. r.watershed directions)"),
+           _("bitmask encoded directions (e.g. r.cost -b)"));
+    dfopt->descriptions = desc;
+
     opt2 = G_define_standard_option(G_OPT_R_INPUT);
     opt2->key = "values";
     opt2->label =
@@ -166,7 +184,6 @@
     vpointopt->key = "start_points";
     vpointopt->required = NO;
     vpointopt->label = _("Name of starting vector points map(s)");
-    vpointopt->description = NULL;
     vpointopt->guisection = _("Start");
 
     flag1 = G_define_flag();
@@ -184,11 +201,6 @@
     flag3->description = _("Count cell numbers along the path");
     flag3->guisection = _("Path settings");
 
-    flag4 = G_define_flag();
-    flag4->key = 'b';
-    flag4->description =
-	_("The input direction map is bitmask encoded");
-
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -360,11 +372,56 @@
 	G_free(map_buf);
     }
 
+    if (Rast_read_fp_range(dir_name, "", &drange) < 0)
+	G_fatal_error(_("Unable to read range file"));
+    Rast_get_fp_range_min_max(&drange, &dmin, &dmax);
+    if (dmax <= 0)
+	G_fatal_error(_("Invalid directions map <%s>"), dir_name);
+
+    dir_format = -1;
+    if (strcmp(dfopt->answer, "degree") == 0) {
+	if (dmax > 360)
+	    G_fatal_error(_("Directional degrees can not be > 360"));
+	dir_format = DIR_DEG;
+    }
+    else if (strcmp(dfopt->answer, "45degree") == 0) {
+	if (dmax > 8)
+	    G_fatal_error(_("Directional degrees divided by 45 can not be > 8"));
+	dir_format = DIR_DEG45;
+    }
+    else if (strcmp(dfopt->answer, "bitmask") == 0) {
+	if (dmax > (1 << 16) - 1)
+	    G_fatal_error(_("Bitmask encoded directions can not be > %d"), (1 << 16) - 1);
+	dir_format = DIR_BIT;
+    }
+    else if (strcmp(dfopt->answer, "auto") == 0) {
+	if (dmax <= 8) {
+	    dir_format = DIR_DEG45;
+	    G_important_message(_("Input direction format assumed to be degrees CCW from East divided by 45"));
+	}
+	else if (dmax <= (1 << 8) - 1) {
+	    dir_format = DIR_BIT;
+	    G_important_message(_("Input direction format assumed to be bitmask encoded without Knight's move"));
+	}
+	else if (dmax <= 360) {
+	    dir_format = DIR_DEG;
+	    G_important_message(_("Input direction format assumed to be degrees CCW from East"));
+	}
+	else if (dmax <= (1 << 16) - 1) {
+	    dir_format = DIR_BIT;
+	    G_important_message(_("Input direction format assumed to be bitmask encoded with Knight's move"));
+	}
+	else
+	    G_fatal_error(_("Unable to detect format of input direction map <%s>"), dir_name);
+    }
+    if (dir_format <= 0)
+	G_fatal_error(_("Invalid directions format '%s'"), dfopt->answer);
+
     dir_id = Rast_open_old(dir_name, "");
     tempfile2 = G_tempfile();
     dir_fd = open(tempfile2, O_RDWR | O_CREAT, 0666);
 
-    if (flag4->answer) {
+    if (dir_format == DIR_BIT) {
 	dir_buf = Rast_allocate_c_buf();
 	for (i = 0; i < nrows; i++) {
 	    Rast_get_c_row(dir_id, dir_buf, i);
@@ -378,6 +435,13 @@
 	dir_buf = Rast_allocate_d_buf();
 	for (i = 0; i < nrows; i++) {
 	    Rast_get_d_row(dir_id, dir_buf, i);
+	    if (dir_format == DIR_DEG45) {
+		DCELL *dp;
+
+		dp = (DCELL *)dir_buf;
+		for (j = 0; j < ncols; j++, dp++)
+		    *dp *= 45;
+	    }
 	    if (write(dir_fd, dir_buf, ncols * sizeof(DCELL)) !=
 	        ncols * sizeof(DCELL)) {
 		G_fatal_error(_("Unable to write to tempfile"));
@@ -404,7 +468,7 @@
     while (next_start_pt) {
 	/* follow directions from start points to determine paths */
 	/* path tracing algorithm selection */
-	if (flag4->answer) {
+	if (dir_format == DIR_BIT) {
 	    struct Map_info Tmp;
 
 	    if (pvout) {

Modified: grass/trunk/raster/r.path/r.path.html
===================================================================
--- grass/trunk/raster/r.path/r.path.html	2017-12-22 09:25:13 UTC (rev 71966)
+++ grass/trunk/raster/r.path/r.path.html	2017-12-22 19:55:21 UTC (rev 71967)
@@ -16,8 +16,7 @@
 to all neighbours with their bit set. This means a path can split and 
 merge. Such bitmasked directions can be created with the <b>-b</b> 
 flag of <em><a href="r.cost.html">r.cost</a></em> and 
-<em><a href="r.walk.html">r.walk</a></em>. The corresponding <b>-b</b> 
-flag must then be set for <em>r.path</em>.
+<em><a href="r.walk.html">r.walk</a></em>.
 
 <div class="code"><pre>
 Direction encoding for neighbors of x
@@ -154,14 +153,14 @@
 r.walk -b elevation=elev_ned_30m friction=friction output=walkcost \
     outdir=walkdir start_coordinates=635576,216485
 
-r.path -b input=walkdir start_coordinates=640206,222795 \
+r.path input=walkdir start_coordinates=640206,222795 \
     raster_path=walkpath vector_path=walkpath
 
 # with Knight's move
 r.walk -b -k elevation=elev_ned_30m friction=friction output=walkcost_k \
     outdir=walkdir_k start_coordinates=635576,216485
 
-r.path -b input=walkdir_k start_coordinates=640206,222795 \
+r.path input=walkdir_k start_coordinates=640206,222795 \
     raster_path=walkpath_k vector_path=walkpath_k
 
 # without Knight's move and without bitmask encoding (single direction)
@@ -186,11 +185,11 @@
 
 <div align="center">
 <a href="r_path_with_bitmask.png">
-    <img src="r_path_with_bitmask.png" alt="least cost path using bitmask" width="129" height="300">
+    <img src="r_path_with_bitmask.png" alt="least cost path using bitmask" height="300">
 </a>
 <br>
-<i>Figure: Comparison of shortest path computed using single directional
-angles and bitmask direction encoding with and without Knight's move</i>
+<i>Figure: Comparison of shortest paths using single directions and 
+multiple bitmask encoded directions without and with Knight's move</i>
 </div>
 
 <h2>SEE ALSO</h2>

Modified: grass/trunk/raster/r.path/r_path_with_bitmask.png
===================================================================
(Binary files differ)



More information about the grass-commit mailing list