Libosmium  2.15.4
Fast and flexible C++ library for working with OpenStreetMap data
projection.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_PROJECTION_HPP
2 #define OSMIUM_GEOM_PROJECTION_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2019 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 
47 #include <osmium/geom/util.hpp>
48 #include <osmium/osm/location.hpp>
49 
50 #ifdef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
51 # include <proj_api.h>
52 #else
53 # define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
54 # include <proj_api.h>
55 # undef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
56 #endif
57 
58 #include <memory>
59 #include <string>
60 
61 namespace osmium {
62 
63  namespace geom {
64 
68  class CRS {
69 
70  struct ProjCRSDeleter {
71  void operator()(void* crs) {
72  pj_free(crs);
73  }
74  }; // struct ProjCRSDeleter
75 
76  std::unique_ptr<void, ProjCRSDeleter> m_crs;
77 
78  public:
79 
80  explicit CRS(const char* crs) :
81  m_crs(pj_init_plus(crs), ProjCRSDeleter()) {
82  if (!m_crs) {
83  throw osmium::projection_error{std::string{"creation of CRS failed: "} + pj_strerrno(*pj_get_errno_ref())};
84  }
85  }
86 
87  explicit CRS(const std::string& crs) :
88  CRS(crs.c_str()) {
89  }
90 
91  explicit CRS(int epsg) :
92  CRS(std::string{"+init=epsg:"} + std::to_string(epsg)) {
93  }
94 
98  projPJ get() const noexcept {
99  return m_crs.get();
100  }
101 
102  bool is_latlong() const noexcept {
103  return pj_is_latlong(m_crs.get()) != 0;
104  }
105 
106  bool is_geocent() const noexcept {
107  return pj_is_geocent(m_crs.get()) != 0;
108  }
109 
110  }; // class CRS
111 
120  // cppcheck-suppress passedByValue (because c is small and we want to change it)
121  inline Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
122  const int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
123  if (result != 0) {
124  throw osmium::projection_error{std::string{"projection failed: "} + pj_strerrno(result)};
125  }
126  return c;
127  }
128 
140  class Projection {
141 
142  int m_epsg;
143  std::string m_proj_string;
144  CRS m_crs_wgs84{4326};
146 
147  public:
148 
149  explicit Projection(const std::string& proj_string) :
150  m_epsg(-1),
151  m_proj_string(proj_string),
152  m_crs_user(proj_string) {
153  }
154 
155  explicit Projection(const char* proj_string) :
156  m_epsg(-1),
157  m_proj_string(proj_string),
158  m_crs_user(proj_string) {
159  }
160 
161  explicit Projection(int epsg) :
162  m_epsg(epsg),
163  m_proj_string(std::string{"+init=epsg:"} + std::to_string(epsg)),
164  m_crs_user(epsg) {
165  }
166 
174  if (m_epsg == 4326) {
175  return Coordinates{location.lon(), location.lat()};
176  }
177 
178  if (m_epsg == 3857) {
179  return Coordinates{detail::lon_to_x(location.lon()),
180  detail::lat_to_y(location.lat())};
181  }
182 
183  Coordinates c{transform(m_crs_wgs84, m_crs_user, Coordinates{deg_to_rad(location.lon()),
184  deg_to_rad(location.lat())})};
185  if (m_crs_user.is_latlong()) {
186  c.x = rad_to_deg(c.x);
187  c.y = rad_to_deg(c.y);
188  }
189 
190  return c;
191  }
192 
193  int epsg() const noexcept {
194  return m_epsg;
195  }
196 
197  std::string proj_string() const {
198  return m_proj_string;
199  }
200 
201  }; // class Projection
202 
203  } // namespace geom
204 
205 } // namespace osmium
206 
207 #endif // OSMIUM_GEOM_PROJECTION_HPP
double y
Definition: coordinates.hpp:51
projPJ get() const noexcept
Definition: projection.hpp:98
double lon() const
Definition: location.hpp:395
std::string proj_string() const
Definition: projection.hpp:197
bool is_latlong() const noexcept
Definition: projection.hpp:102
Definition: projection.hpp:70
double lat() const
Definition: location.hpp:414
Definition: location.hpp:550
CRS(int epsg)
Definition: projection.hpp:91
bool is_geocent() const noexcept
Definition: projection.hpp:106
Projection(int epsg)
Definition: projection.hpp:161
CRS(const std::string &crs)
Definition: projection.hpp:87
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:121
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
std::string m_proj_string
Definition: projection.hpp:143
Definition: coordinates.hpp:48
CRS m_crs_user
Definition: projection.hpp:145
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:76
CRS(const char *crs)
Definition: projection.hpp:80
Definition: projection.hpp:140
Definition: location.hpp:271
int m_epsg
Definition: projection.hpp:142
void operator()(void *crs)
Definition: projection.hpp:71
Definition: projection.hpp:68
Projection(const std::string &proj_string)
Definition: projection.hpp:149
Projection(const char *proj_string)
Definition: projection.hpp:155
int epsg() const noexcept
Definition: projection.hpp:193
double x
Definition: coordinates.hpp:50
Definition: util.hpp:45
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:173
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67