Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.distance: Add tests for r.distance module #4679

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions raster/r.distance/testsuite/test_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.gmodules import SimpleModule


class TestRDistance(TestCase):

@classmethod
def setUpClass(cls):
"""Set up a temporary region and generate test data."""
cls.use_temp_region()
cls.runModule("g.region", n=10, s=0, e=10, w=0, res=1)
# Create 'map1' with a block at the top-left corner
cls.runModule(
"r.mapcalc",
expression="map1 = if(row() <= 2 && col() <= 2, 1, null())",
overwrite=True,
)
# Create 'map2' with a block in the center
cls.runModule(
"r.mapcalc",
expression="map2 = if(row() >= 4 && row() <=6 && col() >= 4 && col() <= 6, 1, null())",
overwrite=True,
)
cls.runModule("r.mapcalc", expression="map3 = null()", overwrite=True)

@classmethod
def tearDownClass(cls):
"""Clean up after tests."""
cls.runModule(
"g.remove", flags="f", type="raster", name=["map1", "map2", "map3"]
)
cls.del_temp_region()

def test_distance(self):
"""Test distance calculation between map1 and map2."""
module = SimpleModule("r.distance", map=("map1", "map2"))
self.assertModule(module)

result = module.outputs.stdout.strip().split("\n")

expected_results = ["1:1:2.8284271247:1.5:8.5:3.5:6.5"]

for i, component in enumerate(result):
self.assertEqual(
component, expected_results[i], f"Mismatch at line {i + 1}"
)

def test_overlap_distance(self):
"""Test r.distance when comparing a map to itself with overlapping features."""
module = SimpleModule("r.distance", map=("map1", "map1"), flags="o")
self.assertModule(module)

result = module.outputs.stdout.strip().split("\n")

expected_results = ["1:1:0:0.5:9.5:0.5:9.5"]

self.assertEqual(
result,
expected_results,
"Mismatch in r.distance output for overlapping features",
)

def test_null_distance(self):
"""Test r.distance when comparing a map to itself with overlapping features."""
module = SimpleModule("r.distance", map=("map3", "map2"), flags="n")
self.assertModule(module)

result = module.outputs.stdout.strip().split("\n")

expected_results = ["*:*:0:0.5:9.5:0.5:9.5", "*:1:2:3.5:8.5:3.5:6.5"]

self.assertEqual(
result,
expected_results,
"Mismatch in r.distance output for reporting null objects as *",
)


if __name__ == "__main__":
test()
Loading