-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathosm2gmns_lib.cpp
150 lines (132 loc) · 6.45 KB
/
osm2gmns_lib.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Created by Jiawei Lu on 2/17/23.
//
#include <absl/base/log_severity.h>
#include <absl/container/flat_hash_map.h>
#include <absl/container/flat_hash_set.h>
#include <absl/log/globals.h>
#include <absl/log/initialize.h>
#include <absl/log/log.h>
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
#include "src/config.h"
#include "src/functions.h"
#include "src/io.h"
#include "src/networks.h"
#include "src/osmconfig.h"
#ifdef _WIN32
#define C_API __declspec(dllexport)
#else
#define C_API
#endif
absl::flat_hash_set<HighWayLinkType> parseLinkTypes(const char** link_types_val, size_t link_types_len) {
absl::flat_hash_set<HighWayLinkType> link_types;
link_types.reserve(link_types_len);
for (size_t idx = 0; idx < link_types_len; ++idx) {
const std::string& link_type_str = link_types_val[idx]; // NOLINT
const HighWayLinkType link_type = highwayStringToHighWayLinkType(link_type_str);
if (link_type != HighWayLinkType::OTHER) {
link_types.insert(link_type);
} else {
LOG(WARNING) << "unrecogonized link_type " << link_type_str;
}
}
return link_types;
}
absl::flat_hash_set<ModeType> parseModeTypes(const char** mode_types_val, size_t mode_types_len) {
absl::flat_hash_set<ModeType> mode_types;
mode_types.reserve(mode_types_len);
for (size_t idx = 0; idx < mode_types_len; ++idx) {
const std::string& mode_type_str = mode_types_val[idx]; // NOLINT
const ModeType mode_type = modeStringToModeType(mode_type_str);
if (mode_type != ModeType::OTHER) {
mode_types.insert(mode_type);
} else {
LOG(WARNING) << "unrecogonized mode_type " << mode_type_str;
}
}
return mode_types;
}
std::vector<std::string> parseCharArraysToStringVector(const char** chars_val, size_t chars_len) {
std::vector<std::string> char_vector;
char_vector.reserve(chars_len);
for (size_t idx = 0; idx < chars_len; ++idx) {
char_vector.emplace_back(chars_val[idx]); // NOLINT
}
return char_vector;
}
template <typename T>
struct StrNumDict {
const char* key;
T value;
};
template <typename T>
absl::flat_hash_map<HighWayLinkType, T> parseLinkTypeToNumDict(const StrNumDict<T>* dict_val, size_t dict_len) {
absl::flat_hash_map<HighWayLinkType, T> dict;
for (size_t idx = 0; idx < dict_len; ++idx) {
const std::string& link_type_str = dict_val[idx].key; // NOLINT
const HighWayLinkType link_type = highwayStringToHighWayLinkType(link_type_str);
if (link_type != HighWayLinkType::OTHER) {
dict.emplace(link_type, dict_val[idx].value); // NOLINT
} else {
LOG(WARNING) << "unrecogonized link_type " << link_type_str;
}
}
return dict;
}
extern "C" {
C_API void initializeAbslLoggingPy() {
absl::InitializeLog();
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
};
C_API void releaseNetworkMemoryPy(Network* network) { delete network; };
C_API Network* getNetFromFilePy(const char* osm_filepath, const char** mode_types_val, size_t mode_types_len,
const char** link_types_val, size_t link_types_len,
const char** connector_link_types_val, size_t connector_link_types_len, bool POI,
float POI_sampling_ratio, const char** osm_node_attributes_val,
size_t osm_node_attributes_len, const char** osm_link_attributes_val,
size_t osm_link_attributes_len, const char** osm_poi_attributes_val,
size_t osm_poi_attributes_len, bool strict_boundary) {
const absl::flat_hash_set<ModeType> mode_types = parseModeTypes(mode_types_val, mode_types_len);
const absl::flat_hash_set<HighWayLinkType> link_types = parseLinkTypes(link_types_val, link_types_len);
const absl::flat_hash_set<HighWayLinkType> connector_link_types =
parseLinkTypes(connector_link_types_val, connector_link_types_len);
auto* osm_parsing_config =
new OsmParsingConfig{parseCharArraysToStringVector(osm_node_attributes_val, osm_node_attributes_len),
parseCharArraysToStringVector(osm_link_attributes_val, osm_link_attributes_len),
parseCharArraysToStringVector(osm_poi_attributes_val, osm_poi_attributes_len)};
Network* network = getNetFromFile(osm_filepath, mode_types, link_types, connector_link_types, POI, POI_sampling_ratio,
osm_parsing_config, strict_boundary);
return network;
};
C_API void consolidateComplexIntersectionsPy(Network* network, bool auto_identify, const char* intersection_file,
float int_buffer) {
consolidateComplexIntersections(network, auto_identify, intersection_file, int_buffer);
};
C_API void generateNodeActivityInfoPy(Network* network, const char* zone_file) {
generateNodeActivityInfo(network, zone_file);
};
C_API void fillLinkAttributesWithDefaultValuesPy(Network* network, bool default_lanes,
const StrNumDict<int32_t>* default_lanes_dict_val,
size_t default_lanes_dict_len, bool default_speed,
const StrNumDict<float>* default_speed_dict_val,
size_t default_speed_dict_len, bool default_capacity,
const StrNumDict<int32_t>* default_capacity_dict_val,
size_t default_capacity_dict_len) {
const absl::flat_hash_map<HighWayLinkType, int32_t> default_lanes_dict =
parseLinkTypeToNumDict<int32_t>(default_lanes_dict_val, default_lanes_dict_len);
const absl::flat_hash_map<HighWayLinkType, float> default_speed_dict =
parseLinkTypeToNumDict<float>(default_speed_dict_val, default_speed_dict_len);
const absl::flat_hash_map<HighWayLinkType, int32_t> default_capacity_dict =
parseLinkTypeToNumDict<int32_t>(default_capacity_dict_val, default_capacity_dict_len);
fillLinkAttributesWithDefaultValues(network, default_lanes, default_lanes_dict, default_speed, default_speed_dict,
default_capacity, default_capacity_dict);
}
C_API void outputNetToCSVPy(const Network* network, const char* output_folder) {
outputNetToCSV(network, output_folder);
};
C_API size_t getNumberOfNodesPy(const Network* network) { return network->numberOfNodes(); };
C_API size_t getNumberOfLinksPy(const Network* network) { return network->numberOfLinks(); };
}