Files
notifications-admin/app/broadcast_areas/plot-areas.py
Chris Hill-Scott 969e7a6dbd Show how a broadcast will overspill selected area
Broadcasting is not a precise technology, because:
- cell towers are directional
- their range varies depending on whether they are 2, 3, 4, or 5G
  (the higher the bandwidth the shorter the range)
- in urban areas the towers are more densely packed, so a phone is
  likely to have a greater choice of tower to connect to, and will
  favour a closer one (which has a stronger signal)
- topography and even weather can affect the range of a tower

So it’s good for us to visually indicate that the broadcast is not as
precise as the boundaries of the area, because it gives the person
sending the message an indication of how the technology works.

At the same time we have a restriction on the number of polygons we
think and area can have, so we’ve done some work to make versions of
polygons which are simplified and buffered (see
https://github.com/alphagov/notifications-utils/pull/769 for context).

Serendipitously, the simplified and buffered polygons are larger and
smoother than the detailed polygons we’ve got from the GeoJSON files. So
they naturally give the impression of covering an area which is wider
and less precise.

So this commit takes those simple polygons and uses them to render the
blue fill. This makes the blue fill extend outside the black stroke,
which is still using the detailed polygons direct from the GeoJSON.
2020-08-13 11:20:49 +01:00

100 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python
from random import sample
import geojson
import shapely.geometry as sgeom
from notifications_utils.safe_string import make_string_safe_for_id
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from repo import BroadcastAreasRepository
def main():
print() # noqa: T001
print("pick a library") # noqa: T001
for library in BroadcastAreasRepository().get_libraries():
print(" ", library) # noqa: T001
library = input("> ")
lid = make_string_safe_for_id(library)
features = []
simple_features = []
inp = ""
while True:
print() # noqa: T001
print("pick an area, or press enter to skip") # noqa: T001
all_areas = BroadcastAreasRepository().get_all_areas_for_library(lid)
some_areas = sample(all_areas, min(len(all_areas), 25))
for area in some_areas:
print(" ", area[0], area[1]) # noqa: T001
inp = input("> ")
if inp == "":
break
aid = inp.strip()
area = BroadcastAreasRepository().get_areas([aid])[0]
feature = area[-2]
feature_shape = sgeom.shape(geojson.loads(feature)["geometry"])
features.append(feature_shape)
simple_feature = area[-1]
simple_feature_shape = sgeom.shape(geojson.loads(simple_feature)["geometry"])
simple_features.append(simple_feature_shape)
print() # noqa: T001
print("Plotting") # noqa: T001
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent([-20, 5, 40, 60], crs=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.add_geometries(
features,
ccrs.PlateCarree(),
facecolor='#00ff00',
alpha=0.25,
)
ax.add_geometries(
simple_features,
ccrs.PlateCarree(),
facecolor='#0000ff',
alpha=0.25,
)
ax.scatter(
[
p[0]
for f in simple_features
for geom in (f.geoms if hasattr(f, 'geoms') else [f])
for p in geom.exterior.coords
],
[
p[1]
for f in simple_features
for geom in (f.geoms if hasattr(f, 'geoms') else [f])
for p in geom.exterior.coords
],
transform=ccrs.PlateCarree(),
)
plt.show()
if __name__ == '__main__':
main()