Skip to content

Commit 4180885

Browse files
committed
Add stream output functions for geometry types for debugging
Output is to something very similar to WKT (results might be a bit different for corner cases like empty linestrings etc.) This is only to be used for debugging. When tests fail the Catch framework will now output the geometries properly.
1 parent b29e6ab commit 4180885

12 files changed

+261
-0
lines changed

src/geom-output.hpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#ifndef OSM2PGSQL_GEOM_OUTPUT_HPP
2+
#define OSM2PGSQL_GEOM_OUTPUT_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2022 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
#include "geom-functions.hpp"
14+
#include "geom.hpp"
15+
16+
/**
17+
* \file
18+
*
19+
* Functions to output geometries to an output stream in something similar to
20+
* the WKT format.
21+
*
22+
* This is used for debugging: The Catch framework can use these to output
23+
* geometries when a test fails.
24+
*/
25+
26+
namespace geom {
27+
28+
template <typename CHAR, typename TRAITS>
29+
std::basic_ostream<CHAR, TRAITS> &
30+
operator<<(std::basic_ostream<CHAR, TRAITS> &out, const nullgeom_t & /*input*/)
31+
{
32+
return out << "NULL";
33+
}
34+
35+
template <typename CHAR, typename TRAITS>
36+
std::basic_ostream<CHAR, TRAITS> &
37+
operator<<(std::basic_ostream<CHAR, TRAITS> &out, const point_t &input)
38+
{
39+
return out << input.x() << ' ' << input.y();
40+
}
41+
42+
template <typename CHAR, typename TRAITS>
43+
std::basic_ostream<CHAR, TRAITS> &
44+
operator<<(std::basic_ostream<CHAR, TRAITS> &out, const point_list_t &input)
45+
{
46+
if (input.empty()) {
47+
return out << "EMPTY";
48+
}
49+
auto it = input.cbegin();
50+
out << *it;
51+
for (++it; it != input.cend(); ++it) {
52+
out << ',' << *it;
53+
}
54+
return out;
55+
}
56+
57+
template <typename CHAR, typename TRAITS>
58+
std::basic_ostream<CHAR, TRAITS> &
59+
operator<<(std::basic_ostream<CHAR, TRAITS> &out, const polygon_t &input)
60+
{
61+
out << '(' << input.outer() << ')';
62+
for (auto const &ring : input.inners()) {
63+
out << ",(" << ring << ')';
64+
}
65+
return out;
66+
}
67+
68+
template <typename CHAR, typename TRAITS>
69+
std::basic_ostream<CHAR, TRAITS> &
70+
operator<<(std::basic_ostream<CHAR, TRAITS> &out, const collection_t &input)
71+
{
72+
if (input.num_geometries() == 0) {
73+
return out << "EMPTY";
74+
}
75+
auto it = input.cbegin();
76+
out << *it;
77+
for (++it; it != input.cend(); ++it) {
78+
out << ',' << *it;
79+
}
80+
return out;
81+
}
82+
83+
template <typename CHAR, typename TRAITS, typename GEOM>
84+
std::basic_ostream<CHAR, TRAITS> &
85+
operator<<(std::basic_ostream<CHAR, TRAITS> &out,
86+
const multigeometry_t<GEOM> &input)
87+
{
88+
if (input.num_geometries() == 0) {
89+
return out << "EMPTY";
90+
}
91+
auto it = input.cbegin();
92+
out << '(' << *it << ')';
93+
for (++it; it != input.cend(); ++it) {
94+
out << ",(" << *it << ')';
95+
}
96+
return out;
97+
}
98+
99+
template <typename CHAR, typename TRAITS>
100+
std::basic_ostream<CHAR, TRAITS> &
101+
operator<<(std::basic_ostream<CHAR, TRAITS> &out, const geometry_t &geom)
102+
{
103+
out << geometry_type(geom) << '(';
104+
geom.visit(overloaded{[&](auto const &input) { out << input; }});
105+
return out << ')';
106+
}
107+
108+
} // namespace geom
109+
110+
#endif // OSM2PGSQL_GEOM_OUTPUT_HPP

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ set_test(test-geom-multilinestrings LABELS NoDB)
4949
set_test(test-geom-multipoints LABELS NoDB)
5050
set_test(test-geom-multipolygons LABELS NoDB)
5151
set_test(test-geom-null LABELS NoDB)
52+
set_test(test-geom-output LABELS NoDB)
5253
set_test(test-geom-points LABELS NoDB)
5354
set_test(test-geom-polygons LABELS NoDB)
5455
set_test(test-geom-transform LABELS NoDB)

