From e21d424f975552e7ad33a5a6d89563c474233757 Mon Sep 17 00:00:00 2001 From: Toby Lorne Date: Tue, 28 Jul 2020 18:39:23 +0100 Subject: [PATCH] broadcast-areas: (dev) add visualisation tool Signed-off-by: Toby Lorne --- .../broadcast_areas/plot-areas.py | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 notifications_utils/broadcast_areas/plot-areas.py diff --git a/notifications_utils/broadcast_areas/plot-areas.py b/notifications_utils/broadcast_areas/plot-areas.py new file mode 100755 index 000000000..b1c76adea --- /dev/null +++ b/notifications_utils/broadcast_areas/plot-areas.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +import cartopy.crs as ccrs +import cartopy.feature as cfeature +import matplotlib.pyplot as plt +import shapely.geometry as sgeom + +from random import sample + +from notifications_utils.safe_string import make_string_safe_for_id +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[1]) # noqa: T001 + + inp = input("> ") + if inp == "": + break + + aid = make_string_safe_for_id(inp) + area = BroadcastAreasRepository().get_areas([aid])[0] + + feature = area[-2] + feature_shape = sgeom.shape(feature["geometry"]) + features.append(feature_shape) + + simple_feature = area[-1] + simple_feature_shape = sgeom.shape(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, + ) + + plt.show() + + +if __name__ == '__main__': + main()