forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r.buffer: Added test script (OSGeo#4482)
* Added test script for r.buffer * Corrected input map and added tearDown method to delete temp maps * Added pre commit fixes * Added resolution for feedback * Tweaked the last test and used f string * Update raster/r.buffer/testsuite/test_buffer.py Reduced the region of the test Co-authored-by: Anna Petrasova <[email protected]> * Corrected redudancy * Streamlined output map initialization --------- Co-authored-by: Anna Petrasova <[email protected]>
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from grass.gunittest.case import TestCase | ||
from grass.gunittest.main import test | ||
from grass.gunittest.gmodules import SimpleModule | ||
import grass.script as gs | ||
|
||
|
||
class TestRBuffer(TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Set up a temporary region for testing.""" | ||
cls.output = "test_buffer" | ||
cls.use_temp_region() | ||
cls.runModule("g.region", n=223000, s=220000, w=640000, e=643000, nsres=100) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
"""Clean up after tests.""" | ||
cls.del_temp_region() | ||
|
||
def tearDown(self): | ||
"""Remove temporary maps created during tests""" | ||
gs.run_command( | ||
"g.remove", | ||
type="raster", | ||
name="test_buffer,zero_map,null_map", | ||
flags="f", | ||
) | ||
|
||
def test_buffer_creation(self): | ||
"""Test creating a buffer around roadsmajor with multiple distances.""" | ||
distances = [100, 200, 300, 400, 500] | ||
|
||
module = SimpleModule( | ||
"r.buffer", | ||
input="roadsmajor", | ||
output=self.output, | ||
distances=distances, | ||
overwrite=True, | ||
) | ||
self.assertModule(module) | ||
|
||
self.assertRasterExists(self.output) | ||
|
||
expected_categories = [i + 1 for i in range(len(distances) + 1)] | ||
|
||
self.assertRasterMinMax( | ||
map=self.output, | ||
refmin=min(expected_categories), | ||
refmax=max(expected_categories), | ||
msg=f"Buffer zones should have category values from 1 to {max(expected_categories)}", | ||
) | ||
|
||
def test_no_non_null_values(self): | ||
"""Test r.buffer with null input raster resulting in an empty output.""" | ||
null_map = "null_map" | ||
self.runModule("r.mapcalc", expression=f"{null_map} = null()") | ||
|
||
distances = [100, 200, 300] | ||
|
||
module = SimpleModule( | ||
"r.buffer", | ||
input=null_map, | ||
output=self.output, | ||
distances=distances, | ||
overwrite=True, | ||
) | ||
self.assertModule(module) | ||
|
||
self.assertRasterExists(self.output) | ||
|
||
expected_stats = {"n": 0} | ||
self.assertRasterFitsUnivar(self.output, reference=expected_stats) | ||
|
||
def test_ignore_zero_values(self): | ||
"""Test r.buffer with input raster of only zero values using -z flag.""" | ||
zero_map = "zero_map" | ||
self.runModule("r.mapcalc", expression=f"{zero_map} = 0") | ||
|
||
distances = [100] | ||
|
||
module = SimpleModule( | ||
"r.buffer", | ||
input=zero_map, | ||
output=self.output, | ||
distances=distances, | ||
flags="z", | ||
overwrite=True, | ||
) | ||
self.assertModule(module) | ||
|
||
self.assertRasterExists(self.output) | ||
|
||
expected_stats = {"n": 0} | ||
self.assertRasterFitsUnivar(self.output, reference=expected_stats) | ||
|
||
|
||
if __name__ == "__main__": | ||
test() |