Add Pixel.northeast/southwest properties

- the properties are needed for the drawing functionalitites
This commit is contained in:
Alexander Hess 2021-01-26 17:02:51 +01:00
commit 605ade4078
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
5 changed files with 96 additions and 2 deletions

View file

@ -1,9 +1,11 @@
"""Provide the ORM's `Pixel` model."""
import sqlalchemy as sa
import utm
from sqlalchemy import orm
from urban_meal_delivery.db import meta
from urban_meal_delivery.db import utils
class Pixel(meta.Base):
@ -58,3 +60,48 @@ class Pixel(meta.Base):
def area(self) -> float:
"""The area of a pixel in square kilometers."""
return self.grid.pixel_area
@property
def northeast(self) -> utils.Location:
"""The pixel's northeast corner, relative to `.grid.city.southwest`.
Implementation detail: This property is cached as none of the
underlying attributes to calculate the value are to be changed.
"""
if not hasattr(self, '_northeast'): # noqa:WPS421 note:d334120e
# The origin is the southwest corner of the `.grid.city`'s viewport.
easting_origin = self.grid.city.southwest.easting
northing_origin = self.grid.city.southwest.northing
# `+1` as otherwise we get the pixel's `.southwest` corner.
easting = easting_origin + ((self.n_x + 1) * self.side_length)
northing = northing_origin + ((self.n_y + 1) * self.side_length)
zone, band = self.grid.city.southwest.zone_details
latitude, longitude = utm.to_latlon(easting, northing, zone, band)
self._northeast = utils.Location(latitude, longitude)
self._northeast.relate_to(self.grid.city.southwest)
return self._northeast
@property
def southwest(self) -> utils.Location:
"""The pixel's northeast corner, relative to `.grid.city.southwest`.
Implementation detail: This property is cached as none of the
underlying attributes to calculate the value are to be changed.
"""
if not hasattr(self, '_southwest'): # noqa:WPS421 note:d334120e
# The origin is the southwest corner of the `.grid.city`'s viewport.
easting_origin = self.grid.city.southwest.easting
northing_origin = self.grid.city.southwest.northing
easting = easting_origin + (self.n_x * self.side_length)
northing = northing_origin + (self.n_y * self.side_length)
zone, band = self.grid.city.southwest.zone_details
latitude, longitude = utm.to_latlon(easting, northing, zone, band)
self._southwest = utils.Location(latitude, longitude)
self._southwest.relate_to(self.grid.city.southwest)
return self._southwest

View file

@ -2,7 +2,7 @@
from __future__ import annotations
from typing import Optional
from typing import Optional, Tuple
import utm
@ -82,6 +82,11 @@ class Location:
"""The UTM zone of the location."""
return f'{self._zone}{self._band}'
@property
def zone_details(self) -> Tuple[int, str]:
"""The UTM zone of the location as the zone number and the band."""
return (self._zone, self._band)
def __eq__(self, other: object) -> bool:
"""Check if two `Location` objects are the same location."""
if not isinstance(other, Location):