Skip to content

Commit 5575577

Browse files
authored
Merge pull request #1706 from joto/geom-two-funcs
All geometry creation functions are now available in two variants
2 parents 0df433b + 122c99c commit 5575577

File tree

2 files changed

+110
-22
lines changed

2 files changed

+110
-22
lines changed

src/geom-from-osm.cpp

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717

1818
namespace geom {
1919

20+
void create_point(geometry_t *geom, osmium::Node const &node)
21+
{
22+
auto &point = geom->set<point_t>();
23+
point.set_x(node.location().lon());
24+
point.set_y(node.location().lat());
25+
}
26+
2027
geometry_t create_point(osmium::Node const &node)
2128
{
2229
return geometry_t{point_t{node.location()}};
@@ -36,29 +43,33 @@ static void fill_point_list(point_list_t *list,
3643
}
3744
}
3845

39-
geometry_t create_linestring(osmium::Way const &way)
46+
void create_linestring(geometry_t *geom, osmium::Way const &way)
4047
{
41-
geometry_t geom{linestring_t{}};
42-
auto &line = geom.get<linestring_t>();
48+
auto &line = geom->set<linestring_t>();
4349

4450
fill_point_list(&line, way.nodes());
4551

4652
// Return nullgeom_t if the line geometry is invalid
4753
if (line.size() <= 1U) {
48-
geom.reset();
54+
geom->reset();
4955
}
56+
}
5057

58+
geometry_t create_linestring(osmium::Way const &way)
59+
{
60+
geometry_t geom{};
61+
create_linestring(&geom, way);
5162
return geom;
5263
}
5364

54-
geometry_t create_polygon(osmium::Way const &way)
65+
void create_polygon(geometry_t *geom, osmium::Way const &way)
5566
{
56-
geometry_t geom{polygon_t{}};
67+
auto &polygon = geom->set<polygon_t>();
5768

5869
// A closed way with less than 4 nodes can never be a valid polygon
5970
if (way.nodes().size() < 4U) {
60-
geom.reset();
61-
return geom;
71+
geom->reset();
72+
return;
6273
}
6374

6475
osmium::area::AssemblerConfig area_config;
@@ -67,22 +78,27 @@ geometry_t create_polygon(osmium::Way const &way)
6778
osmium::memory::Buffer area_buffer{1024};
6879

6980
if (!assembler(way, area_buffer)) {
70-
geom.reset();
71-
return geom;
81+
geom->reset();
82+
return;
7283
}
7384

7485
auto const &area = area_buffer.get<osmium::Area>(0);
7586
auto const &ring = *area.begin<osmium::OuterRing>();
7687

77-
fill_point_list(&geom.get<polygon_t>().outer(), ring);
88+
fill_point_list(&polygon.outer(), ring);
89+
}
7890

91+
geometry_t create_polygon(osmium::Way const &way)
92+
{
93+
geometry_t geom{};
94+
create_polygon(&geom, way);
7995
return geom;
8096
}
8197

82-
geometry_t create_multilinestring(osmium::memory::Buffer const &ways)
98+
void create_multilinestring(geometry_t *geom,
99+
osmium::memory::Buffer const &ways)
83100
{
84-
geometry_t geom{multilinestring_t{}};
85-
auto &mls = geom.get<multilinestring_t>();
101+
auto &mls = geom->set<multilinestring_t>();
86102

87103
for (auto const &way : ways.select<osmium::Way>()) {
88104
linestring_t line;
@@ -93,9 +109,14 @@ geometry_t create_multilinestring(osmium::memory::Buffer const &ways)
93109
}
94110

95111
if (mls.num_geometries() == 0) {
96-
geom.reset();
112+
geom->reset();
97113
}
114+
}
98115

116+
geometry_t create_multilinestring(osmium::memory::Buffer const &ways)
117+
{
118+
geometry_t geom{};
119+
create_multilinestring(&geom, ways);
99120
return geom;
100121
}
101122

@@ -116,34 +137,39 @@ static void fill_polygon(polygon_t *polygon, osmium::Area const &area,
116137
}
117138
}
118139

