mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-11 18:37:33 -04:00
Refactor coordinate processing into Polygons class
We have a bunch of stuff for doing lat/long transformation in the `BroadcastMessage` class. This is not a good separation of concerns, now that we have a separate class for dealing with polygons and coordinates.
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import itertools
|
||||
|
||||
from notifications_utils.serialised_model import SerialisedModelCollection
|
||||
from werkzeug.utils import cached_property
|
||||
|
||||
from .polygons import Polygons
|
||||
from .repo import BroadcastAreasRepository
|
||||
|
||||
|
||||
@@ -35,11 +34,15 @@ class BroadcastArea(SortableMixin):
|
||||
|
||||
@cached_property
|
||||
def polygons(self):
|
||||
return BroadcastAreasRepository().get_polygons_for_area(self.id)
|
||||
return Polygons(
|
||||
BroadcastAreasRepository().get_polygons_for_area(self.id)
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def simple_polygons(self):
|
||||
return BroadcastAreasRepository().get_simple_polygons_for_area(self.id)
|
||||
return Polygons(
|
||||
BroadcastAreasRepository().get_simple_polygons_for_area(self.id)
|
||||
)
|
||||
|
||||
@property
|
||||
def sub_areas(self):
|
||||
@@ -80,29 +83,5 @@ class BroadcastAreaLibraries(SerialisedModelCollection, GetItemByIdMixin):
|
||||
areas = BroadcastAreasRepository().get_areas(area_ids)
|
||||
return [BroadcastArea(area) for area in areas]
|
||||
|
||||
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)
|
||||
]
|
||||
|
||||
def get_simple_polygons_for_areas_long_lat(self, *area_ids):
|
||||
return list(itertools.chain(*(
|
||||
area.simple_polygons
|
||||
for area in self.get_areas(*area_ids)
|
||||
)))
|
||||
|
||||
def get_simple_polygons_for_areas_lat_long(self, *area_ids):
|
||||
return [
|
||||
[[long, lat] for lat, long in polygon]
|
||||
for polygon in self.get_simple_polygons_for_areas_long_lat(*area_ids)
|
||||
]
|
||||
|
||||
|
||||
broadcast_area_libraries = BroadcastAreaLibraries()
|
||||
|
||||
@@ -43,7 +43,10 @@ def polygons_and_simplified_polygons(feature):
|
||||
'Polygons.perimeter_to_buffer_ratio)'
|
||||
)
|
||||
|
||||
return full_resolution.as_coordinate_pairs, simplified.as_coordinate_pairs
|
||||
return (
|
||||
full_resolution.as_coordinate_pairs_long_lat,
|
||||
simplified.as_coordinate_pairs_long_lat,
|
||||
)
|
||||
|
||||
|
||||
repo = BroadcastAreasRepository()
|
||||
|
||||
@@ -53,6 +53,9 @@ class Polygons():
|
||||
def __getitem__(self, index):
|
||||
return self.polygons[index]
|
||||
|
||||
def __len__(self):
|
||||
return len(self.polygons)
|
||||
|
||||
@cached_property
|
||||
def perimeter_length(self):
|
||||
return sum(
|
||||
@@ -141,25 +144,22 @@ class Polygons():
|
||||
])
|
||||
|
||||
@cached_property
|
||||
def as_coordinate_pairs(self):
|
||||
def as_coordinate_pairs_long_lat(self):
|
||||
return [
|
||||
[
|
||||
[x, y] for x, y in p.exterior.coords
|
||||
]
|
||||
for p in self
|
||||
[[x, y] for x, y in polygon.exterior.coords]
|
||||
for polygon in self
|
||||
]
|
||||
|
||||
@cached_property
|
||||
def as_unenclosed_coordinate_pairs(self):
|
||||
# Some mapping tools require shapes to be unenclosed, i.e. the
|
||||
# last point joins the first point implicitly
|
||||
def as_coordinate_pairs_lat_long(self):
|
||||
return [
|
||||
coordinates[:-1] for coordinates in self.as_coordinate_pairs
|
||||
[[y, x] for x, y in coordinate_pairs]
|
||||
for coordinate_pairs in self.as_coordinate_pairs_long_lat
|
||||
]
|
||||
|
||||
@cached_property
|
||||
def point_count(self):
|
||||
return len(list(itertools.chain(*self.as_coordinate_pairs)))
|
||||
return len(list(itertools.chain(*self.as_coordinate_pairs_long_lat)))
|
||||
|
||||
|
||||
def flatten_polygons(polygons):
|
||||
|
||||
Reference in New Issue
Block a user