Skip to content

Commit

Permalink
split subway and bus into two transportation
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchenplus committed Nov 12, 2024
1 parent 23b6c2b commit 87d53e8
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 209 deletions.
1 change: 0 additions & 1 deletion mosstool/map/_map_util/aois/append_aois_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ def _process_matched_result(aoi, d_matched, w_matched):
if w_positions:
aoi["external"]["walking_distances"] = [x[0] for x in w_externals]
aoi["external"]["walking_lane_project_point"] = [x[1] for x in w_externals]
aoi["subline_ids"] = []
return aoi


Expand Down
11 changes: 10 additions & 1 deletion mosstool/map/_map_util/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import numpy as np
import pycityproto.city.map.v2.map_pb2 as mapv2

EPS = 1e-4

Expand All @@ -15,7 +16,7 @@
POI_START_ID = 7_0000_0000
PUBLIC_LINE_START_ID = 9_0000_0000

SUBWAY_HEIGHT_OFFSET = -20 # underground depth of subway
SUBWAY_HEIGHT_OFFSET = -20 # underground depth of subway

CLUSTER_PENALTY = 0.01 # Clustering penalty term
UP_THRESHOLD = 0.01 # Select the one with the largest number of clusters within the threshold range
Expand Down Expand Up @@ -120,3 +121,11 @@
"MAIN_SMALL_RIGHT": 1,
"MAIN_LARGE_RIGHT": 2,
}
STRING_SUBLINE_TYPE_TO_ENUM_TYPE = {
"BUS": mapv2.SUBLINE_TYPE_BUS,
"SUBWAY": mapv2.SUBLINE_TYPE_SUBWAY,
}
ENUM_STATION_CAPACITY = {
mapv2.SUBLINE_TYPE_BUS: 35,
mapv2.SUBLINE_TYPE_SUBWAY: 850,
}
89 changes: 57 additions & 32 deletions mosstool/map/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sklearn.cluster import KMeans

from ...type import Map
from ...util.format_converter import pb2dict
from .._map_util.aois import add_aoi_pop, add_aoi_to_map, generate_aoi_poi
from .._map_util.aois.convert_aoi_poi import convert_aoi, convert_poi
from .._map_util.aois.reuse_aois_matchers import match_map_aois
Expand Down Expand Up @@ -229,6 +230,7 @@ def __init__(
self.ways[feature["id"]] = feature
self.uid_mapping[feature["uid"]] = feature["id"]
self.junction_types = defaultdict(list)
self.dict_sublines_list: List[Dict] = []
elif net_type == Map:
logging.info("Reading from pb files")
self.net = net
Expand Down Expand Up @@ -335,6 +337,8 @@ def __init__(
self.min_lon, self.min_lat = self.projector(
net.header.west, net.header.south, inverse=True
)
assert type(net) == Map
self.dict_sublines_list: List[Dict] = list(pb2dict(net)["sublines"])
else:
raise ValueError(f"Unsupported data type {net_type}")

Expand Down Expand Up @@ -3836,6 +3840,18 @@ def _add_traffic_light(self):
def _add_public_transport(self) -> Set[int]:
if self.public_transport is None:
return set()
if self.dict_sublines_list:
_subway_sublines = [
sl
for sl in self.dict_sublines_list
if sl["type"] == mapv2.SUBLINE_TYPE_SUBWAY
]
return {
rid
for sl in _subway_sublines
for road_ids_dict in sl["station_connection_road_ids"]
for rid in road_ids_dict["road_ids"]
}
logging.info("Adding public transport to map")
public_road_uids: Set[int] = set()
connected_subway_lane_pairs: Set[Tuple[LineString, LineString]] = set()
Expand All @@ -3844,7 +3860,9 @@ def _add_public_transport(self) -> Set[int]:
projector is not None
), "building with public_transport must provide projstr!"

def create_subway_connections(geos: list, stas: list) -> List[List[int]]:
def create_subway_connections(
geos: list, stas: list
) -> List[Dict[str, List[int]]]:
# Create new subway lane road and junction
station_connection_road_ids = []
next_way_id = max(self.map_roads.keys()) + 1
Expand Down Expand Up @@ -4023,7 +4041,11 @@ def create_subway_connections(geos: list, stas: list) -> List[List[int]]:
"parent_name": lname,
}
public_road_uids.update(
{rid for part in station_connection_road_ids for rid in part}
{
rid
for part in station_connection_road_ids
for rid in part["road_ids"]
}
)
line_id += 1

Expand Down Expand Up @@ -4269,42 +4291,45 @@ def get_output_map(self, name: str):
self.output_aois[uid] = d
# output public transport
self.output_public_transport = {}
for line_type, lines in public_lines.items():
for _, sublines in lines.items():
matched_lines = []
for _, subline in sublines.items():
aoi_ids = []
slname = subline["name"]
sl_parent_name = subline["parent_name"]
schedules = subline["schedules"]
if not schedules:
schedules = DEFAULT_SCHEDULES[line_type]
station_connection_road_ids = subline["station_connection_road_ids"]
for sta_id in subline["raw_station_ids"]:
sta = public_stations[line_type][sta_id]
if not "aoi_uid" in sta:
continue
else:
aoi_id = sta["aoi_uid"]
aoi_ids.append(aoi_id)
if len(aoi_ids) > 1:
matched_lines.append(
{
if self.dict_sublines_list is None:
# If public transportation simulation is required, the map needs to be post-processed
logging.info("Making output public transport")
for line_type, lines in public_lines.items():
for _, sublines in lines.items():
for _, subline in sublines.items():
aoi_ids = []
slname = subline["name"]
sl_parent_name = subline["parent_name"]
schedules = subline["schedules"]
if not schedules:
schedules = DEFAULT_SCHEDULES[line_type]
station_connection_road_ids = subline[
"station_connection_road_ids"
]
for sta_id in subline["raw_station_ids"]:
sta = public_stations[line_type][sta_id]
if not "aoi_uid" in sta:
continue
else:
aoi_id = sta["aoi_uid"]
aoi_ids.append(aoi_id)
if len(aoi_ids) > 1:
# set field TAZ as empty
self.output_public_transport[self.public_transport_uid] = {
"id": self.public_transport_uid,
"name": slname,
"aoi_ids": aoi_ids,
"station_connection_road_ids": station_connection_road_ids,
"type": STRING_SUBLINE_TYPE_TO_ENUM_TYPE[line_type],
"parent_name": sl_parent_name,
"schedules": schedules,
}
)
if len(matched_lines) >= 1:
self.output_public_transport[self.public_transport_uid] = {
"sublines": matched_lines,
"type": line_type,
}
self.public_transport_uid += 1
# If public transportation simulation is required, the map needs to be post-processed
logging.info("Making output public transport")
self.public_transport_uid += 1
else:
logging.info("Making output public transport from input `Map`!")
self.output_public_transport = {
sl["id"]: sl for sl in self.dict_sublines_list
}
# output poi
logging.info("Making output pois")
self.output_pois = {uid: d for uid, d in self.map_pois.items()}
Expand Down
Loading

0 comments on commit 87d53e8

Please sign in to comment.