diff --git a/raster/r.horizon/main.c b/raster/r.horizon/main.c index b7becc858a0..ec6f0c4f50e 100644 --- a/raster/r.horizon/main.c +++ b/raster/r.horizon/main.c @@ -233,6 +233,7 @@ int main(int argc, char *argv[]) parm.bufferzone->description = _("For horizon rasters, read from the DEM an extra buffer around the " "present region"); + parm.bufferzone->options = "0-"; parm.bufferzone->guisection = _("Raster mode"); parm.e_buff = G_define_option(); @@ -241,6 +242,7 @@ int main(int argc, char *argv[]) parm.e_buff->required = NO; parm.e_buff->description = _("For horizon rasters, read from the DEM an " "extra buffer eastward the present region"); + parm.e_buff->options = "0-"; parm.e_buff->guisection = _("Raster mode"); parm.w_buff = G_define_option(); @@ -249,6 +251,7 @@ int main(int argc, char *argv[]) parm.w_buff->required = NO; parm.w_buff->description = _("For horizon rasters, read from the DEM an " "extra buffer westward the present region"); + parm.w_buff->options = "0-"; parm.w_buff->guisection = _("Raster mode"); parm.n_buff = G_define_option(); @@ -257,6 +260,7 @@ int main(int argc, char *argv[]) parm.n_buff->required = NO; parm.n_buff->description = _("For horizon rasters, read from the DEM an " "extra buffer northward the present region"); + parm.n_buff->options = "0-"; parm.n_buff->guisection = _("Raster mode"); parm.s_buff = G_define_option(); @@ -265,6 +269,7 @@ int main(int argc, char *argv[]) parm.s_buff->required = NO; parm.s_buff->description = _("For horizon rasters, read from the DEM an " "extra buffer southward the present region"); + parm.s_buff->options = "0-"; parm.s_buff->guisection = _("Raster mode"); parm.maxdistance = G_define_option(); @@ -499,6 +504,12 @@ int main(int argc, char *argv[]) if (nbufferZone == 0.) nbufferZone = bufferZone; + /* adjust buffer to multiples of resolution */ + ebufferZone = (int)(ebufferZone / stepx) * stepx; + wbufferZone = (int)(wbufferZone / stepx) * stepx; + sbufferZone = (int)(sbufferZone / stepy) * stepy; + nbufferZone = (int)(nbufferZone / stepy) * stepy; + new_cellhd.rows += (int)((nbufferZone + sbufferZone) / stepy); new_cellhd.cols += (int)((ebufferZone + wbufferZone) / stepx); diff --git a/raster/r.horizon/testsuite/test_r_horizon.py b/raster/r.horizon/testsuite/test_r_horizon.py index 95a2127eba4..8111e5d9791 100644 --- a/raster/r.horizon/testsuite/test_r_horizon.py +++ b/raster/r.horizon/testsuite/test_r_horizon.py @@ -222,6 +222,86 @@ def test_raster_mode_multiple_direction_offset(self): second=stdout, ) + def test_raster_mode_bufferzone(self): + """Test buffer 100 m and 109 m with resolution 10 gives the same result""" + self.runModule( + "g.region", + raster="elevation", + n="n-5000", + s="s+5000", + e="e-5000", + w="w+5000", + ) + # raises ValueError from pygrass parameter check + self.assertRaises( + ValueError, + SimpleModule, + "r.horizon", + elevation="elevation", + output=self.horizon_output, + direction=50, + bufferzone=-100, + ) + self.assertRaises( + ValueError, + SimpleModule, + "r.horizon", + elevation="elevation", + output=self.horizon_output, + direction=50, + e_buff=100, + n_buff=0, + s_buff=-100, + w_buff=-100, + ) + module = SimpleModule( + "r.horizon", + elevation="elevation", + output=self.horizon_output, + direction=50, + bufferzone=100, + ) + self.assertModule(module) + ref = { + "mean": 0.0344791, + } + output = "test_horizon_output_from_elevation_050" + self.assertRasterFitsUnivar( + raster=output, + reference=ref, + precision=1e-6, + ) + module = SimpleModule( + "r.horizon", + elevation="elevation", + output=self.horizon_output, + direction=50, + bufferzone=103, + ) + self.assertModule(module) + self.assertRasterFitsUnivar( + raster=output, + reference=ref, + precision=1e-6, + ) + module = SimpleModule( + "r.horizon", + elevation="elevation", + output=self.horizon_output, + direction=50, + bufferzone=95, + ) + self.assertModule(module) + ref = { + "mean": 0.0344624, + } + self.assertRasterFitsUnivar( + raster=output, + reference=ref, + precision=1e-6, + ) + self.runModule("g.region", raster="elevation") + if __name__ == "__main__": test()