Skip to content

Commit

Permalink
Example of how to use libosmium
Browse files Browse the repository at this point in the history
  • Loading branch information
ewfuentes committed Dec 3, 2024
1 parent b4b5e0c commit f39f19c
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions common/openstreetmap/openstreetmap_test.cc
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@

#include <fmt/core.h>
#include "gtest/gtest.h"

#include "fmt/format.h"
#include <filesystem>
#include <limits>

#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 <filesystem>
#include <limits>

namespace robot::openstreetmap {

TEST(OpenstreetmapTest, can_open_pbf_file) {
// Setup
struct TestHandler : public osmium::handler::Handler {
int node_counter = 0;
std::pair<double, double> min{std::numeric_limits<double>::max(),std::numeric_limits<double>::max()};
std::pair<double, double> max{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::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

0 comments on commit f39f19c

Please sign in to comment.