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>
86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
"""Choosing which raster cells a drawn selection covers.
|
|
|
|
``rasterio.features.geometry_mask`` selects a cell when the cell *centre* falls
|
|
inside the geometry. That is the right rule for a selection spanning many
|
|
cells, and the wrong one for a small selection: a rectangle smaller than a cell,
|
|
or one landing between four centres, selects nothing. The analysis then reports
|
|
zeros, which on screen reads as "we measured this area and found nothing"
|
|
rather than "this selection is finer than the source raster".
|
|
|
|
Falling back to every touched cell keeps a small selection answerable, at the
|
|
cost of analysing more ground than was drawn. That trade is only honest if it
|
|
is stated, so the fallback is reported alongside the result.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RasterCellSelection:
|
|
mask: Any
|
|
mode: str
|
|
expanded_to_touched_cells: bool
|
|
warning: str | None
|
|
|
|
|
|
def select_cells(
|
|
geometry,
|
|
*,
|
|
out_shape: tuple[int, int],
|
|
transform,
|
|
cell_area_m2: float | None = None,
|
|
) -> RasterCellSelection:
|
|
"""Mask the cells a selection covers, widening only when it covers none.
|
|
|
|
``geometry`` must already be in the raster's own CRS.
|
|
"""
|
|
|
|
from rasterio.features import geometry_mask
|
|
from shapely.geometry import mapping
|
|
|
|
shapes = [mapping(geometry)]
|
|
mask = geometry_mask(shapes, out_shape=out_shape, transform=transform, invert=True)
|
|
if mask.any():
|
|
return RasterCellSelection(
|
|
mask=mask,
|
|
mode="cell_centre",
|
|
expanded_to_touched_cells=False,
|
|
warning=None,
|
|
)
|
|
|
|
touched = geometry_mask(
|
|
shapes,
|
|
out_shape=out_shape,
|
|
transform=transform,
|
|
invert=True,
|
|
all_touched=True,
|
|
)
|
|
if not touched.any():
|
|
# The selection does not reach the raster at all. Widening the rule
|
|
# must never manufacture coverage that is genuinely absent.
|
|
return RasterCellSelection(
|
|
mask=mask,
|
|
mode="cell_centre",
|
|
expanded_to_touched_cells=False,
|
|
warning=None,
|
|
)
|
|
|
|
cell_count = int(touched.sum())
|
|
if cell_area_m2:
|
|
analysed_ha = cell_count * float(cell_area_m2) / 10_000.0
|
|
detail = f"{cell_count} rastercel{'' if cell_count == 1 else 'len'} ({analysed_ha:.1f} ha)"
|
|
else:
|
|
detail = f"{cell_count} rastercel{'' if cell_count == 1 else 'len'}"
|
|
return RasterCellSelection(
|
|
mask=touched,
|
|
mode="all_touched",
|
|
expanded_to_touched_cells=True,
|
|
warning=(
|
|
f"De selectie is kleiner dan één rastercel van deze bron. Het resultaat geldt voor {detail} "
|
|
"die de selectie raken, dus voor een groter gebied dan getekend."
|
|
),
|
|
)
|