-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_bubbles.py
189 lines (148 loc) · 7.05 KB
/
generate_bubbles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import fiona
import requests
import zipfile
import io
from shapely.geometry import shape
from shapely import Point, GeometryCollection, LineString, MultiPolygon, union_all, minimum_rotated_rectangle, buffer, is_empty
from shapely.validation import make_valid
import os
import matplotlib.pyplot as plt
import numpy as np
import pyproj
import csv
BUBBLE_LIMIT = 200
england_shapefile_url = 'https://boundarycommissionforengland.independent.gov.uk/wp-content/uploads/2023/06/984162_2023_06_27_Final_recommendations_England_shp.zip'
scotland_shapefile_path = 'https://www.bcomm-scotland.independent.gov.uk/sites/default/files/2023_review_final/bcs_final_recs_2023_review.zip'
wales_shapefile_path = 'https://bcomm-wales.gov.uk/sites/bcomm/files/review/Shapefiles.zip'
england_shapefile_filename = '2023_06_27_Final_recommendations_England.shp'
scotland_shapefile_filename = 'All_Scotland_Final_Recommended_Constituencies_2023_Review.shp'
wales_shapefile_filename = 'Final Recs Shapefiles/Final Recommendations_region.shp'
def download_and_extract(url, path):
if not os.path.exists('data/' + path):
response = requests.get(url)
zip_file = zipfile.ZipFile(io.BytesIO(response.content))
zip_file.extractall('data/' + path)
def create_constituency_list(shapefile_path, constituency_name_key):
constituencies = []
with fiona.open('data/' + shapefile_path) as boundaries:
for constituency in boundaries:
constituency_shape = make_valid(shape(constituency['geometry']))
constituencies.append((constituency.properties[constituency_name_key], constituency_shape))
return constituencies
def calculate_radius_upper_bound(boundary):
mrr = minimum_rotated_rectangle(boundary)
x, y = mrr.exterior.xy
edge_lengths = (
Point(x[0], y[0]).distance(Point(x[1], y[1])),
Point(x[1], y[1]).distance(Point(x[2], y[2]))
)
width = min(edge_lengths)
return int((width // 2000) * 1000)
def calculate_step(polygons, radius, bubble_length):
total_polygon_length = sum([polygon.exterior.length for polygon in polygons])
iteration_bubble_count = total_polygon_length / radius
step = radius
is_last_iteration = radius == 1000 or (bubble_length + iteration_bubble_count) > BUBBLE_LIMIT
if is_last_iteration:
step = total_polygon_length / (BUBBLE_LIMIT - bubble_length)
return step
def calculate_bubbles(boundary):
radius = calculate_radius_upper_bound(boundary)
island_of_possibility = None
bubbles = []
bubblesData = []
while radius > 0 and len(bubbles) < BUBBLE_LIMIT:
island_of_possibility = buffer(boundary, -(radius + 30))
if not is_empty(island_of_possibility):
print(radius)
polygons = island_of_possibility.geoms if isinstance(island_of_possibility, MultiPolygon) else [island_of_possibility]
step = calculate_step(polygons, radius, len(bubbles))
for polygon in polygons:
for interpolation in np.arange(0, polygon.exterior.length, step):
point = polygon.exterior.interpolate(interpolation)
bubble = point.buffer(radius)
if boundary.contains(bubble):
bubbles.append(bubble)
bubblesData.append([point.x, point.y, int(radius / 1000)])
if len(bubbles) > 0:
radius = (radius // 1500) * 1000
else:
radius -= 1000
return bubbles[:BUBBLE_LIMIT], bubblesData[:BUBBLE_LIMIT]
def get_statistics_row(constituency_name, coverage_percentage, bubblesData):
statistics_row = [constituency_name, coverage_percentage]
if len(bubblesData) == 0:
return statistics_row
bubble_count_by_radius = {}
for (_, _, radius) in bubblesData:
if radius in bubble_count_by_radius:
bubble_count_by_radius[radius] += 1
else:
bubble_count_by_radius[radius] = 1
for radius in range(1, max(bubble_count_by_radius.keys()) + 1):
if radius in bubble_count_by_radius:
statistics_row.append(bubble_count_by_radius[radius])
else:
statistics_row.append(0)
return statistics_row
if __name__ == '__main__':
download_and_extract(england_shapefile_url, 'england')
download_and_extract(scotland_shapefile_path, 'scotland')
download_and_extract(wales_shapefile_path, 'wales')
england_constituencies = create_constituency_list('england/' + england_shapefile_filename, 'Constituen')
scotland_constituencies = create_constituency_list('scotland/' + scotland_shapefile_filename, 'NAME')
wales_constituencies = create_constituency_list('wales/' + wales_shapefile_filename, 'Official_N')
constituencies = england_constituencies + scotland_constituencies + wales_constituencies
if not os.path.exists('output/JPGs'):
os.makedirs('output/JPGs')
transformer = pyproj.Transformer.from_crs("epsg:27700", "epsg:4326")
with (
open('output/bubbles.csv', 'w') as csv_output,
open('output/statistics.csv', 'w') as stastics_output
):
output_writer = csv.writer(csv_output)
output_writer.writerow(['bubble', 'constituency'])
statistics_writer = csv.writer(stastics_output)
statistics_writer.writerow(['constituency', 'coverage'])
statistics = []
for constituency in constituencies:
constituency_name = constituency[0]
boundary = constituency[1]
print(constituency_name)
bubbles, bubblesData = calculate_bubbles(boundary)
for (x, y, radius) in bubblesData:
lat, long = transformer.transform(x, y)
output_writer.writerow(['({}, {}) +{}km'.format(lat, long, radius), constituency_name])
fig, ax = plt.subplots(1, 2)
ax[0].set_aspect('equal', adjustable='box')
ax[1].set_aspect('equal', adjustable='box')
fig.suptitle(constituency_name)
coverage_percentage = 100 * union_all(bubbles).area / boundary.area
statistics_writer.writerow(get_statistics_row(constituency_name, coverage_percentage, bubblesData))
statistics.append(coverage_percentage)
fig.text(0.5, 0.9, '{:.0f}% coverage'.format(coverage_percentage), ha='center', fontsize=12)
ax[0].xaxis.set_visible(False)
ax[0].yaxis.set_visible(False)
ax[1].xaxis.set_visible(False)
ax[1].yaxis.set_visible(False)
polygons = boundary.geoms if isinstance(boundary, GeometryCollection) or isinstance(boundary, MultiPolygon) else [boundary]
for polygon in polygons:
if isinstance(polygon, LineString):
continue
rings = [polygon.exterior] + [interior for interior in polygon.interiors]
for ring in rings:
x, y = ring.xy
ax[0].plot(x, y, color='blue')
ax[1].plot(x, y, color='blue')
for valid_circle in bubbles:
x, y = valid_circle.exterior.xy
ax[0].plot(x, y, color='red', linewidth=0.5)
ax[1].fill(x, y, color='red')
fig.savefig('output/JPGs/' + constituency_name + '.jpg', dpi=300)
plt.close(fig)
statistics_writer.writerow(['', ''])
statistics_writer.writerow(['mean', sum(statistics) / len(statistics)])
statistics_writer.writerow(['median', np.median(statistics)])
statistics_writer.writerow(['min', min(statistics)])
statistics_writer.writerow(['max', max(statistics)])
statistics_writer.writerow(['sigma', np.std(statistics)])