Skip to content

Commit 6d8a3e3

Browse files
authored
Merge pull request #1713 from joto/geom-more-tests
Add tests for multipoint and multipolygon geometries
2 parents f91eb31 + 6d12454 commit 6d8a3e3

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ set_test(test-geom-box LABELS NoDB)
4646
set_test(test-geom-collections LABELS NoDB)
4747
set_test(test-geom-linestrings LABELS NoDB)
4848
set_test(test-geom-multilinestrings LABELS NoDB)
49+
set_test(test-geom-multipoints LABELS NoDB)
50+
set_test(test-geom-multipolygons LABELS NoDB)
4951
set_test(test-geom-null LABELS NoDB)
5052
set_test(test-geom-points LABELS NoDB)
5153
set_test(test-geom-polygons LABELS NoDB)

tests/test-geom-multipoints.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2022 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include <catch.hpp>
11+
12+
#include "common-buffer.hpp"
13+
14+
#include "geom-from-osm.hpp"
15+
#include "geom-functions.hpp"
16+
#include "geom.hpp"
17+
18+
#include <array>
19+
20+
TEST_CASE("multipoint_t with a single point", "[NoDB]")
21+
{
22+
geom::point_t const point{1, 1};
23+
24+
geom::geometry_t geom{geom::multipoint_t{}};
25+
auto &mp = geom.get<geom::multipoint_t>();
26+
mp.add_geometry({1, 1});
27+
28+
REQUIRE(geom.is_multipoint());
29+
REQUIRE(geometry_type(geom) == "MULTIPOINT");
30+
REQUIRE(num_geometries(geom) == 1);
31+
REQUIRE(area(geom) == Approx(0.0));
32+
REQUIRE(centroid(geom) == geom::geometry_t{point});
33+
34+
REQUIRE(mp[0] == point);
35+
}
36+
37+
TEST_CASE("multipoint_t with several points", "[NoDB]")
38+
{
39+
geom::point_t const p0{1, 1};
40+
geom::point_t const p1{2, 1};
41+
geom::point_t const p2{3, 1};
42+
43+
geom::geometry_t geom{geom::multipoint_t{}};
44+
auto &mp = geom.get<geom::multipoint_t>();
45+
mp.add_geometry({1, 1});
46+
mp.add_geometry({2, 1});
47+
mp.add_geometry({3, 1});
48+
49+
REQUIRE(geom.is_multipoint());
50+
REQUIRE(geometry_type(geom) == "MULTIPOINT");
51+
REQUIRE(num_geometries(geom) == 3);
52+
REQUIRE(area(geom) == Approx(0.0));
53+
REQUIRE(centroid(geom) == geom::geometry_t{p1});
54+
55+
REQUIRE(mp[0] == p0);
56+
REQUIRE(mp[1] == p1);
57+
REQUIRE(mp[2] == p2);
58+
}