tests/test-geom-collections.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "geom-from-osm.hpp"
1515
#include "geom-functions.hpp"
16+
#include "geom-output.hpp"
1617
#include "geom.hpp"
1718

1819
TEST_CASE("geometry collection with point", "[NoDB]")

tests/test-geom-linestrings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "geom-from-osm.hpp"
1515
#include "geom-functions.hpp"
16+
#include "geom-output.hpp"
1617
#include "geom.hpp"
1718

1819
#include <array>

tests/test-geom-multilinestrings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "geom-from-osm.hpp"
1515
#include "geom-functions.hpp"
16+
#include "geom-output.hpp"
1617
#include "geom.hpp"
1718

1819
#include <array>

tests/test-geom-multipoints.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "geom-from-osm.hpp"
1515
#include "geom-functions.hpp"
16+
#include "geom-output.hpp"
1617
#include "geom.hpp"
1718

1819
#include <array>

tests/test-geom-multipolygons.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "geom-from-osm.hpp"
1515
#include "geom-functions.hpp"
16+
#include "geom-output.hpp"
1617
#include "geom.hpp"
1718

1819
TEST_CASE("multipolygon geometry with single outer, no inner", "[NoDB]")

tests/test-geom-null.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "geom-from-osm.hpp"
1313
#include "geom-functions.hpp"
14+
#include "geom-output.hpp"
1415
#include "geom.hpp"
1516

1617
TEST_CASE("null geometry", "[NoDB]")

