Files
geointel/backend/tests/test_partition_selection_deduplication.py
T
JensandClaude Opus 5 dd87a62e8f report what an area selection actually measured
Four ways a selection produced a confident number about a different area than
the operator drew:

Flood hazard divided the inundated cells by every cell in the drawn rectangle,
including cells the VMM raster does not model at all. A selection reaching
past the modelled extent therefore reported a diluted risk share, turning
missing data into an implied absence of risk. Terrain, bathymetry and thematic
raster already divided by valid cells; flood hazard was the outlier. It now
reports the three populations separately, states model coverage next to the
drawn area, and returns a null fraction rather than a zero when nothing was
modelled.

geometry_mask selects a cell when its centre falls inside the geometry, so a
rectangle smaller than one cell — or one landing between four centres —
selected nothing and the analysis returned zeros indistinguishable on screen
from "we looked and there is nothing here". On a 100 m population raster a
40 m rectangle over a city block reported no inhabitants. Selection now falls
back to the touched cells and says that it did, since the answer then covers
more ground than was requested. rasterio.mask applies the same centre rule
when cropping, so that call is widened too; the cells that count are still
decided by the centre rule wherever it selects anything.

The object count treated any feature touching the selection as whole, while
intersection_area clipped it — two headline numbers on one panel describing
different populations. The count stays whole-feature, which is what "objecten"
means to an operator, but now reports how many the edge cuts and is marked an
estimate when it does. The area_weighted_sum branch reuses that same count
instead of issuing its own near-identical query.

Partitioned selection de-duplicated the count on source_feature_id but
returned the raw rows, so a building on a municipal boundary was counted once
and drawn twice.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 14:33:19 +02:00

62 lines
1.9 KiB
Python

"""A rectangle across a municipal boundary must not return the same object twice.
Partitioned selection de-duplicated ``total_feature_count`` on
``source_feature_id`` but returned the raw rows. A feature present in two
municipal partitions was therefore drawn twice on the map and counted once in
the headline, so the number on the panel disagreed with the geometry beside it.
"""
from __future__ import annotations
from uuid import uuid4
from app.services.vector_feature_service import VectorFeatureService
class _Row:
def __init__(self, source_feature_id, row_id=None, dataset_id=None):
self.source_feature_id = source_feature_id
self.id = row_id or uuid4()
self.dataset_id = dataset_id or uuid4()
def _ids(rows):
return [row.source_feature_id or str(row.id) for row in rows]
def test_a_feature_in_two_partitions_is_returned_once() -> None:
shared = "grb-building-42"
rows = [_Row(shared), _Row("grb-building-7"), _Row(shared)]
kept = VectorFeatureService.deduplicate_rows(rows)
assert _ids(kept) == [shared, "grb-building-7"]
def test_the_first_occurrence_wins_so_the_result_is_stable() -> None:
first = _Row("dup")
second = _Row("dup")
assert VectorFeatureService.deduplicate_rows([first, second])[0] is first
assert VectorFeatureService.deduplicate_rows([second, first])[0] is second
def test_rows_without_a_source_id_fall_back_to_their_own_identity() -> None:
"""Two distinct rows with no source id are two distinct features."""
rows = [_Row(None), _Row(None)]
assert len(VectorFeatureService.deduplicate_rows(rows)) == 2
def test_an_empty_source_id_is_not_treated_as_a_shared_identity() -> None:
rows = [_Row(""), _Row("")]
assert len(VectorFeatureService.deduplicate_rows(rows)) == 2
def test_deduplication_leaves_a_clean_population_untouched() -> None:
rows = [_Row("a"), _Row("b"), _Row("c")]
assert VectorFeatureService.deduplicate_rows(rows) == rows