From e7ec77c5bb344f8d7e335c7c3d7151fc45e87a37 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 5 Jul 2021 15:54:06 +0100 Subject: [PATCH] Make calculating overlapping areas faster By using the simplified polygons instead of the full resolutions ones we: - query less data from SQLite - pass less data around - give Shapely a less complicated shape to do its calculations on This makes it faster to calculate how much of each electoral ward a custom area overlaps. For the two areas in our tests: Place represented by custom area | Before | After ---------------------------------|--------|-------- Bristol | 0.07s | 0.02s Skye | 0.02s | 0.01s --- app/broadcast_areas/models.py | 2 +- tests/app/broadcast_areas/test_broadcast_area.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/broadcast_areas/models.py b/app/broadcast_areas/models.py index 4af79ed54..2ce313969 100644 --- a/app/broadcast_areas/models.py +++ b/app/broadcast_areas/models.py @@ -171,7 +171,7 @@ class CustomBroadcastArea(BaseBroadcastArea): @cached_property def count_of_phones(self): return sum( - area.polygons.ratio_of_intersection_with(self.polygons) * area.count_of_phones + area.simple_polygons.ratio_of_intersection_with(self.polygons) * area.count_of_phones for area in self.overlapping_areas ) diff --git a/tests/app/broadcast_areas/test_broadcast_area.py b/tests/app/broadcast_areas/test_broadcast_area.py index 448b76f00..e637318e0 100644 --- a/tests/app/broadcast_areas/test_broadcast_area.py +++ b/tests/app/broadcast_areas/test_broadcast_area.py @@ -377,7 +377,7 @@ def test_estimated_bleed( 'Stoke Bishop', 'Windmill Hill', ], - 73_496, + 73_119, ), ( SKYE, @@ -387,7 +387,7 @@ def test_estimated_bleed( 'Na Hearadh agus Ceann a Deas nan Loch', 'Wester Ross, Strathpeffer and Lochalsh', ], - 3_517, + 3_534, ), )) def test_count_of_phones_for_custom_area(