Skip to content

Commit 4d8b92c

Browse files
committed
Misc code cleanups.
* Use {} in constructors instead of () * Use "using" instead of "typedef" * Use osmium::Tags::has_tag() instead of rolling our own * Use const and noexcept in more places * Add some asserts * Formatting
1 parent c0454fb commit 4d8b92c

15 files changed

+137
-130
lines changed

src/coastline_handlers.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,15 @@ class CoastlineHandlerPass1 : public osmium::handler::Handler {
4848

4949
void way(const osmium::Way& way) {
5050
// We are only interested in ways tagged with natural=coastline.
51-
const char* natural = way.tags().get_value_by_key("natural");
52-
if (natural && !std::strcmp(natural, "coastline")) {
53-
const char* bogus = way.tags().get_value_by_key("coastline");
54-
if (bogus && !std::strcmp(bogus, "bogus")) {
51+
if (way.tags().has_tag("natural", "coastline")) {
52+
if (way.tags().has_tag("coastline", "bogus")) {
5553
return; // ignore bogus coastline in Antarctica
5654
}
5755
m_coastline_rings.add_way(way);
5856
}
5957
}
6058

61-
};
59+
}; // class CoastlineHandlerPass1
6260

6361
/**
6462
* Osmium handler for the second pass over the input file in which
@@ -89,8 +87,7 @@ class CoastlineHandlerPass2 : public osmium::handler::Handler {
8987
}
9088

9189
void node(const osmium::Node& node) {
92-
const char* natural = node.tags().get_value_by_key("natural");
93-
if (natural && !std::strcmp(natural, "coastline")) {
90+
if (node.tags().has_tag("natural", "coastline")) {
9491
try {
9592
m_output.add_error_point(m_factory.create_point(node), "tagged_node", node.id());
9693
} catch (const osmium::geometry_error&) {
@@ -104,6 +101,6 @@ class CoastlineHandlerPass2 : public osmium::handler::Handler {
104101
}
105102
}
106103

107-
};
104+
}; // class CoastlineHandlerPass2
108105

109106
#endif // COASTLINE_HANDLERS_HPP

src/coastline_polygons.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ std::unique_ptr<OGRPolygon> CoastlinePolygons::create_rectangular_polygon(double
4646
// make sure we are inside the bounds for the output SRS
4747
e.Intersect(srs.max_extent());
4848

49-
std::unique_ptr<OGRLinearRing> ring { new OGRLinearRing() };
49+
std::unique_ptr<OGRLinearRing> ring{new OGRLinearRing()};
5050
ring->addPoint(e.MinX, e.MinY);
5151
ring->addPoint(e.MinX, e.MaxY);
5252
ring->addPoint(e.MaxX, e.MaxY);
5353
ring->addPoint(e.MaxX, e.MinY);
5454
ring->closeRings();
5555

56-
std::unique_ptr<OGRPolygon> polygon { new OGRPolygon() };
56+
std::unique_ptr<OGRPolygon> polygon{new OGRPolygon()};
5757
polygon->addRingDirectly(ring.release());
5858
polygon->assignSpatialReference(srs.out());
5959

@@ -95,7 +95,7 @@ void CoastlinePolygons::split_geometry(std::unique_ptr<OGRGeometry>&& geom, int
9595
} else { // wkbMultiPolygon
9696
const auto mp = static_cast_unique_ptr<OGRMultiPolygon>(std::move(geom));
9797
while (mp->getNumGeometries() > 0) {
98-
std::unique_ptr<OGRPolygon> polygon { static_cast<OGRPolygon*>(mp->getGeometryRef(0)) };
98+
std::unique_ptr<OGRPolygon> polygon{static_cast<OGRPolygon*>(mp->getGeometryRef(0))};
9999
mp->removeGeometry(0, false);
100100
polygon->assignSpatialReference(srs.out());
101101
split_polygon(std::move(polygon), level);
@@ -108,7 +108,7 @@ void CoastlinePolygons::split_polygon(std::unique_ptr<OGRPolygon>&& polygon, int
108108
m_max_split_depth = level;
109109
}
110110

111-
int num_points = polygon->getExteriorRing()->getNumPoints();
111+
const int num_points = polygon->getExteriorRing()->getNumPoints();
112112
if (num_points <= m_max_points_in_polygon) {
113113
// do not split the polygon if it is small enough
114114
m_polygons.push_back(std::move(polygon));
@@ -139,7 +139,7 @@ void CoastlinePolygons::split_polygon(std::unique_ptr<OGRPolygon>&& polygon, int
139139
}
140140

141141
// split vertically
142-
double MidY = (envelope.MaxY+envelope.MinY) / 2;
142+
const double MidY = (envelope.MaxY+envelope.MinY) / 2;
143143

144144
b1 = create_rectangular_polygon(envelope.MinX, envelope.MinY, envelope.MaxX, MidY, m_expand);
145145
b2 = create_rectangular_polygon(envelope.MinX, MidY, envelope.MaxX, envelope.MaxY, m_expand);
@@ -151,15 +151,15 @@ void CoastlinePolygons::split_polygon(std::unique_ptr<OGRPolygon>&& polygon, int
151151
}
152152

153153
// split horizontally
154-
double MidX = (envelope.MaxX+envelope.MinX) / 2;
154+
const double MidX = (envelope.MaxX+envelope.MinX) / 2;
155155

156156
b1 = create_rectangular_polygon(envelope.MinX, envelope.MinY, MidX, envelope.MaxY, m_expand);
157157
b2 = create_rectangular_polygon(MidX, envelope.MinY, envelope.MaxX, envelope.MaxY, m_expand);
158158
}
159159

160160
// Use intersection with bbox polygons to split polygon into two halfes
161-
std::unique_ptr<OGRGeometry> geom1 { polygon->Intersection(b1.get()) };
162-
std::unique_ptr<OGRGeometry> geom2 { polygon->Intersection(b2.get()) };
161+
std::unique_ptr<OGRGeometry> geom1{polygon->Intersection(b1.get())};
162+
std::unique_ptr<OGRGeometry> geom2{polygon->Intersection(b2.get())};
163163

164164
if (geom1 && (geom1->getGeometryType() == wkbPolygon || geom1->getGeometryType() == wkbMultiPolygon) &&
165165
geom2 && (geom2->getGeometryType() == wkbPolygon || geom2->getGeometryType() == wkbMultiPolygon)) {
@@ -247,12 +247,12 @@ void CoastlinePolygons::add_line_to_output(std::unique_ptr<OGRLineString> line,
247247
// Add a coastline ring as LineString to output. Segments in this line that are
248248
// near the southern edge of the map or near the antimeridian are suppressed.
249249
void CoastlinePolygons::output_polygon_ring_as_lines(int max_points, const OGRLinearRing* ring) const {
250-
int num = ring->getNumPoints();
250+
const int num = ring->getNumPoints();
251251
assert(num > 2);
252252

253-
std::unique_ptr<OGRPoint> point1 { new OGRPoint };
254-
std::unique_ptr<OGRPoint> point2 { new OGRPoint };
255-
std::unique_ptr<OGRLineString> line { new OGRLineString };
253+
std::unique_ptr<OGRPoint> point1{new OGRPoint};
254+
std::unique_ptr<OGRPoint> point2{new OGRPoint};
255+
std::unique_ptr<OGRLineString> line{new OGRLineString};
256256

257257
ring->getPoint(0, point1.get());
258258
for (int i=1; i < num; ++i) {
@@ -262,7 +262,7 @@ void CoastlinePolygons::output_polygon_ring_as_lines(int max_points, const OGRLi
262262

263263
if (line->getNumPoints() >= max_points || !added) {
264264
if (line->getNumPoints() >= 2) {
265-
std::unique_ptr<OGRLineString> new_line { new OGRLineString };
265+
std::unique_ptr<OGRLineString> new_line{new OGRLineString};
266266
using std::swap;
267267
swap(line, new_line);
268268
add_line_to_output(std::move(new_line), ring->getSpatialReference());
@@ -333,7 +333,7 @@ void CoastlinePolygons::split_bbox(OGREnvelope e, polygon_vector_type&& v) {
333333

334334
if (e.MaxX - e.MinX < e.MaxY-e.MinY) {
335335
// split vertically
336-
double MidY = (e.MaxY+e.MinY) / 2;
336+
const double MidY = (e.MaxY+e.MinY) / 2;
337337

338338
e1.MinX = e.MinX;
339339
e1.MinY = e.MinY;
@@ -347,7 +347,7 @@ void CoastlinePolygons::split_bbox(OGREnvelope e, polygon_vector_type&& v) {
347347

348348
} else {
349349
// split horizontally
350-
double MidX = (e.MaxX+e.MinX) / 2;
350+
const double MidX = (e.MaxX+e.MinX) / 2;
351351

352352
e1.MinX = e.MinX;
353353
e1.MinY = e.MinY;
@@ -371,8 +371,8 @@ void CoastlinePolygons::split_bbox(OGREnvelope e, polygon_vector_type&& v) {
371371
OGREnvelope e;
372372
polygon->getEnvelope(&e);
373373

374-
bool e1_intersects_e = e1.Intersects(e);
375-
bool e2_intersects_e = e2.Intersects(e);
374+
const bool e1_intersects_e = e1.Intersects(e);
375+
const bool e2_intersects_e = e2.Intersects(e);
376376

377377
if (e1_intersects_e && e2_intersects_e) {
378378
v1.emplace_back(static_cast<OGRPolygon*>(polygon->clone()));

src/coastline_polygons.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class OGRSpatialReference;
3232
class OutputDatabase;
3333

34-
typedef std::vector<std::unique_ptr<OGRPolygon>> polygon_vector_type;
34+
using polygon_vector_type = std::vector<std::unique_ptr<OGRPolygon>>;
3535

3636
/**
3737
* A collection of land polygons created out of coastlines.
@@ -120,6 +120,6 @@ class CoastlinePolygons {
120120
/// Write all coastlines to the output database (as lines).
121121
void output_lines(int max_points) const;
122122

123-
};
123+
}; // class CoastlinePolygons
124124

125125
#endif // COASTLINE_POLYGONS_HPP

src/coastline_ring.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void CoastlineRing::close_ring() {
9595
}
9696

9797
void CoastlineRing::close_antarctica_ring(int epsg) {
98-
double min = epsg == 4326 ? -90.0 : -85.0511288;
98+
const double min = epsg == 4326 ? -90.0 : -85.0511288;
9999

100100
for (int lat = -78; lat > int(min); --lat) {
101101
m_way_node_list.emplace_back(0, osmium::Location(-179.99999, double(lat)));
@@ -137,18 +137,18 @@ std::unique_ptr<OGRLineString> CoastlineRing::ogr_linestring(osmium::geom::OGRFa
137137

138138
std::unique_ptr<OGRPoint> CoastlineRing::ogr_first_point() const {
139139
const osmium::NodeRef& node_ref = m_way_node_list.front();
140-
return std::unique_ptr<OGRPoint>(new OGRPoint(node_ref.lon(), node_ref.lat()));
140+
return std::unique_ptr<OGRPoint>{new OGRPoint(node_ref.lon(), node_ref.lat())};
141141
}
142142

143143
std::unique_ptr<OGRPoint> CoastlineRing::ogr_last_point() const {
144144
const osmium::NodeRef& node_ref = m_way_node_list.back();
145-
return std::unique_ptr<OGRPoint>(new OGRPoint(node_ref.lon(), node_ref.lat()));
145+
return std::unique_ptr<OGRPoint>{new OGRPoint(node_ref.lon(), node_ref.lat())};
146146
}
147147

148148
// Pythagoras doesn't work on a round earth but that is ok here, we only need a
149149
// rough measure anyway
150150
double CoastlineRing::distance_to_start_position(osmium::Location pos) const {
151-
osmium::Location p = m_way_node_list.front().location();
151+
const osmium::Location p = m_way_node_list.front().location();
152152
return (pos.lon() - p.lon()) * (pos.lon() - p.lon()) + (pos.lat() - p.lat()) * (pos.lat() - p.lat());
153153
}
154154

src/coastline_ring.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
2323
*/
2424

25+
#include <cassert>
2526
#include <map>
2627
#include <memory>
2728
#include <ostream>
@@ -35,7 +36,7 @@ class OGRPoint;
3536
class OGRLineString;
3637
class OGRPolygon;
3738

38-
typedef std::multimap<osmium::object_id_type, osmium::Location*> posmap_type;
39+
using posmap_type = std::multimap<osmium::object_id_type, osmium::Location*>;
3940

4041
/**
4142
* The CoastlineRing class models a (possibly unfinished) ring of
@@ -108,41 +109,45 @@ class CoastlineRing {
108109

109110
/// ID of first node in the ring.
110111
osmium::object_id_type first_node_id() const {
112+
assert(!m_way_node_list.empty());
111113
return m_way_node_list.front().ref();
112114
}
113115

114116
/// ID of last node in the ring.
115117
osmium::object_id_type last_node_id() const {
118+
assert(!m_way_node_list.empty());
116119
return m_way_node_list.back().ref();
117120
}
118121

119122
/// Position of the first node in the ring.
120123
osmium::Location first_position() const {
124+
assert(!m_way_node_list.empty());
121125
return m_way_node_list.front().location();
122126
}
123127

124128
/// Position of the last node in the ring.
125129
osmium::Location last_position() const {
130+
assert(!m_way_node_list.empty());
126131
return m_way_node_list.back().location();
127132
}
128133

129134
/// Return ID of this ring (defined as smallest ID of the ways making up the ring).
130-
osmium::object_id_type ring_id() const {
135+
osmium::object_id_type ring_id() const noexcept {
131136
return m_ring_id;
132137
}
133138

134139
/**
135140
* Set ring ID. The ring will only get the new ID if it is smaller than the
136141
* old one.
137142
*/
138-
void update_ring_id(osmium::object_id_type new_id) {
143+
void update_ring_id(osmium::object_id_type new_id) noexcept {
139144
if (new_id < m_ring_id) {
140145
m_ring_id = new_id;
141146
}
142147
}
143148

144149
/// Returns the number of ways making up this ring.
145-
unsigned int nways() const {
150+
unsigned int nways() const noexcept {
146151
return m_nways;
147152
}
148153

@@ -157,7 +162,7 @@ class CoastlineRing {
157162
}
158163

159164
/// Was this ring fixed because of missing/wrong OSM data?
160-
bool is_fixed() const {
165+
bool is_fixed() const noexcept {
161166
return m_fixed;
162167
}
163168

@@ -170,6 +175,7 @@ class CoastlineRing {
170175
* method does this.
171176
*/
172177
void fake_close() {
178+
assert(!m_way_node_list.empty());
173179
m_way_node_list.back().set_ref(first_node_id());
174180
}
175181

@@ -258,7 +264,7 @@ class CoastlineRing {
258264

259265
friend std::ostream& operator<<(std::ostream& out, const CoastlineRing& cp);
260266

261-
};
267+
}; // class CoastlineRing
262268

263269
inline bool operator<(const CoastlineRing& lhs, const CoastlineRing& rhs) {
264270
return lhs.first_position() < rhs.first_position();

0 commit comments

Comments
 (0)