From f39f19cf4fc1e425ac6d20088f54f5600fb9a7c5 Mon Sep 17 00:00:00 2001 From: Erick Fuentes Date: Tue, 3 Dec 2024 12:59:46 -0500 Subject: [PATCH] Example of how to use libosmium --- common/openstreetmap/openstreetmap_test.cc | 61 ++++++++-------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/common/openstreetmap/openstreetmap_test.cc b/common/openstreetmap/openstreetmap_test.cc index c5c0a878..ab4127ca 100644 --- a/common/openstreetmap/openstreetmap_test.cc +++ b/common/openstreetmap/openstreetmap_test.cc @@ -1,59 +1,44 @@ #include -#include "gtest/gtest.h" -#include "fmt/format.h" +#include +#include -#include "osmium/io/pbf_input.hpp" +#include "fmt/format.h" +#include "gtest/gtest.h" #include "osmium/handler.hpp" +#include "osmium/handler/dump.hpp" +#include "osmium/io/pbf_input.hpp" #include "osmium/visitor.hpp" -#include -#include - namespace robot::openstreetmap { - TEST(OpenstreetmapTest, can_open_pbf_file) { // Setup struct TestHandler : public osmium::handler::Handler { int node_counter = 0; - std::pair min{std::numeric_limits::max(),std::numeric_limits::max()}; - std::pair max{std::numeric_limits::lowest(), std::numeric_limits::lowest()}; - void node(const osmium::Node &node) { - const auto location = node.location(); - - - if (location.valid()) { - min = std::make_pair(std::min(min.first, location.lat()), std::min(min.second, location.lon())); - max = std::make_pair(std::max(max.first, location.lat()), std::max(max.second, location.lon())); - } - if (!node.visible()) { - return; - } - - fmt::print("{} id: {} location: ({}, {}) tags: {{", node_counter++, node.id(), location.lat(), location.lon()); - for (const auto &tag: node.tags()) { - fmt::print("{}: {}, ", tag.key(), tag.value()); - } - std::cout << "}" << std::endl; - } - - void way(const osmium::Way &way) { - - } + int way_counter = 0; + int relation_counter = 0; + + void node(const osmium::Node &node) { node_counter++; } + + void way(const osmium::Way &way) { way_counter++; } + + void relation(const osmium::Relation &relation) { relation_counter++; } }; - const std::filesystem::path osm_pbf_path("external/openstreetmap_snippet/us-virgin-islands-latest.osm.pbf"); - osmium::io::Reader reader(osm_pbf_path, osmium::osm_entity_bits::node | osmium::osm_entity_bits::way); + const std::filesystem::path osm_pbf_path( + "external/openstreetmap_snippet/us-virgin-islands-latest.osm.pbf"); + osmium::io::Reader reader(osm_pbf_path, osmium::osm_entity_bits::node | + osmium::osm_entity_bits::way | + osmium::osm_entity_bits::relation); // Action auto handler = TestHandler(); osmium::apply(reader, handler); - fmt::print("min: ({}, {}) max: ({}, {})\r\n", handler.min.first, handler.min.second, handler.max.first, handler.max.second); - // Verification - EXPECT_TRUE(std::filesystem::exists(osm_pbf_path)); -} - + EXPECT_GT(handler.node_counter, 0); + EXPECT_GT(handler.way_counter, 0); + EXPECT_GT(handler.relation_counter, 0); } +} // namespace robot::openstreetmap