Skip to content

Commit e1ab481

Browse files
committed
Refactor: Make fill_point_list return a bool showing validity
This allows easier and more consistent checks of the validity.
1 parent 5b5685f commit e1ab481

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/geom-from-osm.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ geometry_t create_point(osmium::Node const &node)
2929
return geometry_t{point_t{node.location()}};
3030
}
3131

32-
static void fill_point_list(point_list_t *list,
32+
/**
33+
* Fill point list with locations from nodes list. Consecutive identical
34+
* locations are collapsed into a single point.
35+
*
36+
* Returns true if the result is a valid linestring, i.e. it has more than
37+
* one point.
38+
*/
39+
static bool fill_point_list(point_list_t *list,
3340
osmium::NodeRefList const &nodes)
3441
{
3542
osmium::Location last{};
@@ -41,16 +48,15 @@ static void fill_point_list(point_list_t *list,
4148
last = loc;
4249
}
4350
}
51+
52+
return list->size() > 1;
4453
}
4554

4655
void create_linestring(geometry_t *geom, osmium::Way const &way)
4756
{
4857
auto &line = geom->set<linestring_t>();
4958

50-
fill_point_list(&line, way.nodes());
51-
52-
// Return nullgeom_t if the line geometry is invalid
53-
if (line.size() <= 1U) {
59+
if (!fill_point_list(&line, way.nodes())) {
5460
geom->reset();
5561
}
5662
}
@@ -103,16 +109,14 @@ void create_multilinestring(geometry_t *geom,
103109
if (ways.size() == 1 && !force_multi) {
104110
auto &line = geom->set<linestring_t>();
105111
auto &way = *ways.begin();
106-
fill_point_list(&line, way.nodes());
107-
if (line.size() < 2U) {
112+
if (!fill_point_list(&line, way.nodes())) {
108113
geom->reset();
109114
}
110115
} else {
111116
auto &multiline = geom->set<multilinestring_t>();
112117
for (auto const &way : ways) {
113118
linestring_t line;
114-
fill_point_list(&line, way.nodes());
115-
if (line.size() >= 2U) {
119+
if (fill_point_list(&line, way.nodes())) {
116120
multiline.add_geometry(std::move(line));
117121
}
118122
}
@@ -197,8 +201,7 @@ void create_collection(geometry_t *geom, osmium::memory::Buffer const &buffer)
197201
auto const &way = static_cast<osmium::Way const &>(obj);
198202
geometry_t item;
199203
auto &line = item.set<linestring_t>();
200-
fill_point_list(&line, way.nodes());
201-
if (line.size() >= 2U) {
204+
if (fill_point_list(&line, way.nodes())) {
202205
collection.add_geometry(std::move(item));
203206
}
204207
}

0 commit comments

Comments
 (0)