|
| 1 | +// Copyright 2020 TIER IV, Inc. |
| 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 <autoware_lanelet2_extension/io/autoware_osm_parser.hpp> |
| 16 | +#include <autoware_lanelet2_extension/projection/mgrs_projector.hpp> |
| 17 | +#include <autoware_lanelet2_extension/utility/message_conversion.hpp> |
| 18 | +#include <rclcpp/rclcpp.hpp> |
| 19 | + |
| 20 | +#include <lanelet2_core/LaneletMap.h> |
| 21 | +#include <lanelet2_core/geometry/Lanelet.h> |
| 22 | +#include <lanelet2_core/primitives/LaneletSequence.h> |
| 23 | +#include <lanelet2_io/Io.h> |
| 24 | +#include <lanelet2_routing/RoutingGraph.h> |
| 25 | + |
| 26 | +#include <iostream> |
| 27 | +#include <unordered_set> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +namespace autoware::lanelet2_map_utils |
| 31 | +{ |
| 32 | +bool load_lanelet_map( |
| 33 | + const std::string & llt_map_path, lanelet::LaneletMapPtr & lanelet_map_ptr, |
| 34 | + lanelet::Projector & projector) |
| 35 | +{ |
| 36 | + lanelet::LaneletMapPtr lanelet_map; |
| 37 | + lanelet::ErrorMessages errors; |
| 38 | + lanelet_map_ptr = lanelet::load(llt_map_path, "autoware_osm_handler", projector, &errors); |
| 39 | + |
| 40 | + for (const auto & error : errors) { |
| 41 | + RCLCPP_ERROR_STREAM(rclcpp::get_logger("loadLaneletMap"), error); |
| 42 | + } |
| 43 | + if (!errors.empty()) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + std::cout << "Loaded Lanelet2 map" << std::endl; |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +lanelet::Lanelets convert_to_vector(const lanelet::LaneletMapPtr & lanelet_map_ptr) |
| 51 | +{ |
| 52 | + lanelet::Lanelets lanelets; |
| 53 | + std::copy( |
| 54 | + lanelet_map_ptr->laneletLayer.begin(), lanelet_map_ptr->laneletLayer.end(), |
| 55 | + std::back_inserter(lanelets)); |
| 56 | + return lanelets; |
| 57 | +} |
| 58 | +void fix_tags(lanelet::LaneletMapPtr & lanelet_map_ptr) |
| 59 | +{ |
| 60 | + auto lanelets = convert_to_vector(lanelet_map_ptr); |
| 61 | + lanelet::traffic_rules::TrafficRulesPtr traffic_rules = |
| 62 | + lanelet::traffic_rules::TrafficRulesFactory::create( |
| 63 | + lanelet::Locations::Germany, lanelet::Participants::Vehicle); |
| 64 | + lanelet::routing::RoutingGraphUPtr routing_graph = |
| 65 | + lanelet::routing::RoutingGraph::build(*lanelet_map_ptr, *traffic_rules); |
| 66 | + |
| 67 | + for (auto & llt : lanelets) { |
| 68 | + if (!routing_graph->conflicting(llt).empty()) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + llt.attributes().erase("turn_direction"); |
| 72 | + if (!!routing_graph->adjacentRight(llt)) { |
| 73 | + llt.rightBound().attributes()["lane_change"] = "yes"; |
| 74 | + } |
| 75 | + if (!!routing_graph->adjacentLeft(llt)) { |
| 76 | + llt.leftBound().attributes()["lane_change"] = "yes"; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +} // namespace autoware::lanelet2_map_utils |
| 81 | + |
| 82 | +int main(int argc, char * argv[]) |
| 83 | +{ |
| 84 | + rclcpp::init(argc, argv); |
| 85 | + |
| 86 | + auto node = rclcpp::Node::make_shared("fix_lane_change_tags"); |
| 87 | + |
| 88 | + const auto llt_map_path = node->declare_parameter<std::string>("llt_map_path"); |
| 89 | + const auto output_path = node->declare_parameter<std::string>("output_path"); |
| 90 | + |
| 91 | + lanelet::LaneletMapPtr llt_map_ptr(new lanelet::LaneletMap); |
| 92 | + lanelet::projection::MGRSProjector projector; |
| 93 | + |
| 94 | + if (!autoware::lanelet2_map_utils::load_lanelet_map(llt_map_path, llt_map_ptr, projector)) { |
| 95 | + return EXIT_FAILURE; |
| 96 | + } |
| 97 | + |
| 98 | + autoware::lanelet2_map_utils::fix_tags(llt_map_ptr); |
| 99 | + lanelet::write(output_path, *llt_map_ptr, projector); |
| 100 | + |
| 101 | + rclcpp::shutdown(); |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
0 commit comments