✨ Internal train network structure
- Defines Network, StopArea, StopPoints and JourneySections - Loads them from json using the build_from_json method
This commit is contained in:
parent
27c5a55400
commit
61aec3d856
9 changed files with 1877 additions and 0 deletions
7
lib/trenesis/geojson.cpp
Normal file
7
lib/trenesis/geojson.cpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#include "geojson.h"
|
||||
|
||||
namespace trenesis {
|
||||
Point::Point(double longitude, double latitude)
|
||||
: longitude(longitude), latitude(latitude) {
|
||||
}
|
||||
}
|
||||
10
lib/trenesis/geojson.h
Normal file
10
lib/trenesis/geojson.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
namespace trenesis {
|
||||
struct Point {
|
||||
double longitude;
|
||||
double latitude;
|
||||
|
||||
Point(double longitude, double latitude);
|
||||
};
|
||||
}
|
||||
112
lib/trenesis/network/graph.cpp
Normal file
112
lib/trenesis/network/graph.cpp
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#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;
|
||||
}
|
||||
}
|
||||
54
lib/trenesis/network/graph.h
Normal file
54
lib/trenesis/network/graph.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "trenesis/geojson.h"
|
||||
|
||||
namespace trenesis {
|
||||
using json = nlohmann::json;
|
||||
|
||||
struct StopPoint;
|
||||
|
||||
struct StopArea {
|
||||
std::string id;
|
||||
std::string label;
|
||||
std::string name;
|
||||
Point coord;
|
||||
|
||||
std::unordered_map<std::string, std::unique_ptr<StopPoint>> stop_points;
|
||||
|
||||
StopArea(std::string id, std::string label, std::string name, Point coord);
|
||||
};
|
||||
|
||||
struct JourneySection {
|
||||
StopPoint* origin;
|
||||
StopPoint* destination;
|
||||
// TODO: use real types for times
|
||||
std::string departure_time;
|
||||
std::string arrival_time;
|
||||
};
|
||||
|
||||
struct StopPoint {
|
||||
std::string id;
|
||||
std::string label;
|
||||
std::string name;
|
||||
Point coord;
|
||||
StopArea* stop_area;
|
||||
|
||||
std::vector<JourneySection> departures;
|
||||
|
||||
StopPoint(
|
||||
std::string id,
|
||||
std::string label,
|
||||
std::string name,
|
||||
Point coord,
|
||||
StopArea* stop_area
|
||||
);
|
||||
};
|
||||
|
||||
struct Network {
|
||||
std::unordered_map<std::string, std::unique_ptr<StopArea>> stop_areas;
|
||||
|
||||
static Network build_from_json(const json& vehicle_journeys, const json& stop_points);
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue