Support area aggregation for custom polygons

This commit is contained in:
Ben Thorner
2021-08-24 15:11:00 +01:00
parent a210d87dee
commit 2d912cef42
2 changed files with 68 additions and 0 deletions

View File

@@ -1,8 +1,24 @@
from app.broadcast_areas.models import CustomBroadcastArea
def aggregate_areas(areas):
areas = _convert_custom_areas_to_wards(areas)
areas = _aggregate_wards_by_local_authority(areas)
return areas
def _convert_custom_areas_to_wards(areas):
results = set()
for area in areas:
if type(area) == CustomBroadcastArea:
results |= set(area.overlapping_electoral_wards)
else:
results |= {area}
return results
def _aggregate_wards_by_local_authority(areas):
return {
area.parent if area.id.startswith('wd20-')

View File

@@ -69,3 +69,55 @@ def test_aggregate_areas(
assert sorted(
area.name for area in aggregate_areas(broadcast_message.areas)
) == expected_area_names
@pytest.mark.parametrize(('simple_polygons', 'expected_area_names'), [
(
[SKYE], [
# Area covers a single unitary authority
'Highland',
]
),
(
[BRISTOL], [
# Area covers a single unitary authority
'Bristol, City of',
]
),
(
[CHELTENHAM], [
# Area covers three lower-tier authorities
# in Gloucestershire (upper tier authority)
'Cheltenham',
'Cotswold',
'Tewkesbury',
]
),
(
[BURFORD], [
# Area covers two lower-tier authorities
# in Gloucestershire (upper tier authority)
'Cotswold',
'West Oxfordshire',
],
),
(
[SANTA_A], [
# Does not overlap with the UK
],
),
])
def test_aggregate_areas_for_custom_polygons(
simple_polygons,
expected_area_names,
):
broadcast_message = BroadcastMessage(
broadcast_message_json(
area_ids=['derived from polygons'],
simple_polygons=simple_polygons
)
)
assert sorted(
area.name for area in aggregate_areas(broadcast_message.areas)
) == expected_area_names