48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
"""CLI script to create pixel grids."""
|
|
|
|
import click
|
|
|
|
from velofy import config as pkg_config
|
|
from velofy import db
|
|
from velofy.console import decorators
|
|
|
|
|
|
@click.command()
|
|
@decorators.db_revision("e86290e7305e")
|
|
def gridify() -> None: # pragma: no cover note:b1f68d24
|
|
"""Create grids for all cities.
|
|
|
|
This command creates grids with pixels of various
|
|
side lengths (specified in `velofy.config`).
|
|
|
|
Pixels are only generated if they contain at least one
|
|
(pickup or delivery) address.
|
|
|
|
All data are persisted to the database.
|
|
"""
|
|
cities = db.session.query(db.City).all()
|
|
click.echo(f"{len(cities)} cities retrieved from the database")
|
|
|
|
for city in cities:
|
|
click.echo(f"\nCreating grids for {city.name}")
|
|
|
|
for side_length in pkg_config.GRID_SIDE_LENGTHS:
|
|
click.echo(f"Creating grid with a side length of {side_length} meters")
|
|
|
|
grid = db.Grid.gridify(city=city, side_length=side_length)
|
|
db.session.add(grid)
|
|
|
|
click.echo(f" -> created {len(grid.pixels)} pixels")
|
|
|
|
# Because the number of assigned addresses is the same across
|
|
# different `side_length`s, we can take the last `grid` from the `city`.
|
|
n_assigned = (
|
|
db.session.query(db.AddressPixelAssociation)
|
|
.filter(db.AddressPixelAssociation.grid_id == grid.id)
|
|
.count()
|
|
)
|
|
click.echo(
|
|
f"=> assigned {n_assigned} out of {len(city.addresses)} addresses in {city.name}", # noqa:E501
|
|
)
|
|
|
|
db.session.commit()
|