1
+ // Copyright 2022 The Autoware Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include " map_loader/lanelet2_differential_loader_module.hpp"
16
+
17
+ #include " map_loader/lanelet2_map_loader_node.hpp"
18
+
19
+ Lanelet2DifferentialLoaderModule::Lanelet2DifferentialLoaderModule (
20
+ rclcpp::Node * node,
21
+ const std::map<std::string, Lanelet2FileMetaData> & lanelet2_file_metadata_dict)
22
+ : logger_(node->get_logger ()),
23
+ clock_(node->get_clock ()),
24
+ lanelet2_file_metadata_dict_(lanelet2_file_metadata_dict)
25
+ {
26
+ // Publish lanelet2 map meta data
27
+ {
28
+ pub_lanelet_map_meta_data_ = node->create_publisher <autoware_map_msgs::msg::LaneletMapMetaData>(
29
+ " /map/lanelet2_map_meta_data" , rclcpp::QoS{1 }.transient_local ());
30
+
31
+ const auto msg = getLaneletMapMetaDataMsg ();
32
+
33
+ pub_lanelet_map_meta_data_->publish (msg);
34
+ }
35
+
36
+ get_differential_lanelet2_maps_service_ = node->create_service <GetDifferentialLanelet2Map>(
37
+ " /map/get_differential_lanelet2_map" ,
38
+ std::bind (
39
+ &Lanelet2DifferentialLoaderModule::onServiceGetDifferentialLanelet2Map, this ,
40
+ std::placeholders::_1, std::placeholders::_2));
41
+
42
+ pub_map_bin_ = node->create_publisher <HADMapBin>(
43
+ " /map/differential_lanelet2_map_bin" , rclcpp::QoS{1 }.transient_local ());
44
+ pub_map_bin_debug_ =
45
+ node->create_publisher <HADMapBin>(" /map/vector_map" , rclcpp::QoS{1 }.transient_local ());
46
+ }
47
+
48
+ bool Lanelet2DifferentialLoaderModule::onServiceGetDifferentialLanelet2Map (
49
+ GetDifferentialLanelet2Map::Request::SharedPtr req,
50
+ GetDifferentialLanelet2Map::Response::SharedPtr res)
51
+ {
52
+ if (!differentialLanelet2Load (req, res)) {
53
+ RCLCPP_ERROR (logger_, " Failed to load differential lanelet2 map" );
54
+ return false ;
55
+ }
56
+ res->header .stamp = clock_->now ();
57
+ res->header .frame_id = " map" ;
58
+
59
+ return true ;
60
+ }
61
+
62
+ bool Lanelet2DifferentialLoaderModule::differentialLanelet2Load (
63
+ GetDifferentialLanelet2Map::Request::SharedPtr & request,
64
+ GetDifferentialLanelet2Map::Response::SharedPtr & response)
65
+ {
66
+ {
67
+ std::vector<std::string> lanelet2_paths;
68
+ for (const auto & path : lanelet2_file_metadata_dict_) {
69
+ if (!std::filesystem::exists (path.first )) {
70
+ continue ;
71
+ }
72
+ lanelet2_paths.push_back (path.first );
73
+ }
74
+ if (lanelet2_paths.empty ()) {
75
+ return false ;
76
+ }
77
+
78
+ lanelet::ErrorMessages errors{};
79
+ lanelet::projection::MGRSProjector projector{};
80
+ lanelet::io_handlers::MultiOsmParser parser (projector, {});
81
+ lanelet::LaneletMapPtr map = parser.parse (lanelet2_paths, errors);
82
+
83
+ for (lanelet::Point3d point : map->pointLayer ) {
84
+ if (point.hasAttribute (" local_x" )) {
85
+ point.x () = point.attribute (" local_x" ).asDouble ().value ();
86
+ }
87
+ if (point.hasAttribute (" local_y" )) {
88
+ point.y () = point.attribute (" local_y" ).asDouble ().value ();
89
+ }
90
+ }
91
+ // realign lanelet borders using updated points
92
+ for (lanelet::Lanelet lanelet : map->laneletLayer ) {
93
+ auto left = lanelet.leftBound ();
94
+ auto right = lanelet.rightBound ();
95
+ std::tie (left, right) = lanelet::geometry::align (left, right);
96
+ lanelet.setLeftBound (left);
97
+ lanelet.setRightBound (right);
98
+ }
99
+ // overwrite centerline
100
+ lanelet::utils::overwriteLaneletsCenterline (map, 5.0 , false );
101
+ // convert map to binary message
102
+ {
103
+ const auto map_bin_msg =
104
+ Lanelet2MapLoaderNode::create_map_bin_msg (map, lanelet2_paths[0 ], rclcpp::Clock ().now ());
105
+
106
+ pub_map_bin_debug_->publish (map_bin_msg);
107
+ } // convert map to binary message end
108
+ }
109
+
110
+ {
111
+ std::vector<std::string> lanelet2_paths;
112
+ for (const auto & id : request->osm_file_ids ) {
113
+ auto it = std::find_if (
114
+ lanelet2_file_metadata_dict_.begin (), lanelet2_file_metadata_dict_.end (),
115
+ [&id](const auto & file) { return file.second .id == id; });
116
+ if (it == lanelet2_file_metadata_dict_.end ()) {
117
+ continue ;
118
+ }
119
+ if (!std::filesystem::exists (it->first )) {
120
+ continue ;
121
+ }
122
+ lanelet2_paths.push_back (it->first );
123
+ }
124
+ if (lanelet2_paths.empty ()) {
125
+ return false ;
126
+ }
127
+
128
+ lanelet::ErrorMessages errors{};
129
+ lanelet::projection::MGRSProjector projector{};
130
+ lanelet::io_handlers::MultiOsmParser parser (projector);
131
+ lanelet::LaneletMapPtr map = parser.parse (lanelet2_paths, errors);
132
+
133
+ for (lanelet::Point3d point : map->pointLayer ) {
134
+ if (point.hasAttribute (" local_x" )) {
135
+ point.x () = point.attribute (" local_x" ).asDouble ().value ();
136
+ }
137
+ if (point.hasAttribute (" local_y" )) {
138
+ point.y () = point.attribute (" local_y" ).asDouble ().value ();
139
+ }
140
+ }
141
+ // realign lanelet borders using updated points
142
+ for (lanelet::Lanelet lanelet : map->laneletLayer ) {
143
+ auto left = lanelet.leftBound ();
144
+ auto right = lanelet.rightBound ();
145
+ std::tie (left, right) = lanelet::geometry::align (left, right);
146
+ lanelet.setLeftBound (left);
147
+ lanelet.setRightBound (right);
148
+ }
149
+ // overwrite centerline
150
+ lanelet::utils::overwriteLaneletsCenterline (map, 5.0 , false );
151
+ // convert map to binary message
152
+ {
153
+ const auto map_bin_msg =
154
+ Lanelet2MapLoaderNode::create_map_bin_msg (map, lanelet2_paths[0 ], rclcpp::Clock ().now ());
155
+
156
+ pub_map_bin_->publish (map_bin_msg);
157
+ response->differential_map = map_bin_msg;
158
+ }
159
+ }
160
+
161
+ return true ;
162
+ }
163
+
164
+ autoware_map_msgs::msg::LaneletMapMetaData
165
+ Lanelet2DifferentialLoaderModule::getLaneletMapMetaDataMsg () const
166
+ {
167
+ autoware_map_msgs::msg::LaneletMapMetaData metadata;
168
+ for (const auto & file : lanelet2_file_metadata_dict_) {
169
+ std::cout << " ----------------" << std::endl;
170
+ std::cout << file.first << std::endl;
171
+ std::cout << file.second .id << std::endl;
172
+
173
+ autoware_map_msgs::msg::LaneletMapTileMetaData tile_msg;
174
+ tile_msg.mgrs_code = file.second .mgrs_code ;
175
+ tile_msg.origin_lat = file.second .origin_lat ;
176
+ tile_msg.origin_lon = file.second .origin_lon ;
177
+
178
+ autoware_map_msgs::msg::LaneletMapTileMetaDataWithID tile_msg_with_id;
179
+ tile_msg_with_id.metadata = tile_msg;
180
+ tile_msg_with_id.tile_id = file.second .id ;
181
+
182
+ metadata.metadata_list .push_back (tile_msg_with_id);
183
+ }
184
+ metadata.header .frame_id = " map" ;
185
+ metadata.header .stamp = clock_->now ();
186
+
187
+ return metadata;
188
+ }
0 commit comments