From f96b75b401a4ce6bceb3c9cf293aa46e854277fc Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Tue, 24 Aug 2021 14:36:14 +0100 Subject: [PATCH] Add method to get wards for custom polygon This will be used as part of the area aggregation in the following commits. --- app/broadcast_areas/models.py | 7 +++++++ tests/app/broadcast_areas/custom_polygons.py | 8 ++++++++ tests/app/broadcast_areas/test_models.py | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/app/broadcast_areas/test_models.py diff --git a/app/broadcast_areas/models.py b/app/broadcast_areas/models.py index 6084cd7c9..ba61fb915 100644 --- a/app/broadcast_areas/models.py +++ b/app/broadcast_areas/models.py @@ -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: diff --git a/tests/app/broadcast_areas/custom_polygons.py b/tests/app/broadcast_areas/custom_polygons.py index 6faea8675..7e8ceda16 100644 --- a/tests/app/broadcast_areas/custom_polygons.py +++ b/tests/app/broadcast_areas/custom_polygons.py @@ -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], +] diff --git a/tests/app/broadcast_areas/test_models.py b/tests/app/broadcast_areas/test_models.py new file mode 100644 index 000000000..23b9933d2 --- /dev/null +++ b/tests/app/broadcast_areas/test_models.py @@ -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