Libosmium  2.2.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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 
36 #include <cmath>
37 #include <string>
38 
40 #include <osmium/geom/util.hpp>
41 #include <osmium/osm/location.hpp>
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(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  constexpr inline double x_to_lon(double x) {
61  return rad_to_deg(x) / earth_radius_for_epsg3857;
62  }
63 
64  inline double y_to_lat(double y) { // not constexpr because math functions aren't
65  return rad_to_deg(2 * std::atan(std::exp(y / earth_radius_for_epsg3857)) - osmium::geom::PI/2);
66  }
67 
68  } // namespace detail
69 
74  constexpr double MERCATOR_MAX_LAT = 85.0511288;
75 
77  return Coordinates(detail::lon_to_x(c.x), detail::lat_to_y(c.y));
78  }
79 
81  return Coordinates(detail::x_to_lon(c.x), detail::y_to_lat(c.y));
82  }
83 
89 
90  public:
91 
93  return Coordinates {detail::lon_to_x(location.lon()), detail::lat_to_y(location.lat())};
94  }
95 
96  int epsg() const noexcept {
97  return 3857;
98  }
99 
100  std::string proj_string() const {
101  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";
102  }
103 
104  }; // class MercatorProjection
105 
106  } // namespace geom
107 
108 } // namespace osmium
109 
110 #endif // OSMIUM_GEOM_MERCATOR_PROJECTION_HPP
Coordinates operator()(osmium::Location location) const
Definition: mercator_projection.hpp:92
double y
Definition: coordinates.hpp:50
Coordinates mercator_to_lonlat(const Coordinates &c)
Definition: mercator_projection.hpp:80
std::string proj_string() const
Definition: mercator_projection.hpp:100
double lat() const
Definition: location.hpp:205
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
constexpr double deg_to_rad(double degree) noexcept
Convert angle from degrees to radians.
Definition: util.hpp:62
Definition: coordinates.hpp:47
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition: mercator_projection.hpp:76
constexpr double MERCATOR_MAX_LAT
Definition: mercator_projection.hpp:74
Definition: location.hpp:79
Definition: mercator_projection.hpp:88
double lon() const
Definition: location.hpp:186
double x
Definition: coordinates.hpp:49
int epsg() const noexcept
Definition: mercator_projection.hpp:96
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