Skip to content

Commit

Permalink
✨ Added point to road mapping function
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbrandreth committed Mar 5, 2021
1 parent 92d37d2 commit 111a584
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
38 changes: 35 additions & 3 deletions containers/cleanair/cleanair/features/point_road_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from typing import Optional, List

from sqlalchemy import func
from sqlalchemy.sql.expression import literal_column, literal
from sqlalchemy.types import Float

from ..databases import DBWriter
from ..databases.tables import MetaPoint, PointRoadMap
from ..databases.tables import MetaPoint, PointRoadMap, OSHighway
from ..decorators import db_query
from ..types.enum_types import Source

Expand All @@ -27,10 +29,40 @@ def unprocessed_ids(self, sources=None, time=None):
return unprocessed_ids_query

@db_query()
def unprocessed_counts(self):
def unprocessed_counts(self, sources=None):

ids = self.unprocessed_ids().all()
ids = self.unprocessed_ids(sources=sources).all()

with self.dbcnxn.open_session() as session:
return session.query(MetaPoint.source, func.count(MetaPoint.id).label("unprocessed")).group_by(MetaPoint.source)

@db_query()
def buffers(self, point_ids: List[str], radius: float):
with self.dbcnxn.open_session() as session:
return session.query(
MetaPoint.id.label("id"),
func.Geometry(func.ST_Buffer(func.Geography(MetaPoint.location), radius)).label("geom")
).filter(MetaPoint.id in point_ids)

@db_query()
def map_points(self, point_ids: List[str]):
with self.dbcnxn.open_session() as session:
for radius in [10, 100, 200, 500, 1000]:
buffers = self.buffers(point_ids=point_ids, radius=radius).cte("buffer")

maps = (
session.query(
buffers.c.id.label("point_id"),
OSHighway.toid.label("road_segment_id"),
literal(radius, Float).label("buffer_radius"),
func.now().label("map_datetime")
)
.filter(func.ST_Intersects(buffers.c.geom, OSHighway.geom))
.subquery()
)

self.commit_records(
maps,
on_conflict="overwrite",
table=PointRoadMap
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@


@app.command()
def points(source: List[ValidSources] = Sources):
pass
def check() -> None:
map = PointRoadMapper(secretfile=state["secretfile"])
unprocessed_ids = map.unprocessed_counts(output_type="tabulate")

typer.echo(unprocessed_ids)


@app.command()
def check() -> None:
def map(source: List[ValidSources] = Sources) -> None:
map = PointRoadMapper(secretfile=state["secretfile"])
unprocesed_ids = map.unprocessed_counts(output_type="tabulate")

typer.echo(unprocesed_ids)
unprocessed_ids = map.unprocessed_ids(sources=source).all()

map.map_points(point_ids=unprocessed_ids)

0 comments on commit 111a584

Please sign in to comment.