tests/test-geom-output.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 "geom-output.hpp"
13+
14+
#include <sstream>
15+
16+
TEST_CASE("nullgeom_t output", "[NoDB]")
17+
{
18+
geom::nullgeom_t g;
19+
std::stringstream ss1;
20+
ss1 << g;
21+
CHECK(ss1.str() == "NULL");
22+
23+
geom::geometry_t geom;
24+
std::stringstream ss2;
25+
ss2 << geom;
26+
CHECK(ss2.str() == "NULL(NULL)");
27+
}
28+
29+
TEST_CASE("point_t output", "[NoDB]")
30+
{
31+
geom::point_t g{1, 2};
32+
std::stringstream ss1;
33+
ss1 << g;
34+
CHECK(ss1.str() == "1 2");
35+
36+
geom::geometry_t geom{std::move(g)};
37+
std::stringstream ss2;
38+
ss2 << geom;
39+
CHECK(ss2.str() == "POINT(1 2)");
40+
}
41+
42+
TEST_CASE("linestring_t output", "[NoDB]")
43+
{
44+
geom::linestring_t g{{1, 2}, {2, 2}};
45+
std::stringstream ss1;
46+
ss1 << g;
47+
CHECK(ss1.str() == "1 2,2 2");
48+
49+
geom::geometry_t geom{std::move(g)};
50+
std::stringstream ss2;
51+
ss2 << geom;
52+
CHECK(ss2.str() == "LINESTRING(1 2,2 2)");
53+
}
54+
55+
TEST_CASE("polygon_t with no inner rings output", "[NoDB]")
56+
{
57+
geom::polygon_t g{geom::ring_t{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}};
58+
std::stringstream ss1;
59+
ss1 << g;
60+
CHECK(ss1.str() == "(0 0,1 0,1 1,0 1,0 0)");
61+
62+
geom::geometry_t geom{std::move(g)};
63+
std::stringstream ss2;
64+
ss2 << geom;
65+
CHECK(ss2.str() == "POLYGON((0 0,1 0,1 1,0 1,0 0))");
66+
}
67+
68+
TEST_CASE("polygon_t with inner ring output", "[NoDB]")
69+
{
70+
geom::polygon_t g{geom::ring_t{{0, 0}, {3, 0}, {3, 3}, {0, 3}, {0, 0}}};
71+
g.add_inner_ring(geom::ring_t{{1, 1}, {1, 2}, {2, 2}, {2, 1}, {1, 1}});
72+
std::stringstream ss1;
73+
ss1 << g;
74+
CHECK(ss1.str() == "(0 0,3 0,3 3,0 3,0 0),(1 1,1 2,2 2,2 1,1 1)");
75+
76+
geom::geometry_t geom{std::move(g)};
77+
std::stringstream ss2;
78+
ss2 << geom;
79+
CHECK(ss2.str() == "POLYGON((0 0,3 0,3 3,0 3,0 0),(1 1,1 2,2 2,2 1,1 1))");
80+
}
81+
82+
TEST_CASE("multipoint_t output", "[NoDB]")
83+
{
84+
geom::multipoint_t g;
85+
g.add_geometry({1, 2});
86+
g.add_geometry({4, 3});
87+
std::stringstream ss1;
88+
ss1 << g;
89+
CHECK(ss1.str() == "(1 2),(4 3)");
90+
91+
geom::geometry_t geom{std::move(g)};
92+
std::stringstream ss2;
93+
ss2 << geom;
94+
CHECK(ss2.str() == "MULTIPOINT((1 2),(4 3))");
95+
}
96+
97+
TEST_CASE("multilinestring_t output", "[NoDB]")
98+
{
99+
geom::multilinestring_t g;
100+
g.add_geometry({{1, 2}, {2, 2}});
101+
g.add_geometry({{4, 3}, {1, 1}});
102+
std::stringstream ss1;
103+
ss1 << g;
104+
CHECK(ss1.str() == "(1 2,2 2),(4 3,1 1)");
105+
106+
geom::geometry_t geom{std::move(g)};
107+
std::stringstream ss2;
108+
ss2 << geom;
109+
CHECK(ss2.str() == "MULTILINESTRING((1 2,2 2),(4 3,1 1))");
110+
}
111+
112+
TEST_CASE("multipolygon_t output", "[NoDB]")
113+
{
114+
geom::multipolygon_t g;
115+
g.add_geometry(geom::polygon_t{geom::ring_t{{0, 0}, {0, 1}, {1, 1}}});
116+
g.add_geometry(geom::polygon_t{geom::ring_t{{2, 2}, {2, 3}, {3, 2}}});
117+
std::stringstream ss1;
118+
ss1 << g;
119+
CHECK(ss1.str() == "((0 0,0 1,1 1)),((2 2,2 3,3 2))");
120+
121+
geom::geometry_t geom{std::move(g)};
122+
std::stringstream ss2;
123+
ss2 << geom;
124+
CHECK(ss2.str() == "MULTIPOLYGON(((0 0,0 1,1 1)),((2 2,2 3,3 2)))");
125+
}
126+
127+
TEST_CASE("collection_t output", "[NoDB]")
128+
{
129+
geom::collection_t g;
130+
g.add_geometry(geom::geometry_t{
131+
geom::polygon_t{geom::ring_t{{0, 0}, {0, 1}, {1, 1}}}});
132+
g.add_geometry(geom::geometry_t{geom::point_t{2, 3}});
133+
std::stringstream ss1;
134+
ss1 << g;
135+
CHECK(ss1.str() == "POLYGON((0 0,0 1,1 1)),POINT(2 3)");
136+
137+
geom::geometry_t geom{std::move(g)};
138+
std::stringstream ss2;
139+
ss2 << geom;
140+
CHECK(ss2.str() == "GEOMETRYCOLLECTION(POLYGON((0 0,0 1,1 1)),POINT(2 3))");
141+
}

tests/test-geom-points.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "geom-from-osm.hpp"
1515
#include "geom-functions.hpp"
16+
#include "geom-output.hpp"
1617
#include "geom.hpp"
1718

1819
TEST_CASE("geom::point_t", "[NoDB]")

0 commit comments

Comments
 (0)