Libosmium: include/osmium/geom/coordinates.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
coordinates.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_GEOM_COORDINATES_HPP
2#define OSMIUM_GEOM_COORDINATES_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/osm/location.hpp>
37#include <osmium/util/double.hpp>
38
39#include <cmath>
40#include <iosfwd>
41#include <limits>
42#include <string>
43
44namespace osmium {
45
46 namespace geom {
47
48 struct Coordinates {
49
50 double x;
51 double y;
52
56 Coordinates() noexcept :
57 x(std::numeric_limits<double>::quiet_NaN()),
58 y(std::numeric_limits<double>::quiet_NaN()) {
59 }
60
65 explicit Coordinates(double cx, double cy) noexcept :
66 x(cx),
67 y(cy) {
68 }
69
77 // cppcheck-suppress noExplicitConstructor
78 Coordinates(const osmium::Location& location) : // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
79 x(location.lon()),
80 y(location.lat()) {
81 }
82
86 bool valid() const noexcept {
87 return !std::isnan(x) && !std::isnan(y);
88 }
89
100 void append_to_string(std::string& s, const char infix, int precision) const {
101 if (valid()) {
102 osmium::double2string(s, x, precision);
103 s += infix;
104 osmium::double2string(s, y, precision);
105 } else {
106 s.append("invalid");
107 }
108 }
109
122 void append_to_string(std::string& s, const char prefix, const char infix, const char suffix, int precision) const {
123 s += prefix;
124 append_to_string(s, infix, precision);
125 s += suffix;
126 }
127
128 }; // struct coordinates
129
139 inline bool operator==(const Coordinates& lhs, const Coordinates& rhs) noexcept {
140 if (!lhs.valid() && !rhs.valid()) {
141 return true;
142 }
143#pragma GCC diagnostic push
144#pragma GCC diagnostic ignored "-Wfloat-equal"
145 return lhs.x == rhs.x && lhs.y == rhs.y;
146#pragma GCC diagnostic pop
147 }
148
149 inline bool operator!=(const Coordinates& lhs, const Coordinates& rhs) noexcept {
150 return !(lhs == rhs);
151 }
152
153 template <typename TChar, typename TTraits>
154 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Coordinates& c) {
155 return out << '(' << c.x << ',' << c.y << ')';
156 }
157
158 } // namespace geom
159
160} // namespace osmium
161
162#endif // OSMIUM_GEOM_COORDINATES_HPP
osmium::Location
Definition location.hpp:279
double.hpp
location.hpp
osmium::geom
Everything related to geometry handling.
Definition coordinates.hpp:46
osmium::geom::operator<<
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const Coordinates &c)
Definition coordinates.hpp:154
osmium::geom::operator!=
bool operator!=(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition coordinates.hpp:149
osmium::geom::operator==
bool operator==(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition coordinates.hpp:139
osmium::util::double2string
T double2string(T iterator, double value, int precision)
Definition double.hpp:56
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:654
osmium::geom::Coordinates
Definition coordinates.hpp:48
osmium::geom::Coordinates::append_to_string
void append_to_string(std::string &s, const char prefix, const char infix, const char suffix, int precision) const
Definition coordinates.hpp:122
osmium::geom::Coordinates::valid
bool valid() const noexcept
Definition coordinates.hpp:86
osmium::geom::Coordinates::y
double y
Definition coordinates.hpp:51
osmium::geom::Coordinates::Coordinates
Coordinates(double cx, double cy) noexcept
Definition coordinates.hpp:65
osmium::geom::Coordinates::append_to_string
void append_to_string(std::string &s, const char infix, int precision) const
Definition coordinates.hpp:100
osmium::geom::Coordinates::x
double x
Definition coordinates.hpp:50
osmium::geom::Coordinates::Coordinates
Coordinates(const osmium::Location &location)
Definition coordinates.hpp:78
osmium::geom::Coordinates::Coordinates
Coordinates() noexcept
Definition coordinates.hpp:56