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

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

Loading...
Searching...
No Matches
mercator_projection.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_GEOM_MERCATOR_PROJECTION_HPP
2#define OSMIUM_GEOM_MERCATOR_PROJECTION_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/geom/coordinates.hpp>
37#include <osmium/geom/util.hpp>
38#include <osmium/osm/location.hpp>
39
40#include <cmath>
41#include <string>
42
43namespace osmium {
44
45 namespace geom {
46
47 namespace detail {
48
49 constexpr double earth_radius_for_epsg3857 = 6378137.0;
50 constexpr double max_coordinate_epsg3857 = 20037508.34;
51
52 constexpr double lon_to_x(double lon) noexcept {
53 return earth_radius_for_epsg3857 * deg_to_rad(lon);
54 }
55
56 inline double lat_to_y_with_tan(double lat) { // not constexpr because math functions aren't
57 return earth_radius_for_epsg3857 * std::log(std::tan((osmium::geom::PI / 4) + (deg_to_rad(lat) / 2)));
58 }
59
60#ifdef OSMIUM_USE_SLOW_MERCATOR_PROJECTION
61 inline double lat_to_y(double lat) {
62 return lat_to_y_with_tan(lat);
63 }
64#else
65
66 // This is a much faster implementation than the canonical
67 // implementation using the tan() function. For details
68 // see https://github.com/osmcode/mercator-projection .
69 inline double lat_to_y(double lat) { // not constexpr because math functions aren't
70 if (lat < -78.0 || lat > 78.0) {
71 return lat_to_y_with_tan(lat);
72 }
73
74 return earth_radius_for_epsg3857 *
75 ((((((((((-3.1112583378460085319e-23 * lat +
76 2.0465852743943268009e-19) * lat +
77 6.4905282018672673884e-18) * lat +
78 -1.9685447939983315591e-14) * lat +
79 -2.2022588158115104182e-13) * lat +
80 5.1617537365509453239e-10) * lat +
81 2.5380136069803016519e-9) * lat +
82 -5.1448323697228488745e-6) * lat +
83 -9.4888671473357768301e-6) * lat +
84 1.7453292518154191887e-2) * lat)
85 /
86 ((((((((((-1.9741136066814230637e-22 * lat +
87 -1.258514031244679556e-20) * lat +
88 4.8141483273572351796e-17) * lat +
89 8.6876090870176172185e-16) * lat +
90 -2.3298743439377541768e-12) * lat +
91 -1.9300094785736130185e-11) * lat +
92 4.3251609106864178231e-8) * lat +
93 1.7301944508516974048e-7) * lat +
94 -3.4554675198786337842e-4) * lat +
95 -5.4367203601085991108e-4) * lat + 1.0);
96 }
97#endif
98
99 constexpr double x_to_lon(double x) {
100 return rad_to_deg(x) / earth_radius_for_epsg3857;
101 }
102
103 inline double y_to_lat(double y) { // not constexpr because math functions aren't
104 return rad_to_deg((2 * std::atan(std::exp(y / earth_radius_for_epsg3857))) - (osmium::geom::PI / 2));
105 }
106
107 } // namespace detail
108
113 constexpr double MERCATOR_MAX_LAT = 85.0511288;
114
123 inline Coordinates lonlat_to_mercator(const Coordinates& c) {
124 return Coordinates{detail::lon_to_x(c.x), detail::lat_to_y(c.y)};
125 }
126
134 inline Coordinates mercator_to_lonlat(const Coordinates& c) {
135 return Coordinates{detail::x_to_lon(c.x), detail::y_to_lat(c.y)};
136 }
137
142 class MercatorProjection {
143
144 public:
145
146 // This is not "= default" on purpose because some compilers don't
147 // like it and complain that "default initialization of an object
148 // of const type 'const osmium::geom::MercatorProjection' requires
149 // a user-provided default constructor".
150 MercatorProjection() { // NOLINT(hicpp-use-equals-default, modernize-use-equals-default)
151 }
152
160 Coordinates operator()(osmium::Location location) const {
161 return Coordinates{detail::lon_to_x(location.lon()), detail::lat_to_y(location.lat())};
162 }
163
164 static int epsg() noexcept {
165 return 3857;
166 }
167
168 static std::string proj_string() noexcept {
169 return "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs";
170 }
171
172 }; // class MercatorProjection
173
174 } // namespace geom
175
176} // namespace osmium
177
178#endif // OSMIUM_GEOM_MERCATOR_PROJECTION_HPP
osmium::Location
Definition location.hpp:279
osmium::Location::lon
double lon() const
Definition location.hpp:438
osmium::Location::lat
double lat() const
Definition location.hpp:457
osmium::geom::MercatorProjection::MercatorProjection
MercatorProjection()
Definition mercator_projection.hpp:150
osmium::geom::MercatorProjection::epsg
static int epsg() noexcept
Definition mercator_projection.hpp:164
osmium::geom::MercatorProjection::proj_string
static std::string proj_string() noexcept
Definition mercator_projection.hpp:168
osmium::geom::MercatorProjection::operator()
Coordinates operator()(osmium::Location location) const
Definition mercator_projection.hpp:160
coordinates.hpp
util.hpp
location.hpp
osmium::geom
Everything related to geometry handling.
Definition coordinates.hpp:46
osmium::geom::lonlat_to_mercator
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition mercator_projection.hpp:123
osmium::geom::deg_to_rad
constexpr double deg_to_rad(double degree) noexcept
Convert angle from degrees to radians.
Definition util.hpp:64
osmium::geom::rad_to_deg
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition util.hpp:69
osmium::geom::mercator_to_lonlat
Coordinates mercator_to_lonlat(const Coordinates &c)
Definition mercator_projection.hpp:134
osmium::geom::MERCATOR_MAX_LAT
constexpr double MERCATOR_MAX_LAT
Definition mercator_projection.hpp:113
osmium::geom::PI
constexpr double PI
Definition util.hpp:61
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::geom::Coordinates
Definition coordinates.hpp:48
osmium::geom::Coordinates::y
double y
Definition coordinates.hpp:51
osmium::geom::Coordinates::x
double x
Definition coordinates.hpp:50