Libosmium  2.15.5
Fast and flexible C++ library for working with OpenStreetMap data
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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2020 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
37 #include <osmium/geom/util.hpp>
38 #include <osmium/osm/location.hpp>
39 
40 #include <cmath>
41 #include <string>
42 
43 namespace 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 inline 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 inline 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 
124  return Coordinates{detail::lon_to_x(c.x), detail::lat_to_y(c.y)};
125  }
126 
135  return Coordinates{detail::x_to_lon(c.x), detail::y_to_lat(c.y)};
136  }
137 
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 
161  return Coordinates{detail::lon_to_x(location.lon()), detail::lat_to_y(location.lat())};
162  }
163 
164  int epsg() const noexcept {
165  return 3857;
166  }
167 
168  std::string proj_string() const {
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::geom::MercatorProjection::proj_string
std::string proj_string() const
Definition: mercator_projection.hpp:168
detail
Definition: attr.hpp:342
osmium::Location::lon
double lon() const
Definition: location.hpp:396
osmium::geom::Coordinates::x
double x
Definition: coordinates.hpp:50
osmium::geom::MERCATOR_MAX_LAT
constexpr double MERCATOR_MAX_LAT
Definition: mercator_projection.hpp:113
util.hpp
osmium::geom::MercatorProjection
Definition: mercator_projection.hpp:142
osmium::geom::lonlat_to_mercator
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition: mercator_projection.hpp:123
osmium::geom::PI
constexpr double PI
Definition: util.hpp:59
osmium::Location::lat
double lat() const
Definition: location.hpp:415
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
coordinates.hpp
osmium::geom::Coordinates::y
double y
Definition: coordinates.hpp:51
osmium::geom::deg_to_rad
constexpr double deg_to_rad(double degree) noexcept
Convert angle from degrees to radians.
Definition: util.hpp:62
osmium::geom::Coordinates
Definition: coordinates.hpp:48
osmium::geom::mercator_to_lonlat
Coordinates mercator_to_lonlat(const Coordinates &c)
Definition: mercator_projection.hpp:134
location.hpp
osmium::Location
Definition: location.hpp:271
osmium::geom::MercatorProjection::MercatorProjection
MercatorProjection()
Definition: mercator_projection.hpp:150
osmium::geom::MercatorProjection::epsg
int epsg() const noexcept
Definition: mercator_projection.hpp:164
osmium::geom::rad_to_deg
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67
osmium::geom::MercatorProjection::operator()
Coordinates operator()(osmium::Location location) const
Definition: mercator_projection.hpp:160