Add method to get wards for custom polygon

This will be used as part of the area aggregation in the following
commits.
This commit is contained in:
Ben Thorner
2021-08-24 14:36:14 +01:00
parent 51b786e544
commit f96b75b401
3 changed files with 35 additions and 0 deletions

View File

@@ -162,6 +162,13 @@ class CustomBroadcastArea(BaseBroadcastArea):
simple_polygons = polygons
@cached_property
def overlapping_electoral_wards(self):
return [
area for area in self.nearby_electoral_wards
if area.simple_polygons.intersects(self.polygons)
]
@cached_property
def nearby_electoral_wards(self):
if not self.polygons:

View File

@@ -13,3 +13,11 @@ SKYE = [
[57.7334, -6.8280],
[57.1004, -6.8280],
]
SANTA_A = [
[25.8890, 66.5500],
[25.8890, 66.551],
[25.8910, 66.551],
[25.8910, 66.5500],
[25.889, 66.55000],
]

View File

@@ -0,0 +1,20 @@
import pytest
from app.broadcast_areas.models import CustomBroadcastArea
from tests.app.broadcast_areas.custom_polygons import BRISTOL, SANTA_A, SKYE
@pytest.mark.parametrize(('simple_polygon', 'expected_wards_length'), [
(SKYE, 2),
(BRISTOL, 12),
(SANTA_A, 0) # does not overlap with UK
])
def test_custom_broadcast_area_overlapping_electoral_wards(
simple_polygon,
expected_wards_length,
):
custom_area = CustomBroadcastArea(
name='foo', polygons=[simple_polygon]
)
assert len(custom_area.overlapping_electoral_wards) == expected_wards_length