Add ORM models for the pixel grids
- add Grid, Pixel, and AddressPixelAssociation ORM models - each Grid belongs to a City an is characterized by the side_length of all the square Pixels contained in it - Pixels aggregate Addresses => many-to-many relationship (that is modeled with SQLAlchemy's Association Pattern to implement a couple of constraints)
This commit is contained in:
parent
6cb4be80f6
commit
f996376b13
15 changed files with 665 additions and 36 deletions
48
src/urban_meal_delivery/db/grids.py
Normal file
48
src/urban_meal_delivery/db/grids.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
"""Provide the ORM's `Grid` model."""
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
|
||||
from urban_meal_delivery.db import meta
|
||||
|
||||
|
||||
class Grid(meta.Base):
|
||||
"""A grid of `Pixel`s to partition a `City`.
|
||||
|
||||
A grid is characterized by the uniform size of the `Pixel`s it contains.
|
||||
That is configures via the `Grid.side_length` attribute.
|
||||
"""
|
||||
|
||||
__tablename__ = 'grids'
|
||||
|
||||
# Columns
|
||||
id = sa.Column( # noqa:WPS125
|
||||
sa.SmallInteger, primary_key=True, autoincrement=True,
|
||||
)
|
||||
_city_id = sa.Column('city_id', sa.SmallInteger, nullable=False)
|
||||
side_length = sa.Column(sa.SmallInteger, nullable=False, unique=True)
|
||||
|
||||
# Constraints
|
||||
__table_args__ = (
|
||||
sa.ForeignKeyConstraint(
|
||||
['city_id'], ['cities.id'], onupdate='RESTRICT', ondelete='RESTRICT',
|
||||
),
|
||||
# Needed by a `ForeignKeyConstraint` in `address_pixel_association`.
|
||||
sa.UniqueConstraint('id', 'city_id'),
|
||||
)
|
||||
|
||||
# Relationships
|
||||
city = orm.relationship('City', back_populates='grids')
|
||||
pixels = orm.relationship('Pixel', back_populates='grid')
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""Non-literal text representation."""
|
||||
return '<{cls}: {area}>'.format(
|
||||
cls=self.__class__.__name__, area=self.pixel_area,
|
||||
)
|
||||
|
||||
# Convenience properties
|
||||
@property
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue