Libosmium  2.15.0
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-2018 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) {
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 
121  return Coordinates{detail::lon_to_x(c.x), detail::lat_to_y(c.y)};
122  }
123 
130  return Coordinates{detail::x_to_lon(c.x), detail::y_to_lat(c.y)};
131  }
132 
138 
139  public:
140 
141  // This is not "= default" on purpose because some compilers don't
142  // like it and complain that "default initialization of an object
143  // of const type 'const osmium::geom::MercatorProjection' requires
144  // a user-provided default constructor".
145  MercatorProjection() { // NOLINT(hicpp-use-equals-default, modernize-use-equals-default)
146  }
147 
149  return Coordinates{detail::lon_to_x(location.lon()), detail::lat_to_y(location.lat())};
150  }
151 
152  int epsg() const noexcept {
153  return 3857;
154  }
155 
156  std::string proj_string() const {
157  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";
158  }
159 
160  }; // class MercatorProjection
161 
162  } // namespace geom
163 
164 } // namespace osmium
165 
166 #endif // OSMIUM_GEOM_MERCATOR_PROJECTION_HPP
double y
Definition: coordinates.hpp:51
double lon() const
Definition: location.hpp:399
Coordinates mercator_to_lonlat(const Coordinates &c)
Definition: mercator_projection.hpp:129
MercatorProjection()
Definition: mercator_projection.hpp:145
std::string proj_string() const
Definition: mercator_projection.hpp:156
double lat() const
Definition: location.hpp:418
Coordinates operator()(osmium::Location location) const
Definition: mercator_projection.hpp:148
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
constexpr double deg_to_rad(double degree) noexcept
Convert angle from degrees to radians.
Definition: util.hpp:62
Definition: attr.hpp:333
Definition: coordinates.hpp:48
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition: mercator_projection.hpp:120
constexpr double MERCATOR_MAX_LAT
Definition: mercator_projection.hpp:113
Definition: location.hpp:275
Definition: mercator_projection.hpp:137
double x
Definition: coordinates.hpp:50
int epsg() const noexcept
Definition: mercator_projection.hpp:152
constexpr double PI
Definition: util.hpp:59
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67