Files
notifications-admin/notifications_utils/broadcast_areas/__init__.py

156 lines
4.3 KiB
Python
Raw Normal View History

import geojson
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
import itertools
from werkzeug.utils import cached_property
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
from notifications_utils.serialised_model import SerialisedModelCollection
from notifications_utils.safe_string import make_string_safe_for_id
from .repo import BroadcastAreasRepository
class SortableMixin:
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
def __repr__(self):
return f'{self.__class__.__name__}(<{self.id}>)'
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
def __lt__(self, other):
# Implementing __lt__ means any classes inheriting from this
# method are sortable
return self.name < other.name
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
class GetItemByIdMixin:
def get(self, id):
for item in self:
if item.id == id:
return item
raise KeyError(id)
class BroadcastArea(SortableMixin):
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
def __init__(self, row):
id, name, feature, simple_feature = row
self.id = id
self.name = name
self._feature = feature
self._simple_feature = simple_feature
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
for coordinates in self.polygons:
if coordinates[0] != coordinates[-1]:
# The CAP XML format requires shapes to be closed
raise ValueError(
f'Area {self.name} is not a closed shape '
f'({coordinates[0]}, {coordinates[-1]})'
)
def __eq__(self, other):
return self.id == other.id
def _polygons(self, feature):
if feature['geometry']['type'] == 'MultiPolygon':
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
return [
polygons[0]
for polygons in feature['geometry']['coordinates']
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
]
if feature['geometry']['type'] == 'Polygon':
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
return [
feature['geometry']['coordinates'][0]
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
]
raise TypeError(
f'Unknown geometry type {self.feature["geometry"]["type"]} '
f'in {self.__class__.__name} {self.name}'
)
def _unenclosed_polygons(self, feature):
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
# Some mapping tools require shapes to be unenclosed, i.e. the
# last point joins the first point implicitly
return [
coordinates[:-1] for coordinates in self._polygons(feature)
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
]
@property
def polygons(self):
return self._polygons(self.feature)
@property
def unenclosed_polygons(self):
return self._unenclosed_polygons(self.feature)
@property
def simple_polygons(self):
return self._polygons(self.simple_feature)
@property
def simple_unenclosed_polygons(self):
return self._unenclosed_polygons(self.simple_feature)
@cached_property
def feature(self):
return geojson.loads(self._feature)
@cached_property
def simple_feature(self):
return geojson.loads(self._simple_feature)
@property
def sub_areas(self):
return [
BroadcastArea(row)
for row in BroadcastAreasRepository().get_all_areas_for_group(self.id)
]
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
class BroadcastAreaLibrary(SerialisedModelCollection, SortableMixin, GetItemByIdMixin):
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
model = BroadcastArea
def __init__(self, row):
id, name, is_group = row
self.id = id
self.name = name
self.is_group = bool(is_group)
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
def get_examples(self):
return BroadcastAreasRepository().get_library_description(self.id)
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
@property
def items(self):
return BroadcastAreasRepository().get_all_areas_for_library(self.id)
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
class BroadcastAreaLibraries(SerialisedModelCollection, GetItemByIdMixin):
model = BroadcastAreaLibrary
def __init__(self):
self.libraries = BroadcastAreasRepository().get_libraries()
self.items = self.libraries
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
def get_areas(self, *area_ids):
# allow people to call `get_areas('a', 'b') or get_areas(['a', 'b'])`
if len(area_ids) == 1 and isinstance(area_ids[0], list):
area_ids = area_ids[0]
areas = BroadcastAreasRepository().get_areas(area_ids)
return [BroadcastArea(area) for area in areas]
Add broadcast area model, loading from GeoJSON This commit adds a new model class which can be used by any app to interact with a broadcast area. A broadcast area is one or more polygons representing geographical areas. It also adds some models that make browsing collections of these areas more straightforward. So the hierarchy looks like: > **BroadcastAreaLibraries* > Contains multiple libraries of broadcast area > > **BroadcastAreaLibrary** > > A collection of geographic areas, all of the same type, for example > > counties or electoral wards > > **BroadcastArea** > > Contains one or more shapes that make up an area, for example > > England > > > **BroadcastArea.polygons[n]** > > > A single shape, for example the Isle of Wight or Lindisfarne > > > > **BroadcastArea.polygons[n][o]** > > > > A single coordinate along a polygons The classes support iteration, so all the areas in a library can be looped over, for example if `countries` is an instance of `BroadcastAreaLibrary` you can do: ```python for country in countries: print(country.name) ``` The `BroadcastAreaLibraries` class also provides some useful methods for quickly getting the polygons for an area or areas, for example to render them on a map. So if `libraries` is an instance of `BroadcastAreaLibraries` you can do: ```python libraries.get_polygons_for_areas_long_lat('england', 'wales') ``` This will give polygons for the Welsh mainland, the Isle of Wight, Anglesey, etc. The models load data from GeoJSON files, which is an open standard for serialising geographic data. I’ve added a few example files taken from http://geoportal.statistics.gov.uk to show how it works.
2020-07-06 10:53:40 +01:00
def get_polygons_for_areas_long_lat(self, *area_ids):
return list(itertools.chain(*(
area.polygons
for area in self.get_areas(*area_ids)
)))
def get_polygons_for_areas_lat_long(self, *area_ids):
return [
[[long, lat] for lat, long in polygon]
for polygon in self.get_polygons_for_areas_long_lat(*area_ids)
]
broadcast_area_libraries = BroadcastAreaLibraries()