Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(map_loader, route_handler)!: add format_version validation #7074

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions map/map_loader/config/lanelet2_map_loader.param.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**:
ros__parameters:
allow_unsupported_version: true # flag to load unsupported format_version anyway. If true, just prints warning.
center_line_resolution: 5.0 # [m]
lanelet2_map_path: $(var lanelet2_map_path) # The lanelet2 map path
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <component_interface_specs/map.hpp>
#include <component_interface_utils/rclcpp.hpp>
#include <lanelet2_extension/version.hpp>
#include <rclcpp/rclcpp.hpp>

#include <autoware_map_msgs/msg/lanelet_map_bin.hpp>
Expand All @@ -32,6 +33,9 @@ using tier4_map_msgs::msg::MapProjectorInfo;

class Lanelet2MapLoaderNode : public rclcpp::Node
{
public:
static constexpr lanelet::autoware::Version version = lanelet::autoware::version;

public:
explicit Lanelet2MapLoaderNode(const rclcpp::NodeOptions & options);

Expand Down
5 changes: 5 additions & 0 deletions map/map_loader/schema/lanelet2_map_loader.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"lanelet2_map_loader": {
"type": "object",
"properties": {
"allow_unsupported_version": {
"type": "boolean",
"description": "Flag to load unsupported format_version anyway. If true, just prints warning.",
"default": "true"
},
"center_line_resolution": {
"type": "number",
"description": "Resolution of the Lanelet center line [m]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <lanelet2_io/Io.h>
#include <lanelet2_projection/UTM.h>

#include <stdexcept>
#include <string>

Lanelet2MapLoaderNode::Lanelet2MapLoaderNode(const rclcpp::NodeOptions & options)
Expand All @@ -61,23 +62,54 @@
sub_map_projector_info_,
[this](const MapProjectorInfo::Message::ConstSharedPtr msg) { on_map_projector_info(msg); });

declare_parameter<bool>("allow_unsupported_version");
declare_parameter<std::string>("lanelet2_map_path");
declare_parameter<double>("center_line_resolution");
}

void Lanelet2MapLoaderNode::on_map_projector_info(
const MapProjectorInfo::Message::ConstSharedPtr msg)
{
const auto allow_unsupported_version = get_parameter("allow_unsupported_version").as_bool();
const auto lanelet2_filename = get_parameter("lanelet2_map_path").as_string();
const auto center_line_resolution = get_parameter("center_line_resolution").as_double();

// load map from file
const auto map = load_map(lanelet2_filename, *msg);
if (!map) {
RCLCPP_ERROR(get_logger(), "Failed to load lanelet2_map. Not published.");
return;
}

std::string format_version{"null"}, map_version{""};
lanelet::io_handlers::AutowareOsmParser::parseVersions(
lanelet2_filename, &format_version, &map_version);
if (format_version == "null" || format_version.empty() || !isdigit(format_version[0])) {

Check warning on line 87 in map/map_loader/src/lanelet2_map_loader/lanelet2_map_loader_node.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Conditional

Lanelet2MapLoaderNode::on_map_projector_info has 1 complex conditionals with 2 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.
RCLCPP_WARN(
get_logger(),
"%s has no format_version(null) or non semver-style format_version(%s) information",
lanelet2_filename.c_str(), format_version.c_str());
if (!allow_unsupported_version) {
throw std::invalid_argument(
"allow_unsupported_version is false, so stop loading lanelet map");
}
} else if (const auto map_major_ver_opt = lanelet::io_handlers::parseMajorVersion(format_version);
map_major_ver_opt.has_value()) {
const auto map_major_ver = map_major_ver_opt.value();
if (map_major_ver > static_cast<uint64_t>(lanelet::autoware::version)) {
RCLCPP_WARN(
get_logger(),
"format_version(%ld) of the provided map(%s) is larger than the supported version(%ld)",
map_major_ver, lanelet2_filename.c_str(),
static_cast<uint64_t>(lanelet::autoware::version));
if (!allow_unsupported_version) {
throw std::invalid_argument(
"allow_unsupported_version is false, so stop loading lanelet map");
}
}
}
RCLCPP_INFO(get_logger(), "Loaded map format_version: %s", format_version.c_str());

Check notice on line 112 in map/map_loader/src/lanelet2_map_loader/lanelet2_map_loader_node.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ New issue: Complex Method

Lanelet2MapLoaderNode::on_map_projector_info has a cyclomatic complexity of 11, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 112 in map/map_loader/src/lanelet2_map_loader/lanelet2_map_loader_node.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Bumpy Road Ahead

Lanelet2MapLoaderNode::on_map_projector_info has 2 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is one single, nested block per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.
// overwrite centerline
lanelet::utils::overwriteLaneletsCenterline(map, center_line_resolution, false);

Expand Down
11 changes: 11 additions & 0 deletions planning/autoware_route_handler/src/route_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 TIER IV, Inc.

Check notice on line 1 in planning/autoware_route_handler/src/route_handler.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1848 to 1858, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in planning/autoware_route_handler/src/route_handler.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 4.66 to 4.68, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
#include "autoware_route_handler/route_handler.hpp"

#include <autoware_utils/math/normalization.hpp>
#include <lanelet2_extension/io/autoware_osm_parser.hpp>
#include <lanelet2_extension/utility/message_conversion.hpp>
#include <lanelet2_extension/utility/query.hpp>
#include <lanelet2_extension/utility/route_checker.hpp>
Expand Down Expand Up @@ -186,6 +187,16 @@
lanelet_map_ptr_ = std::make_shared<lanelet::LaneletMap>();
lanelet::utils::conversion::fromBinMsg(
map_msg, lanelet_map_ptr_, &traffic_rules_ptr_, &routing_graph_ptr_);
const auto map_major_version_opt =
lanelet::io_handlers::parseMajorVersion(map_msg.version_map_format);
if (!map_major_version_opt) {
RCLCPP_WARN(
logger_, "setMap() for invalid version map: %s", map_msg.version_map_format.c_str());
} else if (map_major_version_opt.value() > static_cast<uint64_t>(lanelet::autoware::version)) {
RCLCPP_WARN(
logger_, "setMap() for a map(version %s) newer than lanelet2_extension support version(%d)",
map_msg.version_map_format.c_str(), static_cast<int>(lanelet::autoware::version));
}

const auto traffic_rules = lanelet::traffic_rules::TrafficRulesFactory::create(
lanelet::Locations::Germany, lanelet::Participants::Vehicle);
Expand Down
Loading