Add Grid.gridify() constructor
- the purpose of this constructor method is to generate all `Pixel`s for a `Grid` that have at least one `Address` assigned to them - fix missing `UniqueConstraint` in `Grid` class => it was not possible to create two `Grid`s with the same `.side_length` in different cities - change the `City.viewport` property into two separate `City.southwest` and `City.northeast` properties; also add `City.total_x` and `City.total_y` properties for convenience
This commit is contained in:
parent
a1cbb808fd
commit
776112d609
10 changed files with 224 additions and 57 deletions
|
|
@ -1,6 +1,5 @@
|
|||
"""Provide the ORM's `Address` model."""
|
||||
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
|
@ -93,7 +92,7 @@ class Address(meta.Base):
|
|||
def location(self) -> utils.Location:
|
||||
"""The location of the address.
|
||||
|
||||
The returned `Location` object relates to `.city.viewport.southwest`.
|
||||
The returned `Location` object relates to `.city.southwest`.
|
||||
|
||||
See also the `.x` and `.y` properties that are shortcuts for
|
||||
`.location.x` and `.location.y`.
|
||||
|
|
@ -103,7 +102,7 @@ class Address(meta.Base):
|
|||
"""
|
||||
if not hasattr(self, '_location'): # noqa:WPS421 note:b1f68d24
|
||||
self._location = utils.Location(self.latitude, self.longitude)
|
||||
self._location.relate_to(self.city.as_xy_origin)
|
||||
self._location.relate_to(self.city.southwest)
|
||||
return self._location
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
"""Provide the ORM's `City` model."""
|
||||
|
||||
from typing import Dict
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
|
|
@ -69,30 +68,45 @@ class City(meta.Base):
|
|||
return self._center
|
||||
|
||||
@property
|
||||
def viewport(self) -> Dict[str, utils.Location]:
|
||||
"""Google Maps viewport of the city.
|
||||
def northeast(self) -> utils.Location:
|
||||
"""The city's northeast corner of the Google Maps viewport.
|
||||
|
||||
Implementation detail: This property is cached as none of the
|
||||
underlying attributes to calculate the value are to be changed.
|
||||
"""
|
||||
if not hasattr(self, '_viewport'): # noqa:WPS421 note:d334120e
|
||||
self._viewport = {
|
||||
'northeast': utils.Location(
|
||||
self._northeast_latitude, self._northeast_longitude,
|
||||
),
|
||||
'southwest': utils.Location(
|
||||
self._southwest_latitude, self._southwest_longitude,
|
||||
),
|
||||
}
|
||||
if not hasattr(self, '_northeast'): # noqa:WPS421 note:d334120e
|
||||
self._northeast = utils.Location(
|
||||
self._northeast_latitude, self._northeast_longitude,
|
||||
)
|
||||
|
||||
return self._viewport
|
||||
return self._northeast
|
||||
|
||||
@property
|
||||
def as_xy_origin(self) -> utils.Location:
|
||||
"""The southwest corner of the `.viewport`.
|
||||
def southwest(self) -> utils.Location:
|
||||
"""The city's southwest corner of the Google Maps viewport.
|
||||
|
||||
This property serves, for example, as the `other` argument to the
|
||||
`Location.relate_to()` method when representing an `Address`
|
||||
in the x-y plane.
|
||||
Implementation detail: This property is cached as none of the
|
||||
underlying attributes to calculate the value are to be changed.
|
||||
"""
|
||||
return self.viewport['southwest']
|
||||
if not hasattr(self, '_southwest'): # noqa:WPS421 note:d334120e
|
||||
self._southwest = utils.Location(
|
||||
self._southwest_latitude, self._southwest_longitude,
|
||||
)
|
||||
|
||||
return self._southwest
|
||||
|
||||
@property
|
||||
def total_x(self) -> int:
|
||||
"""The horizontal distance from the city's west to east end in meters.
|
||||
|
||||
The city borders refer to the Google Maps viewport.
|
||||
"""
|
||||
return self.northeast.easting - self.southwest.easting
|
||||
|
||||
@property
|
||||
def total_y(self) -> int:
|
||||
"""The vertical distance from the city's south to north end in meters.
|
||||
|
||||
The city borders refer to the Google Maps viewport.
|
||||
"""
|
||||
return self.northeast.northing - self.southwest.northing
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
"""Provide the ORM's `Grid` model."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
|
||||
from urban_meal_delivery import db
|
||||
from urban_meal_delivery.db import meta
|
||||
|
||||
|
||||
|
|
@ -27,6 +30,9 @@ class Grid(meta.Base):
|
|||
sa.ForeignKeyConstraint(
|
||||
['city_id'], ['cities.id'], onupdate='RESTRICT', ondelete='RESTRICT',
|
||||
),
|
||||
# Each `Grid`, characterized by its `.side_length`,
|
||||
# may only exists once for a given `.city`.
|
||||
sa.UniqueConstraint('city_id', 'side_length'),
|
||||
# Needed by a `ForeignKeyConstraint` in `address_pixel_association`.
|
||||
sa.UniqueConstraint('id', 'city_id'),
|
||||
)
|
||||
|
|
@ -46,3 +52,45 @@ class Grid(meta.Base):
|
|||
def pixel_area(self) -> float:
|
||||
"""The area of a `Pixel` on the grid in square kilometers."""
|
||||
return (self.side_length ** 2) / 1_000_000 # noqa:WPS432
|
||||
|
||||
@classmethod
|
||||
def gridify(cls, city: db.City, side_length: int) -> db.Grid:
|
||||
"""Create a fully populated `Grid` for a `city`.
|
||||
|
||||
The created `Grid` contains only the `Pixel`s for which
|
||||
there is at least one `Address` in it.
|
||||
|
||||
Args:
|
||||
city: city for which the grid is created
|
||||
side_length: the length of a square `Pixel`'s side
|
||||
|
||||
Returns:
|
||||
grid: including `grid.pixels` with the associated `city.addresses`
|
||||
"""
|
||||
grid = cls(city=city, side_length=side_length)
|
||||
|
||||
# Create `Pixel` objects covering the entire `city`.
|
||||
# Note: `+1` so that `city.northeast` corner is on the grid.
|
||||
possible_pixels = [
|
||||
db.Pixel(n_x=n_x, n_y=n_y)
|
||||
for n_x in range((city.total_x // side_length) + 1)
|
||||
for n_y in range((city.total_y // side_length) + 1)
|
||||
]
|
||||
|
||||
# For convenient lookup by `.n_x`-`.n_y` coordinates.
|
||||
pixel_map = {(pixel.n_x, pixel.n_y): pixel for pixel in possible_pixels}
|
||||
|
||||
for address in city.addresses:
|
||||
# Determine which `pixel` the `address` belongs to.
|
||||
n_x = address.x // side_length
|
||||
n_y = address.y // side_length
|
||||
pixel = pixel_map[n_x, n_y]
|
||||
|
||||
# Create an association between the `address` and `pixel`.
|
||||
assoc = db.AddressPixelAssociation(address=address, pixel=pixel)
|
||||
pixel.addresses.append(assoc)
|
||||
|
||||
# Only keep `pixel`s that contain at least one `Address`.
|
||||
grid.pixels = [pixel for pixel in pixel_map.values() if pixel.addresses]
|
||||
|
||||
return grid
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue