Update utils to do linear transformation of polygons

Brings in https://github.com/alphagov/notifications-utils/pull/889/files

At the moment, we are not doing any transformation of features before
applying geometric algorithms to them. This is, in effect, assuming that
the earth is flat.

This new version of utils implements the transformation of our polygons
to a Cartesian plane. In other words, it converts them from being
defined in spherical degrees to metres.

For the admin app this means we need to convert places where the code
expects things to be measured in degrees to work in metres instead.
This commit is contained in:
Chris Hill-Scott
2021-08-03 18:46:30 +01:00
parent 6b52735dac
commit 6cb326f153
16 changed files with 99 additions and 80 deletions

View File

@@ -15,11 +15,11 @@ SKYE = [
]
SANTA_A = [
[25.8890, 66.5500],
[25.8890, 66.551],
[25.8910, 66.551],
[25.8910, 66.5500],
[25.889, 66.55000],
[66.5500, 25.8890],
[66.551, 25.8890],
[66.551, 25.8910],
[66.5500, 25.8910],
[66.55000, 25.889],
]
BURFORD = [

View File

@@ -101,7 +101,7 @@ def test_has_polygons():
assert len(scotland.polygons) == 195
assert england.polygons.as_coordinate_pairs_lat_long[0][0] == [
55.811085, -2.034358 # https://goo.gl/maps/wsf2LUWzYinwydMk8
55.81108, -2.03436 # https://goo.gl/maps/HMFHGogohXdh9ggo8
]
@@ -276,24 +276,24 @@ def test_estimate_number_of_smartphones_for_population(
@pytest.mark.parametrize('area, expected_phones_per_square_mile', (
(
# Islington (most dense in UK)
'lad20-E09000019', 21_348
'lad20-E09000019', 34_281
),
(
# Cordwainer Ward (City of London)
# This is higher than Islington because we inflate the
# popualtion to account for daytime workers
'wd20-E05009300', 310_674
'wd20-E05009300', 496_480
),
(
# Crewe East
'wd20-E05008621', 2_078),
'wd20-E05008621', 3_460),
(
# Eden (Cumbria, least dense in England)
'lad20-E07000030', 25.57
'lad20-E07000030', 44.12
),
(
# Highland (least dense in UK)
'lad20-S12000017', 4.40
'lad20-S12000017', 8.18
),
))
def test_phone_density(
@@ -305,44 +305,40 @@ def test_phone_density(
)
@pytest.mark.parametrize('area, expected_bleed_in_m, expected_bleed_in_degrees', (
@pytest.mark.parametrize('area, expected_bleed_in_m', (
(
# Islington (most dense in UK)
'lad20-E09000019', 500, 0.00449
'lad20-E09000019', 500
),
(
# Cordwainer Ward (City of London)
# Special case because of inflated daytime population
'wd20-E05009300', 500, 0.00449
'wd20-E05009300', 500
),
(
# Crewe East
'wd20-E05008621', 1_752, 0.01574
'wd20-E05008621', 1_476
),
(
# Eden (Cumbria, least dense in England)
'lad20-E07000030', 4_140, 0.0372
'lad20-E07000030', 3_844
),
(
# Highland (least dense in UK)
'lad20-S12000017', 5_000, 0.0449
'lad20-S12000017', 4_759
),
(
# No population data available
'test-santa-claus-village-rovaniemi-a', 1_500, 0.01347
'test-santa-claus-village-rovaniemi-a', 1_500
)
))
def test_estimated_bleed(
area, expected_bleed_in_m, expected_bleed_in_degrees,
area, expected_bleed_in_m
):
assert close_enough(
broadcast_area_libraries.get_areas([area])[0].estimated_bleed_in_m,
expected_bleed_in_m,
)
assert close_enough(
broadcast_area_libraries.get_areas([area])[0].estimated_bleed_in_degrees,
expected_bleed_in_degrees,
)
@pytest.mark.parametrize('polygon, expected_possible_overlaps, expected_count_of_phones', (
@@ -362,7 +358,7 @@ def test_estimated_bleed(
'Stoke Bishop',
'Windmill Hill',
],
73_119,
72_817,
),
(
SKYE,
@@ -372,7 +368,7 @@ def test_estimated_bleed(
'Na Hearadh agus Ceann a Deas nan Loch',
'Wester Ross, Strathpeffer and Lochalsh',
],
3_534,
3_413,
),
))
def test_count_of_phones_for_custom_area(

View File

@@ -5,7 +5,7 @@ from tests.app.broadcast_areas.custom_polygons import BRISTOL, SANTA_A, SKYE
@pytest.mark.parametrize(('simple_polygon', 'expected_wards_length'), [
(SKYE, 2),
(SKYE, 1),
(BRISTOL, 12),
(SANTA_A, 0) # does not overlap with UK
])

View File

@@ -837,8 +837,8 @@ def test_broadcast_page(
'England Remove England',
'Scotland Remove Scotland',
], [
'An area of 200,000 square miles Will get the alert',
'An extra area of 8,000 square miles is Likely to get the alert',
'An area of 100,000 square miles Will get the alert',
'An extra area of 6,000 square miles is Likely to get the alert',
'40,000,000 phones estimated',
]),
([
@@ -854,8 +854,8 @@ def test_broadcast_page(
'Penrith South Remove Penrith South',
'Penrith West Remove Penrith West',
], [
'An area of 6 square miles Will get the alert',
'An extra area of 20 square miles is Likely to get the alert',
'An area of 4 square miles Will get the alert',
'An extra area of 10 square miles is Likely to get the alert',
'9,000 to 10,000 phones',
]),
([
@@ -863,17 +863,17 @@ def test_broadcast_page(
], [
'Islington Remove Islington',
], [
'An area of 10 square miles Will get the alert',
'An extra area of 5 square miles is Likely to get the alert',
'200,000 to 500,000 phones',
'An area of 6 square miles Will get the alert',
'An extra area of 4 square miles is Likely to get the alert',
'200,000 to 600,000 phones',
]),
([
'ctyua19-E10000019',
], [
'Lincolnshire Remove Lincolnshire',
], [
'An area of 4,000 square miles Will get the alert',
'An extra area of 700 square miles is Likely to get the alert',
'An area of 2,000 square miles Will get the alert',
'An extra area of 500 square miles is Likely to get the alert',
'500,000 to 600,000 phones',
]),
([
@@ -882,8 +882,8 @@ def test_broadcast_page(
], [
'Lincolnshire Remove Lincolnshire', 'North Yorkshire Remove North Yorkshire',
], [
'An area of 10,000 square miles Will get the alert',
'An extra area of 2,000 square miles is Likely to get the alert',
'An area of 6,000 square miles Will get the alert',
'An extra area of 1,000 square miles is Likely to get the alert',
'1,000,000 phones estimated',
]),
))
@@ -936,7 +936,7 @@ def test_preview_broadcast_areas_page(
[[7, 8], [9, 10], [11, 12]],
],
[
'An area of 700 square miles Will get the alert',
'An area of 1,000 square miles Will get the alert',
'An extra area of 1,000 square miles is Likely to get the alert',
'Unknown number of phones',
]
@@ -944,17 +944,17 @@ def test_preview_broadcast_areas_page(
(
[BRISTOL],
[
'An area of 7 square miles Will get the alert',
'An extra area of 6 square miles is Likely to get the alert',
'An area of 4 square miles Will get the alert',
'An extra area of 3 square miles is Likely to get the alert',
'70,000 to 100,000 phones',
]
),
(
[SKYE],
[
'An area of 3,000 square miles Will get the alert',
'An extra area of 800 square miles is Likely to get the alert',
'4,000 phones estimated',
'An area of 2,000 square miles Will get the alert',
'An extra area of 600 square miles is Likely to get the alert',
'3,000 to 4,000 phones',
]
),
))

View File

@@ -45,7 +45,7 @@ def test_simple_polygons():
# Because the areas are close to each other, the simplification
# and unioning process results in a single polygon with fewer
# total coordinates
[55],
[57],
]