119-
geometry_t create_multipolygon(osmium::Relation const &relation,
120-
osmium::memory::Buffer const &way_buffer)
140+
void create_multipolygon(geometry_t *geom, osmium::Relation const &relation,
141+
osmium::memory::Buffer const &way_buffer)
121142
{
122-
geometry_t geom{};
123-
124143
osmium::area::AssemblerConfig area_config;
125144
area_config.ignore_invalid_locations = true;
126145
osmium::area::GeomAssembler assembler{area_config};
127146
osmium::memory::Buffer area_buffer{1024};
128147

129148
if (!assembler(relation, way_buffer, area_buffer)) {
130-
return geom;
149+
geom->reset();
150+
return;
131151
}
132152

133153
auto const &area = area_buffer.get<osmium::Area>(0);
134154

135155
if (area.is_multipolygon()) {
136-
auto &multipolygon = geom.set<multipolygon_t>();
156+
auto &multipolygon = geom->set<multipolygon_t>();
137157

138158
for (auto const &outer : area.outer_rings()) {
139159
auto &polygon = multipolygon.add_geometry();
140160
fill_polygon(&polygon, area, outer);
141161
}
142162
} else {
143-
auto &polygon = geom.set<polygon_t>();
163+
auto &polygon = geom->set<polygon_t>();
144164
fill_polygon(&polygon, area, *area.outer_rings().begin());
145165
}
166+
}
146167

168+
geometry_t create_multipolygon(osmium::Relation const &relation,
169+
osmium::memory::Buffer const &way_buffer)
170+
{
171+
geometry_t geom{};
172+
create_multipolygon(&geom, relation, way_buffer);
147173
return geom;
148174
}
149175

src/geom-from-osm.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,22 @@
1919
* \file
2020
*
2121
* Functions to create geometries from OSM data.
22+
*
23+
* All functions here are available in two versions, one taking a pointer
24+
* to an existing geometry as output parameter, the second which returns
25+
* a new geometry instead.
2226
*/
2327

2428
namespace geom {
2529

30+
/**
31+
* Create a point geometry from a node.
32+
*
33+
* \param geom Pointer to an existing geometry which will be used as output.
34+
* \param node The input node.
35+
*/
36+
void create_point(geometry_t *geom, osmium::Node const &node);
37+
2638
/**
2739
* Create a point geometry from a node.
2840
*
@@ -31,6 +43,19 @@ namespace geom {
3143
*/
3244
geometry_t create_point(osmium::Node const &node);
3345

46+
/**
47+
* Create a linestring geometry from a way. Nodes without location are ignored.
48+
* Consecutive nodes with the same location will only end up once in the
49+
* linestring.
50+
*
51+
* If the resulting linestring would be invalid (< 1 nodes), a null geometry
52+
* is created.
53+
*
54+
* \param geom Pointer to an existing geometry which will be used as output.
55+
* \param way The input way.
56+
*/
57+
void create_linestring(geometry_t *geom, osmium::Way const &way);
58+
3459
/**
3560
* Create a linestring geometry from a way. Nodes without location are ignored.
3661
* Consecutive nodes with the same location will only end up once in the
@@ -44,6 +69,16 @@ geometry_t create_point(osmium::Node const &node);
4469
*/
4570
geometry_t create_linestring(osmium::Way const &way);
4671

72+
/**
73+
* Create a polygon geometry from a way.
74+
*
75+
* If the resulting polygon would be invalid, a null geometry is returned.
76+
*
77+
* \param geom Pointer to an existing geometry which will be used as output.
78+
* \param way The input way.
79+
*/
80+
void create_polygon(geometry_t *geom, osmium::Way const &way);
81+
4782
/**
4883
* Create a polygon geometry from a way.
4984
*
@@ -54,6 +89,20 @@ geometry_t create_linestring(osmium::Way const &way);
5489
*/
5590
geometry_t create_polygon(osmium::Way const &way);
5691

92+
/**
93+
* Create a multilinestring geometry from a bunch of ways (usually this
94+
* would be used for member ways of a relation). The result is always a
95+
* multilinestring, even if it only contains one linestring.
96+
*
97+
* If the resulting multilinestring would be invalid, a null geometry is
98+
* returned.
99+
*
100+
* \param geom Pointer to an existing geometry which will be used as output.
101+
* \param ways Buffer containing all the input ways.
102+
*/
103+
void create_multilinestring(geometry_t *geom,
104+
osmium::memory::Buffer const &ways);
105+
57106
/**
58107
* Create a multilinestring geometry from a bunch of ways (usually this
59108
* would be used for member ways of a relation). The result is always a
@@ -67,6 +116,19 @@ geometry_t create_polygon(osmium::Way const &way);
67116
*/
68117
geometry_t create_multilinestring(osmium::memory::Buffer const &ways);
69118

119+
/**
120+
* Create a (multi)polygon geometry from a relation and member ways.
121+
*
122+
* If the resulting (multi)polygon would be invalid, a null geometry is
123+
* returned.
124+
*
125+
* \param geom Pointer to an existing geometry which will be used as output.
126+
* \param relation The input relation.
127+
* \param way_buffer Buffer containing all member ways.
128+
*/
129+
void create_multipolygon(geometry_t *geom, osmium::Relation const &relation,
130+
osmium::memory::Buffer const &way_buffer);
131+
70132
/**
71133
* Create a (multi)polygon geometry from a relation and member ways.
72134
*

0 commit comments

Comments
 (0)