- Defines Network, StopArea, StopPoints and JourneySections - Loads them from json using the build_from_json method
112 lines
3.3 KiB
C++
112 lines
3.3 KiB
C++
#include "graph.h"
|
|
|
|
namespace trenesis {
|
|
|
|
StopArea::StopArea(std::string id, std::string label, std::string name, Point coord)
|
|
: id(id), label(label), name(name), coord(coord) {
|
|
}
|
|
|
|
StopPoint::StopPoint(
|
|
std::string id,
|
|
std::string label,
|
|
std::string name,
|
|
Point coord,
|
|
StopArea* stop_area
|
|
) : id(id), label(label), name(name), coord(coord), stop_area(stop_area) {
|
|
}
|
|
|
|
Network Network::build_from_json(const json& vehicle_journeys, const json& stop_points) {
|
|
Network graph;
|
|
auto _vehicle_journeys = vehicle_journeys["vehicle_journeys"];
|
|
auto _stop_points = stop_points["stop_points"];
|
|
std::unordered_map<std::string, std::string> spoint_to_sarea;
|
|
|
|
for(auto v = _stop_points.begin(); v != _stop_points.end(); ++v) {
|
|
auto stop_point = *v;
|
|
auto stop_area = stop_point["stop_area"];
|
|
|
|
auto stop_area_id = stop_area["id"];
|
|
auto& stop_area_in_map = graph.stop_areas.try_emplace(
|
|
stop_area_id,
|
|
new StopArea(
|
|
stop_area_id,
|
|
stop_area["label"],
|
|
stop_area["name"],
|
|
Point(
|
|
std::stod((std::string) stop_area["coord"]["lon"]),
|
|
std::stod((std::string) stop_area["coord"]["lat"])
|
|
)
|
|
)
|
|
).first->second;
|
|
|
|
auto stop_point_id = stop_point["id"];
|
|
spoint_to_sarea[stop_point_id] = stop_area_id;
|
|
|
|
stop_area_in_map->stop_points.try_emplace(
|
|
stop_point_id,
|
|
new StopPoint(
|
|
stop_point_id,
|
|
stop_point["label"],
|
|
stop_point["name"],
|
|
Point(
|
|
std::stod((std::string) stop_point["coord"]["lon"]),
|
|
std::stod((std::string) stop_point["coord"]["lat"])
|
|
),
|
|
stop_area_in_map.get()
|
|
)
|
|
);
|
|
}
|
|
|
|
for(auto v = _vehicle_journeys.begin(); v != _vehicle_journeys.end(); ++v) {
|
|
std::map<std::string, JourneySection> journey;
|
|
auto stop_times = (*v)["stop_times"];
|
|
for(auto t = stop_times.begin(); t < stop_times.end(); ++t) {
|
|
auto stop_point_id = (*t)["stop_point"]["id"];
|
|
auto& stop_area = graph.stop_areas.at(
|
|
spoint_to_sarea.at(stop_point_id)
|
|
);
|
|
JourneySection js;
|
|
js.departure_time = (*t)["departure_time"];
|
|
js.arrival_time = (*t)["arrival_time"];
|
|
js.origin = stop_area->stop_points[stop_point_id].get();
|
|
|
|
journey[js.departure_time] = js;
|
|
}
|
|
if(journey.size() > 1) {
|
|
for(auto js = journey.begin(); js != --journey.end(); ++js) {
|
|
auto js_cpy = js;
|
|
++js_cpy;
|
|
auto next_section = *js_cpy;
|
|
js->second.destination = next_section.second.origin;
|
|
js->second.arrival_time = next_section.second.arrival_time;
|
|
auto& stop_area = graph.stop_areas.at(
|
|
spoint_to_sarea.at(js->second.origin->id)
|
|
);
|
|
stop_area->stop_points[js->second.origin->id]->departures
|
|
.push_back(js->second);
|
|
}
|
|
}
|
|
//auto node_in_map =
|
|
//graph.stop_areas.emplace(std::make_pair(
|
|
//node_id,
|
|
//new StopArea
|
|
//));
|
|
//if (node_in_map.second) {
|
|
//auto& network_node = node_in_map.first->second;
|
|
|
|
//network_node->id = node_id;
|
|
//network_node->name = stop_point["name"];
|
|
//network_node->label = stop_point["label"];
|
|
//network_node->coord.latitude = std::stod(
|
|
//(std::string) stop_point["coord"]["lat"]
|
|
//);
|
|
//network_node->coord.longitude = std::stod(
|
|
//(std::string) stop_point["coord"]["lon"]
|
|
//);
|
|
//}
|
|
//}
|
|
}
|
|
|
|
return graph;
|
|
}
|
|
}
|