Nav2 Navigation Stack - kilted  kilted
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 
23 #ifndef NAV2_ROUTE__PLUGINS__GRAPH_FILE_LOADERS__GEOJSON_GRAPH_FILE_LOADER_HPP_
24 #define NAV2_ROUTE__PLUGINS__GRAPH_FILE_LOADERS__GEOJSON_GRAPH_FILE_LOADER_HPP_
25 
26 namespace nav2_route
27 {
28 
34 {
35 public:
36  using Json = nlohmann::json;
37 
42 
47 
52  void configure(
53  const rclcpp_lifecycle::LifecycleNode::SharedPtr node) override;
54 
62  bool loadGraphFromFile(
63  Graph & graph,
64  GraphToIDMap & graph_to_id_map,
65  std::string filepath) override;
66 
67 protected:
73  bool doesFileExist(const std::string & filepath);
74 
81  void getGraphElements(
82  const Json & features, std::vector<Json> & nodes, std::vector<Json> & edges);
83 
90  void addNodesToGraph(Graph & graph, GraphToIDMap & graph_to_id_map, std::vector<Json> & nodes);
91 
98  void addEdgesToGraph(Graph & graph, GraphToIDMap & graph_to_id_map, std::vector<Json> & edges);
99 
105  Coordinates convertCoordinatesFromJson(const Json & node);
106 
113  Metadata convertMetaDataFromJson(const Json & properties, const std::string & key = "metadata");
114 
120  Operation convertOperationFromJson(const Json & json_operation);
121 
127  Operations convertOperationsFromJson(const Json & properties);
128 
134  EdgeCost convertEdgeCostFromJson(const Json & properties);
135 
136  rclcpp::Logger logger_{rclcpp::get_logger("GeoJsonGraphFileLoader")};
137 };
138 
139 NLOHMANN_JSON_SERIALIZE_ENUM(
140  OperationTrigger, {
141  {OperationTrigger::NODE, "NODE"},
142  {OperationTrigger::ON_ENTER, "ON_ENTER"},
143  {OperationTrigger::ON_EXIT, "ON_EXIT"},
144  })
145 
146 } // namespace nav2_route
147 
148 #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