Opened 12 years ago

Last modified 8 years ago

#1405 new defect

[PATCH] Direct floating point values comparison in r.terraflow

Reported by: dron Owned by: grass-dev@…
Priority: normal Milestone: 6.4.6
Component: Raster Version: svn-trunk
Keywords: r.terraflow Cc:
CPU: Unspecified Platform: Unspecified


r.terraflow module does direct comparison of two floating point values. This is wrong and results in emergency exit with "cell file resolution differs from current region" message. There is a trivial patch fixing that:

---	(revision 47154)
+++	(working copy)
@@ -216,8 +216,8 @@
     G_fatal_error(_("Cannot read header of [%s]"), cellname);
   /* check compatibility with module region */
-  if (!((region->ew_res == cell_hd.ew_res)
-		&& (region->ns_res == cell_hd.ns_res))) {
+  if (!(fabs(region->ew_res - cell_hd.ew_res) < 0.0000001
+		&& fabs(region->ns_res - cell_hd.ns_res) < 0.0000001)) {
     G_fatal_error(_("cell file %s resolution differs from current region"),
   } else {

Applicable for 6.4, 6.5 and 7.0 branches.

Best regards,

Change History (2)

in reply to:  description comment:1 by glynn, 12 years ago

Replying to dron:

+  if (!(fabs(region->ew_res - cell_hd.ew_res) < 0.0000001
+		&& fabs(region->ns_res - cell_hd.ns_res) < 0.0000001)) {

The tolerance is probably too small.

I'd suggest checking that the difference over the entire region is less than some fraction of a cell, e.g.

fabs(region->ns_res - cell_hd.ns_res) * region->rows < region->ns_res * 0.1

The ideal check is probably a 1:1 correspondence between raster cells and region cells, but that isn't easy to implement. Note that this depends on both the resolution and the alignment. In the worst case, if the raster is offset by half a cell relative to the region, even a miniscule difference in resolution could result in cells being either duplicated or omitted.

comment:2 by martinl, 8 years ago

Milestone: 6.4.6
Note: See TracTickets for help on using tickets.