tests/test-geom-multipolygons.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2022 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include <catch.hpp>
11+
12+
#include "common-buffer.hpp"
13+
14+
#include "geom-from-osm.hpp"
15+
#include "geom-functions.hpp"
16+
#include "geom.hpp"
17+
18+
TEST_CASE("multipolygon geometry with single outer, no inner", "[NoDB]")
19+
{
20+
geom::geometry_t geom{geom::multipolygon_t{}};
21+
auto &mp = geom.get<geom::multipolygon_t>();
22+
23+
mp.add_geometry(
24+
geom::polygon_t{geom::ring_t{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}});
25+
26+
REQUIRE(geometry_type(geom) == "MULTIPOLYGON");
27+
REQUIRE(num_geometries(geom) == 1);
28+
REQUIRE(area(geom) == Approx(1.0));
29+
REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{0.5, 0.5}});
30+
}
31+
32+
TEST_CASE("multipolygon geometry with two polygons", "[NoDB]")
33+
{
34+
geom::geometry_t geom{geom::multipolygon_t{}};
35+
auto &mp = geom.get<geom::multipolygon_t>();
36+
37+
mp.add_geometry(
38+
geom::polygon_t{geom::ring_t{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}});
39+
40+
geom::polygon_t polygon{
41+
geom::ring_t{{2, 2}, {2, 5}, {5, 5}, {5, 2}, {2, 2}}};
42+
polygon.add_inner_ring(
43+
geom::ring_t{{3, 3}, {4, 3}, {4, 4}, {3, 4}, {3, 3}});
44+
REQUIRE(polygon.num_geometries() == 1);
45+
REQUIRE(polygon.inners().size() == 1);
46+
47+
mp.add_geometry(std::move(polygon));
48+
49+
REQUIRE(geometry_type(geom) == "MULTIPOLYGON");
50+
REQUIRE(num_geometries(geom) == 2);
51+
REQUIRE(area(geom) == Approx(9.0));
52+
}
53+
54+
TEST_CASE("create_multipolygon creates simple polygon from OSM data", "[NoDB]")
55+
{
56+
test_buffer_t buffer;
57+
buffer.add_way("w20 Nn1x1y1,n2x2y1,n3x2y2,n4x1y2");
58+
buffer.add_way("w21 Nn4x1y2,n1x1y1");
59+
auto const &relation = buffer.add_relation("r30 Mw20@,w21@");
60+
61+
auto const geom = geom::create_multipolygon(relation, buffer.buffer());
62+
63+
REQUIRE(geom.is_polygon());
64+
REQUIRE(geometry_type(geom) == "POLYGON");
65+
REQUIRE(num_geometries(geom) == 1);
66+
REQUIRE(area(geom) == Approx(1.0));
67+
REQUIRE(
68+
geom.get<geom::polygon_t>() ==
69+
geom::polygon_t{geom::ring_t{{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1}}});
70+
REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}});
71+
}
72+
73+
TEST_CASE("create_multipolygon from OSM data", "[NoDB]")
74+
{
75+
test_buffer_t buffer;
76+
buffer.add_way("w20 Nn1x1y1,n2x2y1,n3x2y2,n4x1y2");
77+
buffer.add_way("w21 Nn4x1y2,n1x1y1");
78+
buffer.add_way("w22 Nn5x10y10,n6x10y20,n7x20y20,n5x10y10");
79+
auto const &relation = buffer.add_relation("r30 Mw20@,w21@,w22@");
80+
81+
auto const geom = geom::create_multipolygon(relation, buffer.buffer());
82+
83+
REQUIRE(geom.is_multipolygon());
84+
REQUIRE(geometry_type(geom) == "MULTIPOLYGON");
85+
REQUIRE(num_geometries(geom) == 2);
86+
REQUIRE(area(geom) == Approx(51.0));
87+
}
88+
89+
TEST_CASE("create_multipolygon from OSM data without locations", "[NoDB]")
90+
{
91+
test_buffer_t buffer;
92+
buffer.add_way("w20 Nn1,n2,n3,n1");
93+
94+
auto const &relation = buffer.add_relation("r30 Mw20@");
95+
auto const geom = geom::create_multipolygon(relation, buffer.buffer());
96+
97+
REQUIRE(geom.is_null());
98+
}
99+
100+
TEST_CASE("create_multipolygon from invalid OSM data (single node)", "[NoDB]")
101+
{
102+
test_buffer_t buffer;
103+
buffer.add_way("w20 Nn1x1y1");
104+
105+
auto const &relation = buffer.add_relation("r30 Mw20@");
106+
auto const geom = geom::create_multipolygon(relation, buffer.buffer());
107+
108+
REQUIRE(geom.is_null());
109+
}
110+
111+
TEST_CASE("create_multipolygon from invalid OSM data (way node closed)", "[NoDB]")
112+
{
113+
test_buffer_t buffer;
114+
buffer.add_way("w20 Nn1x1y1,n2x2y2");
115+
116+
auto const &relation = buffer.add_relation("r30 Mw20@");
117+
auto const geom = geom::create_multipolygon(relation, buffer.buffer());
118+
119+
REQUIRE(geom.is_null());
120+
}
121+
122+
TEST_CASE("create_multipolygon from invalid OSM data (self-intersection)", "[NoDB]")
123+
{
124+
test_buffer_t buffer;
125+
buffer.add_way("w20 Nn1x1y1,n2x1y2,n3x2y1,n4x2y2");
126+
buffer.add_way("w21 Nn4x2y2,n1x1y1");
127+
128+
auto const &relation = buffer.add_relation("r30 Mw20@,w21@");
129+
auto const geom = geom::create_multipolygon(relation, buffer.buffer());
130+
131+
REQUIRE(geom.is_null());
132+
}

0 commit comments

Comments
 (0)