-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_export.cpp
More file actions
68 lines (53 loc) · 2.01 KB
/
graph_export.cpp
File metadata and controls
68 lines (53 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "./TFTFGraph/TFTFGraph.h"
#include "./TFTFGraph/Helpers/helpers.h"
#include "json.hpp"
using json = nlohmann::json;
// Load GeoJSON routes into the graph
void loadRoutesFromGeoJSON(const std::string &filepath, TFTFGraph &graph) {
std::ifstream file(filepath);
if (!file.is_open()) {
std::cerr << "Failed to open GeoJSON file!" << std::endl;
return;
}
json geojson;
file >> geojson;
const auto &features = geojson["features"];
int routeId = 0;
for (const auto &feature : features) {
if (feature["geometry"]["type"] != "LineString")
continue;
std::vector<Coordinate> routePath;
const auto &coords = feature["geometry"]["coordinates"];
for (const auto &coord : coords) {
double lon = coord[0];
double lat = coord[1];
routePath.emplace_back(Coordinate{lat, lon});
}
std::string routeName = "Route_" + std::to_string(routeId);
if (feature.contains("properties") && feature["properties"].contains("name")) {
routeName = feature["properties"]["name"];
}
graph.addRoute(routeId, routeName);
graph.setRoutePath(routeId, routePath);
routeId++;
}
}
int main() {
const std::string geojsonInput = "routes.geojson";
const std::string outputFilename = "graph.json";
const float transferRangeMeters = 300.0f;
TFTFGraph graph;
std::cout << "Loading routes from " << geojsonInput << "...\n";
loadRoutesFromGeoJSON(geojsonInput, graph);
std::cout << "Creating transfers within " << transferRangeMeters << " meters...\n";
graph.createTransfersFromCoordinates(transferRangeMeters);
std::cout << "Saving serialized graph to " << outputFilename << "...\n";
saveGraphToDisk(graph, outputFilename);
std::cout << "Done.\n";
return 0;
}
// g++ -std=c++17 graph_export.cpp TFTFGraph/TFTFGraph.cpp TFTFGraph/Helpers/helpers.cpp -o graph_export