-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
from typing import Dict, List | ||
|
||
from app_and_db import db | ||
from models import RoadJunctionKM, RoadSegments | ||
|
||
|
||
class SegmentJunctions: | ||
# {<road segment id>: [<non urban junction id, ...]} | ||
__segment_junctions: Dict[int, List[int]] = {} | ||
__singelton = None | ||
|
||
def __init__(self): | ||
if not self.__segment_junctions: | ||
self.__segment_junctions = self.__calc_fill_segment_junctions() | ||
|
||
@staticmethod | ||
def get_instance(): | ||
if not SegmentJunctions.__singelton: | ||
SegmentJunctions.__singelton = SegmentJunctions() | ||
return SegmentJunctions.__singelton | ||
|
||
def get_segment_junctions(self, segment: int) -> List[int]: | ||
res = self.__segment_junctions.get(segment) | ||
if res is None: | ||
logging.warning(f"{segment}: no such segment in segment junctions data.") | ||
return res or [] | ||
|
||
@staticmethod | ||
def __calc_fill_segment_junctions(): | ||
tmp: List[RoadJunctionKM] = db.session.query(RoadJunctionKM).all() | ||
res = {} | ||
rkj = {} | ||
for t in tmp: | ||
if t.road not in rkj: | ||
rkj[t.road] = {} | ||
rkj[t.road][t.km] = t.non_urban_intersection | ||
tmp: List[RoadSegments] = db.session.query(RoadSegments).all() | ||
segments = {t.segment_id: t for t in tmp} | ||
for k, v in segments.items(): | ||
if v.road not in rkj: | ||
logging.warning(f"No junctions in road {v.road}.") | ||
continue | ||
# logger.debug(f"seg:{k},road:{v.road}:juncs:{rkj[v.road]}.") | ||
junctions = [ | ||
rkj[v.road][km] for km in rkj[v.road].keys() if v.from_km - 1 <= km <= v.to_km + 1 | ||
] | ||
# logger.debug(f"segment junctions:{junctions}") | ||
res[k] = junctions | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters