Skip to content

Commit 91a5381

Browse files
authored
Merge pull request #1708 from joto/geom-test-polygons
Add some tests for polygon geometry creation from OSM data
2 parents 948cedb + 5fa0323 commit 91a5381

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tests/test-geom-polygons.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <catch.hpp>
1111

12+
#include "common-buffer.hpp"
13+
14+
#include "geom-from-osm.hpp"
1215
#include "geom-functions.hpp"
1316
#include "geom.hpp"
1417

@@ -63,3 +66,77 @@ TEST_CASE("geom::polygon_t", "[NoDB]")
6366
REQUIRE(geometry_type(geom) == "POLYGON");
6467
REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}});
6568
}
69+
70+
TEST_CASE("create_polygon from OSM data", "[NoDB]")
71+
{
72+
test_buffer_t buffer;
73+
buffer.add_way("w20 Nn1x1y1,n2x2y1,n3x2y2,n4x1y2,n1x1y1");
74+
75+
auto const geom = geom::create_polygon(buffer.buffer().get<osmium::Way>(0));
76+
77+
REQUIRE(geom.is_polygon());
78+
REQUIRE(geometry_type(geom) == "POLYGON");
79+
REQUIRE(num_geometries(geom) == 1);
80+
REQUIRE(area(geom) == Approx(1.0));
81+
REQUIRE(
82+
geom.get<geom::polygon_t>() ==
83+
geom::polygon_t{geom::ring_t{{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1}}});
84+
REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}});
85+
}
86+
87+
TEST_CASE("create_polygon from OSM data (reverse)", "[NoDB]")
88+
{
89+
test_buffer_t buffer;
90+
buffer.add_way("w20 Nn1x1y1,n2x1y2,n3x2y2,n4x2y1,n1x1y1");
91+
92+
auto const geom = geom::create_polygon(buffer.buffer().get<osmium::Way>(0));
93+
94+
REQUIRE(geom.is_polygon());
95+
REQUIRE(geometry_type(geom) == "POLYGON");
96+
REQUIRE(num_geometries(geom) == 1);
97+
REQUIRE(area(geom) == Approx(1.0));
98+
REQUIRE(
99+
geom.get<geom::polygon_t>() ==
100+
geom::polygon_t{geom::ring_t{{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1}}});
101+
REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}});
102+
}
103+
104+
TEST_CASE("create_polygon from OSM data without locations", "[NoDB]")
105+
{
106+
test_buffer_t buffer;
107+
buffer.add_way("w20 Nn1,n2,n3,n1");
108+
109+
auto const geom = geom::create_polygon(buffer.buffer().get<osmium::Way>(0));
110+
111+
REQUIRE(geom.is_null());
112+
}
113+
114+
TEST_CASE("create_polygon from invalid OSM data (single node)", "[NoDB]")
115+
{
116+
test_buffer_t buffer;
117+
buffer.add_way("w20 Nn1x1y1");
118+
119+
auto const geom = geom::create_polygon(buffer.buffer().get<osmium::Way>(0));
120+
121+
REQUIRE(geom.is_null());
122+
}
123+
124+
TEST_CASE("create_polygon from invalid OSM data (way node closed)", "[NoDB]")
125+
{
126+
test_buffer_t buffer;
127+
buffer.add_way("w20 Nn1x1y1,n2x2y2");
128+
129+
auto const geom = geom::create_polygon(buffer.buffer().get<osmium::Way>(0));
130+
131+
REQUIRE(geom.is_null());
132+
}
133+
134+
TEST_CASE("create_polygon from invalid OSM data (self-intersection)", "[NoDB]")
135+
{
136+
test_buffer_t buffer;
137+
buffer.add_way("w20 Nn1x1y1,n2x1y2,n3x2y1,n4x2y2,n1x1y1");
138+
139+
auto const geom = geom::create_polygon(buffer.buffer().get<osmium::Way>(0));
140+
141+
REQUIRE(geom.is_null());
142+
}

0 commit comments

Comments
 (0)