1 #include "catch.hpp" 2 3 #include "utils.hpp" 4 5 #include <osmium/handler.hpp> 6 #include <osmium/handler/node_locations_for_ways.hpp> 7 #include <osmium/index/map/flex_mem.hpp> 8 #include <osmium/io/xml_input.hpp> 9 #include <osmium/memory/buffer.hpp> 10 #include <osmium/visitor.hpp> 11 12 TEST_CASE("apply with lambdas on reader") { 13 osmium::io::File file{with_data_dir("t/relations/data.osm")}; 14 osmium::io::Reader reader{file}; 15 16 int count_n = 0; 17 int count_w = 0; 18 int count_r = 0; 19 int count_o = 0; 20 int count_a = 0; 21 22 osmium::apply(reader, __anon3b5b442c0102(const osmium::Node& node) 23 [&](const osmium::Node& node) { 24 count_n += node.version(); 25 }, __anon3b5b442c0202(const osmium::Way& way) 26 [&](const osmium::Way& way) { 27 if (way.id() == 20) { 28 ++count_w; 29 } 30 }, __anon3b5b442c0302(const osmium::Relation& relation) 31 [&](const osmium::Relation& relation) { 32 if (relation.id() > 30) { 33 ++count_r; 34 } 35 }, __anon3b5b442c0402(const osmium::OSMObject& object) 36 [&](const osmium::OSMObject& object) { 37 if (object.id() % 10 == 0) { 38 ++count_o; 39 } 40 }, __anon3b5b442c0502(const osmium::Way& way) 41 [&](const osmium::Way& way) { 42 if (way.id() == 21) { 43 ++count_w; 44 } 45 }, __anon3b5b442c0602(const osmium::Area& ) 46 [&](const osmium::Area& /*area*/) { 47 ++count_a; 48 } 49 ); 50 51 REQUIRE(count_n == 5); 52 REQUIRE(count_w == 2); 53 REQUIRE(count_r == 2); 54 REQUIRE(count_o == 3); 55 REQUIRE(count_a == 0); 56 } 57 58 TEST_CASE("apply with lambda on buffer") { 59 osmium::io::File file{with_data_dir("t/relations/data.osm")}; 60 osmium::io::Reader reader{file}; 61 62 const auto buffer = reader.read(); 63 reader.close(); 64 65 std::size_t members = 0; 66 __anon3b5b442c0702(const osmium::Relation& relation) 67 osmium::apply(buffer, [&](const osmium::Relation& relation) { 68 members += relation.members().size(); 69 }); 70 71 REQUIRE(members == 5); 72 } 73 74 TEST_CASE("apply on non-const buffer can change data") { 75 osmium::io::File file{with_data_dir("t/relations/data.osm")}; 76 osmium::io::Reader reader{file}; 77 78 auto buffer = reader.read(); 79 reader.close(); 80 81 int nodes = 0; 82 83 osmium::apply(buffer, __anon3b5b442c0802(osmium::Node& node) 84 [&](osmium::Node& node) { 85 node.set_version(123); 86 }, __anon3b5b442c0902(const osmium::Node& node) 87 [&](const osmium::Node& node) { 88 ++nodes; 89 REQUIRE(node.version() == 123); 90 } 91 ); 92 93 REQUIRE(nodes == 5); 94 } 95 96 TEST_CASE("apply with handler and lambda") { 97 using index_type = osmium::index::map::FlexMem<osmium::unsigned_object_id_type, osmium::Location>; 98 using location_handler_type = osmium::handler::NodeLocationsForWays<index_type>; 99 100 osmium::io::File file{with_data_dir("t/relations/data.osm")}; 101 osmium::io::Reader reader{file}; 102 103 auto buffer = reader.read(); 104 reader.close(); 105 106 index_type index; 107 location_handler_type location_handler{index}; 108 109 int64_t x = 0; 110 int64_t y = 0; 111 112 osmium::apply(buffer, __anon3b5b442c0a02(const osmium::Way& way) 113 [&](const osmium::Way& way) { 114 REQUIRE_FALSE(way.nodes().front().location().valid()); 115 }, 116 location_handler, __anon3b5b442c0b02(const osmium::Way& way) 117 [&](const osmium::Way& way) { 118 REQUIRE(way.nodes().front().location().valid()); 119 for (const auto& wn : way.nodes()) { 120 x += wn.location().x(); 121 y += wn.location().y(); 122 } 123 } 124 ); 125 126 REQUIRE(x == 44000000); 127 REQUIRE(y == 40000000); 128 } 129 130