Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
geojson_graph_file_loader.hpp
1 // Copyright (c) 2025 Joshua Wallace
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 <memory>
16 #include <string>
17 #include <vector>
18 #include <nlohmann/json.hpp>
19 
20 #include "nav2_core/route_exceptions.hpp"
21 #include "nav2_route/interfaces/graph_file_loader.hpp"
22 #include "nav2_util/lifecycle_node.hpp"
23 
24 #ifndef NAV2_ROUTE__PLUGINS__GRAPH_FILE_LOADERS__GEOJSON_GRAPH_FILE_LOADER_HPP_
25 #define NAV2_ROUTE__PLUGINS__GRAPH_FILE_LOADERS__GEOJSON_GRAPH_FILE_LOADER_HPP_
26 
27 namespace nav2_route
28 {
29 
35 {
36 public:
37  using Json = nlohmann::json;
38 
43 
48 
53  void configure(
54  const rclcpp_lifecycle::LifecycleNode::SharedPtr node) override;
55 
63  bool loadGraphFromFile(
64  Graph & graph,
65  GraphToIDMap & graph_to_id_map,
66  std::string filepath) override;
67 
68 protected:
74  bool doesFileExist(const std::string & filepath);
75 
82  void getGraphElements(
83  const Json & features, std::vector<Json> & nodes, std::vector<Json> & edges);
84 
91  void addNodesToGraph(Graph & graph, GraphToIDMap & graph_to_id_map, std::vector<Json> & nodes);
92 
99  void addEdgesToGraph(Graph & graph, GraphToIDMap & graph_to_id_map, std::vector<Json> & edges);
100 
106  Coordinates convertCoordinatesFromJson(const Json & node);
107 
114  Metadata convertMetaDataFromJson(const Json & properties, const std::string & key = "metadata");
115 
121  Operation convertOperationFromJson(const Json & json_operation);
122 
128  Operations convertOperationsFromJson(const Json & properties);
129 
135  EdgeCost convertEdgeCostFromJson(const Json & properties);
136 
137  rclcpp::Logger logger_{rclcpp::get_logger("GeoJsonGraphFileLoader")};
138 };
139 
140 NLOHMANN_JSON_SERIALIZE_ENUM(
141  OperationTrigger, {
142  {OperationTrigger::NODE, "NODE"},
143  {OperationTrigger::ON_ENTER, "ON_ENTER"},
144  {OperationTrigger::ON_EXIT, "ON_EXIT"},
145  })
146 
147 } // namespace nav2_route
148 
149 #endif // NAV2_ROUTE__PLUGINS__GRAPH_FILE_LOADERS__GEOJSON_GRAPH_FILE_LOADER_HPP_
A GraphFileLoader plugin to load a geojson graph representation.
Coordinates convertCoordinatesFromJson(const Json &node)
Converts the coordinates from the json object into the Coordinates type.
bool doesFileExist(const std::string &filepath)
Checks if a file even exists on the filesystem.
Operation convertOperationFromJson(const Json &json_operation)
Converts the operation from the json object into the operation type.
Operations convertOperationsFromJson(const Json &properties)
Converts the operations data from the json object into the operations type if present.
~GeoJsonGraphFileLoader()=default
Destructor.
void addNodesToGraph(Graph &graph, GraphToIDMap &graph_to_id_map, std::vector< Json > &nodes)
Add nodes into the graph.
GeoJsonGraphFileLoader()=default
Constructor.
Metadata convertMetaDataFromJson(const Json &properties, const std::string &key="metadata")
Converts the metadata from the json object into the metadata type.
void configure(const rclcpp_lifecycle::LifecycleNode::SharedPtr node) override
Configure, but do not store the node.
void addEdgesToGraph(Graph &graph, GraphToIDMap &graph_to_id_map, std::vector< Json > &edges)
Add edges into the graph.
EdgeCost convertEdgeCostFromJson(const Json &properties)
Converts the edge cost data from the json object into the edge cost type.
bool loadGraphFromFile(Graph &graph, GraphToIDMap &graph_to_id_map, std::string filepath) override
Loads the geojson file into the graph.
void getGraphElements(const Json &features, std::vector< Json > &nodes, std::vector< Json > &edges)
Get the nodes and edges from features.
A plugin interface to parse a file into the graph.
An object to store Node coordinates in different frames.
Definition: types.hpp:173
An object to store edge cost or cost metadata for scoring.
Definition: types.hpp:88
An object to store arbitrary metadata regarding nodes from the graph file.
Definition: types.hpp:35
An object to store operations to perform on events with types and metadata.
Definition: types.hpp:109