Skip to content

Commit

Permalink
Test for dissolve/merge of areas without shared boundaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzeslaus committed Jun 9, 2022
1 parent 288ff89 commit 1c6fe46
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
63 changes: 63 additions & 0 deletions scripts/v.dissolve/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,66 @@ def dataset(tmp_path_factory):
str_column_name=str_column_name,
str_column_values=str_values,
)


@pytest.fixture(scope="module")
def discontinuous_dataset(tmp_path_factory):
"""Creates a session with a mapset which has vector with a float column"""
tmp_path = tmp_path_factory.mktemp("discontinuous_dataset")
location = "test"
point_map_name = "points"
map_name = "areas"
int_column_name = "int_value"
float_column_name = "double_value"
str_column_name = "str_value"

cats = [1, 2, 3, 4, 5, 6]
int_values = [10, 12, 10, 5, 24, 24]
float_values = [100.78, 102.78, 109.78, 104.78, 103.78, 105.78]
str_values = ["apples", "plumbs", "apples", "plumbs", "oranges", "oranges"]
num_points = len(cats)

gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access
with grass_setup.init(tmp_path / location):
gs.run_command("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
gs.run_command("v.random", output=point_map_name, npoints=num_points, seed=42)
gs.run_command("v.voronoi", input=point_map_name, output=map_name)
gs.run_command(
"v.db.addtable",
map=map_name,
columns=[
f"{int_column_name} integer",
f"{float_column_name} double precision",
f"{str_column_name} text",
],
)
value_update_by_category(
map_name=map_name,
layer=1,
column_name=int_column_name,
cats=cats,
values=int_values,
)
value_update_by_category(
map_name=map_name,
layer=1,
column_name=float_column_name,
cats=cats,
values=float_values,
)
value_update_by_category(
map_name=map_name,
layer=1,
column_name=str_column_name,
cats=cats,
values=str_values,
)
yield SimpleNamespace(
vector_name=map_name,
int_column_name=int_column_name,
int_values=int_values,
float_column_name=float_column_name,
float_values=float_values,
str_column_name=str_column_name,
str_column_values=str_values,
)
59 changes: 54 additions & 5 deletions scripts/v.dissolve/tests/v_dissolve_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def test_dissolve_int(dataset):
assert vector_info["points"] == 0
assert vector_info["lines"] == 0
assert vector_info["boundaries"] == 16
assert vector_info["centroids"] == 3
assert vector_info["areas"] == 3
assert vector_info["islands"] == 1
assert vector_info["primitives"] == 19
assert vector_info["map3d"] == 0
Expand All @@ -53,7 +51,6 @@ def test_dissolve_str(dataset):
assert vector_info["num_dblinks"] == 1
assert vector_info["attribute_primary_key"] == "cat"
# Reference values obtained by examining the result.
assert vector_info["boundaries"] == 15
assert vector_info["north"] == 80
assert vector_info["south"] == 0
assert vector_info["east"] == 120
Expand All @@ -62,8 +59,6 @@ def test_dissolve_str(dataset):
assert vector_info["points"] == 0
assert vector_info["lines"] == 0
assert vector_info["boundaries"] == 15
assert vector_info["centroids"] == 3
assert vector_info["areas"] == 3
assert vector_info["islands"] == 1
assert vector_info["primitives"] == 18
assert vector_info["map3d"] == 0
Expand All @@ -85,3 +80,57 @@ def test_dissolve_str(dataset):
actual_values = [record[dataset.str_column_name] for record in records]
assert len(actual_values) == len(ref_unique_values)
assert set(actual_values) == ref_unique_values


def test_dissolve_discontinuous_str(discontinuous_dataset):
"""Dissolving of discontinuous areas results in a single attribute record
Even when the areas are discontinuous, there should be only one row
in the attribute table.
This behavior is assumed by the attribute aggregation functionality.
"""
dataset = discontinuous_dataset
dissolved_vector = "test_discontinuous_str"
gs.run_command(
"v.dissolve",
input=dataset.vector_name,
column=dataset.str_column_name,
output=dissolved_vector,
)

vector_info = gs.vector_info(dissolved_vector)
assert vector_info["level"] == 2
assert vector_info["centroids"] == 5
assert vector_info["areas"] == 5
assert vector_info["num_dblinks"] == 1
assert vector_info["attribute_primary_key"] == "cat"
# Reference values obtained by examining the result.
assert vector_info["north"] == 80
assert vector_info["south"] == 0
assert vector_info["east"] == 120
assert vector_info["west"] == 0
assert vector_info["nodes"] == 14
assert vector_info["points"] == 0
assert vector_info["lines"] == 0
assert vector_info["boundaries"] == 18
assert vector_info["islands"] == 1
assert vector_info["primitives"] == 23
assert vector_info["map3d"] == 0

columns = gs.vector_columns(dissolved_vector)
assert len(columns) == 2
assert sorted(columns.keys()) == sorted(["cat", dataset.str_column_name])
column_info = columns[dataset.str_column_name]
assert column_info["type"].lower() == "character"

records = json.loads(
gs.read_command(
"v.db.select",
map=dissolved_vector,
format="json",
)
)["records"]
ref_unique_values = set(dataset.str_column_values)
actual_values = [record[dataset.str_column_name] for record in records]
assert len(actual_values) == len(ref_unique_values)
assert set(actual_values) == ref_unique_values
4 changes: 0 additions & 4 deletions scripts/v.dissolve/v.dissolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ def check_aggregate_methods_or_fatal(methods, backend):
# and open for SQLite depending on its extensions.


# TODO: Confirm that there is only one record in the table
# for a given attribute value after dissolve.


def aggregate_attributes_sql(
input_name,
column,
Expand Down

0 comments on commit 1c6fe46

Please sign in to comment.