TestOsmFile.cpp
Go to the documentation of this file.
1 #include <sstream>
2 
3 #include "gtest/gtest.h"
5 
6 using namespace lanelet::osm;
7 
8 TEST(OsmFile, readWrite) { // NOLINT
9  File file;
10  // clang-format off
11  file.nodes.emplace(std::make_pair(-1, Node{-1, {{"key", "value"}, {"key2", "value2"}}, {12.3456789, 0001.2345678000, 9.0123}}));
12  file.nodes.emplace(std::make_pair(-2, Node{-2, {}, {45.6789, 12.3, 4.5}}));
13  file.nodes.emplace(std::make_pair(-3, Node{-3, {}, {67.8, 9.10, 11.0}}));
14  file.ways.emplace(std::make_pair(-4, Way{-4, {{"wayKey", "wayValue"}}, {&file.nodes.at(-1), &file.nodes.at(-2)}}));
15  file.ways.emplace(std::make_pair(-5, Way{-5, {{"wayKey", "wayValue"}}, {&file.nodes.at(-2), &file.nodes.at(-3)}}));
16  file.relations.emplace(std::make_pair(-6,Relation{-6,{{"relKey", "relValue"}},{{"outer", &file.ways.at(-4)}, {"outer", &file.ways.at(-5)}, {"node", &file.nodes.at(-2)}}}));
17  // clang-format on
18  auto doc = write(file);
19  auto file2 = read(*doc);
20 
21  EXPECT_EQ(file2.relations, file.relations);
22  EXPECT_EQ(file2.ways, file.ways);
23  EXPECT_EQ(file2, file);
24  // Test upload (false by default)
25  const auto osmNode = doc->first_child();
26  const auto upload_attribute = osmNode.attributes().begin()->next_attribute();
27  EXPECT_EQ(std::string(upload_attribute.name()), "upload");
28  EXPECT_EQ(std::string(upload_attribute.value()), "false");
29  // Test josm_format_elevation (false by default)
30  EXPECT_DOUBLE_EQ(file2.nodes.at(-1).point.lat, 12.3456789);
31  EXPECT_DOUBLE_EQ(file2.nodes.at(-1).point.lon, 1.2345678);
32  EXPECT_DOUBLE_EQ(file2.nodes.at(-1).point.ele, 9.0123);
33  EXPECT_DOUBLE_EQ(file2.nodes.at(-2).point.lat, 45.6789);
34  EXPECT_DOUBLE_EQ(file2.nodes.at(-2).point.lon, 12.3);
35  EXPECT_DOUBLE_EQ(file2.nodes.at(-2).point.ele, 4.5);
36  EXPECT_DOUBLE_EQ(file2.nodes.at(-3).point.lat, 67.8);
37  EXPECT_DOUBLE_EQ(file2.nodes.at(-3).point.lon, 9.10);
38  EXPECT_DOUBLE_EQ(file2.nodes.at(-3).point.ele, 11.0);
39 }
40 
41 TEST(OsmFile, readWriteJSON) { // NOLINT
42  File file;
43  // clang-format off
44  file.nodes.emplace(std::make_pair(-1, Node{-1, {{"key", "value"}, {"key2", "value2"}}, {12.3456789, 0001.2345678000, 9.0123}}));
45  file.nodes.emplace(std::make_pair(-2, Node{-2, {}, {45.6789, 12.3, 4.5}}));
46  file.nodes.emplace(std::make_pair(-3, Node{-3, {}, {67.8, 9.10, 11.0}}));
47  file.ways.emplace(std::make_pair(-4, Way{-4, {{"wayKey", "wayValue"}}, {&file.nodes.at(-1), &file.nodes.at(-2)}}));
48  file.ways.emplace(std::make_pair(-5, Way{-5, {{"wayKey", "wayValue"}}, {&file.nodes.at(-2), &file.nodes.at(-3)}}));
49  file.relations.emplace(std::make_pair(-6,Relation{-6,{{"relKey", "relValue"}},{{"outer", &file.ways.at(-4)}, {"outer", &file.ways.at(-5)}, {"node", &file.nodes.at(-2)}}}));
50  // clang-format on
51  auto doc = write(file, { {"josm_upload", "true"}, {"josm_format_elevation", "true"} });
52  auto file2 = read(*doc);
53 
54  EXPECT_EQ(file2.relations, file.relations);
55  EXPECT_EQ(file2.ways, file.ways);
56  EXPECT_EQ(file2, file);
57  // Test upload
58  const auto osmNode = doc->first_child();
59  const auto upload_attribute = osmNode.attributes().begin()->next_attribute();
60  EXPECT_EQ(std::string(upload_attribute.name()), "upload");
61  EXPECT_EQ(std::string(upload_attribute.value()), "true");
62  // Test josm_format_elevation
63  EXPECT_DOUBLE_EQ(file2.nodes.at(-1).point.lat, 12.3456789);
64  EXPECT_DOUBLE_EQ(file2.nodes.at(-1).point.lon, 1.2345678);
65  EXPECT_DOUBLE_EQ(file2.nodes.at(-1).point.ele, 9.01); // limited to 2 decimals
66  EXPECT_DOUBLE_EQ(file2.nodes.at(-2).point.lat, 45.6789);
67  EXPECT_DOUBLE_EQ(file2.nodes.at(-2).point.lon, 12.3);
68  EXPECT_DOUBLE_EQ(file2.nodes.at(-2).point.ele, 4.5);
69  EXPECT_DOUBLE_EQ(file2.nodes.at(-3).point.lat, 67.8);
70  EXPECT_DOUBLE_EQ(file2.nodes.at(-3).point.lon, 9.10);
71  EXPECT_DOUBLE_EQ(file2.nodes.at(-3).point.ele, 11.0);
72 }
73 
74 TEST(OsmFile, readEmptyFile) { // NOLINT
75  pugi::xml_document doc;
76  doc.load_string("");
77  Errors errors;
78  auto file = read(doc, &errors);
79  EXPECT_TRUE(errors.empty());
80 }
81 
82 TEST(OsmFile, readFileWithoutMapData) { // NOLINT
83  pugi::xml_document doc;
84  doc.load_string("<osm></osm>");
85  Errors errors;
86  auto file = read(doc, &errors);
87  EXPECT_TRUE(errors.empty());
88 }
89 
90 TEST(OsmFile, readFileWithIncompleteMapData) { // NOLINT
91  pugi::xml_document doc;
92  doc.load_string(R"(<osm><way id="1"><nd ref="2"/></way><node/></osm>)");
93  Errors errors;
94  auto file = read(doc, &errors);
95  EXPECT_FALSE(errors.empty());
96 }
97 
98 TEST(OsmFile, readMapWithIncompleteRoles) { // NOLINT
99  pugi::xml_document doc;
100  doc.load_string(R"(<osm>
101  <node id="1" lat="1" lon="1"/>
102  <way id="1">
103  <nd ref="1"/>
104  </way>
105  <relation id="1">
106  <tag k="type" v="lanelet"/>
107  <member type="way" ref="2" role="left"/>
108  <member type="way" ref="1" role="right"/>
109  </relation>
110  <relation id="2">
111  <tag k="type" v="regulatory_element"/>
112  <member type="way" ref="1" role="somerole"/>
113  <member type="way" ref="2" role="nonexisting"/>
114  <member type="relation" ref="3" role="nonexisting"/>
115  <member type="relation" ref="4" role="nonexisting"/>
116  <member type="relation" ref="1" role="somerole2"/>
117  <member type="way" ref="1" role="somerole3"/>
118  </relation>
119  </osm>)");
120  Errors errors;
121  auto file = read(doc, &errors);
122  EXPECT_FALSE(errors.empty());
123  EXPECT_EQ(file.nodes.size(), 1UL);
124  EXPECT_EQ(file.ways.size(), 1UL);
125  ASSERT_EQ(file.relations.size(), 2UL);
126  ASSERT_NE(file.relations.find(2), file.relations.end());
127  EXPECT_EQ(file.relations[1].members.size(), 1UL);
128  EXPECT_EQ(file.relations[2].members.size(), 3UL);
129  auto& members = file.relations[2].members;
130  EXPECT_EQ(members[0].first, "somerole");
131  EXPECT_EQ(members[0].second->id, 1L);
132  EXPECT_EQ(members[1].first, "somerole2");
133  EXPECT_EQ(members[2].first, "somerole3");
134 }
file
osm::File & file
Definition: OsmHandlerWrite.cpp:245
lanelet::osm::write
std::unique_ptr< pugi::xml_document > write(const File &file, const io::Configuration &params=io::Configuration())
Definition: OsmFile.cpp:345
lanelet::osm::Relation
Osm relation object.
Definition: OsmFile.h:55
lanelet::osm::read
File read(pugi::xml_document &node, lanelet::osm::Errors *errors=nullptr)
Definition: OsmFile.cpp:343
lanelet::osm::File
Intermediate representation of an osm file.
Definition: OsmFile.h:72
lanelet::osm::Node
Osm node object.
Definition: OsmFile.h:38
TEST
TEST(OsmFile, readWrite)
Definition: TestOsmFile.cpp:8
lanelet::osm::Errors
std::vector< std::string > Errors
Definition: OsmFile.h:20
lanelet::osm
Definition: OsmFile.h:14
lanelet::osm::Way
Osm way object.
Definition: OsmFile.h:46
OsmFile.h


lanelet2_io
Author(s): Fabian Poggenhans
autogenerated on Thu Mar 6 2025 03:26:03