Lazy load feature from the SQLite database

Rather than querying all the features whenever we look up area(s) let’s
only get them when we need them.

The features are really big blobs of data to pass around, so there’s a
significant performance gain to be had from doing this.
This commit is contained in:
Chris Hill-Scott
2020-08-12 09:34:13 +01:00
parent 3f9e49603d
commit adad27dadb
2 changed files with 38 additions and 24 deletions

View File

@@ -29,25 +29,19 @@ class GetItemByIdMixin:
class BroadcastArea(SortableMixin):
def __init__(self, row):
id, name, feature, simple_feature = row
self.id = id
self.name = name
self._feature = feature
self._simple_feature = simple_feature
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]})'
)
self.id, self.name = row
def __eq__(self, other):
return self.id == other.id
@property
def _feature(self):
return BroadcastAreasRepository().get_feature_for_area(self.id)
@property
def _simple_feature(self):
return BroadcastAreasRepository().get_simple_feature_for_area(self.id)
def _polygons(self, feature):
if feature['geometry']['type'] == 'MultiPolygon':
return [