Skip to content

Commit

Permalink
r.horizon: adjust bufferzone to multiples of resolution (#3384)
Browse files Browse the repository at this point in the history
* r.horizon: adjust bufferzone to multiples of resolution

* fix test

* restrict input bufferzone to positive values

* add tests for negative bufferzone
  • Loading branch information
petrasovaa committed Feb 14, 2024
1 parent ecc3503 commit 7266cf8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
11 changes: 11 additions & 0 deletions raster/r.horizon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
80 changes: 80 additions & 0 deletions raster/r.horizon/testsuite/test_r_horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 7266cf8

Please sign in to comment.