Libosmium  2.17.0
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-2021 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>
50 
51 #ifdef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
52 # include <proj_api.h>
53 #else
54 # define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
55 # include <proj_api.h>
56 # undef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
57 #endif
58 
59 #include <memory>
60 #include <string>
61 
62 namespace osmium {
63 
64  namespace geom {
65 
71  class CRS {
72 
73  struct ProjCRSDeleter {
74  void operator()(void* crs) {
75  pj_free(crs);
76  }
77  }; // struct ProjCRSDeleter
78 
79  std::unique_ptr<void, ProjCRSDeleter> m_crs;
80 
81  public:
82 
83  explicit CRS(const char* crs) :
84  m_crs(pj_init_plus(crs), ProjCRSDeleter()) {
85  if (!m_crs) {
86  throw osmium::projection_error{std::string{"creation of CRS failed: "} + pj_strerrno(*pj_get_errno_ref())};
87  }
88  }
89 
90  explicit CRS(const std::string& crs) :
91  CRS(crs.c_str()) {
92  }
93 
94  explicit CRS(int epsg) :
95  CRS(std::string{"+init=epsg:"} + std::to_string(epsg)) {
96  }
97 
101  projPJ get() const noexcept {
102  return m_crs.get();
103  }
104 
105  bool is_latlong() const noexcept {
106  return pj_is_latlong(m_crs.get()) != 0;
107  }
108 
109  bool is_geocent() const noexcept {
110  return pj_is_geocent(m_crs.get()) != 0;
111  }
112 
113  }; // class CRS
114 
125  inline OSMIUM_DEPRECATED Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
126  const int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
127  if (result != 0) {
128  throw osmium::projection_error{std::string{"projection failed: "} + pj_strerrno(result)};
129  }
130  return c;
131  }
132 
146  class Projection {
147 
148  int m_epsg;
149  std::string m_proj_string;
152 
153  public:
154 
155  explicit Projection(const std::string& proj_string) :
156  m_epsg(-1),
159  }
160 
161  explicit Projection(const char* proj_string) :
162  m_epsg(-1),
165  }
166 
167  explicit Projection(int epsg) :
168  m_epsg(epsg),
169  m_proj_string(std::string{"+init=epsg:"} + std::to_string(epsg)),
170  m_crs_user(epsg) {
171  }
172 
180  if (m_epsg == 4326) {
181  return Coordinates{location.lon(), location.lat()};
182  }
183 
184  if (m_epsg == 3857) {
185  return Coordinates{detail::lon_to_x(location.lon()),
186  detail::lat_to_y(location.lat())};
187  }
188 
190  deg_to_rad(location.lat())})};
191  if (m_crs_user.is_latlong()) {
192  c.x = rad_to_deg(c.x);
193  c.y = rad_to_deg(c.y);
194  }
195 
196  return c;
197  }
198 
199  int epsg() const noexcept {
200  return m_epsg;
201  }
202 
203  std::string proj_string() const {
204  return m_proj_string;
205  }
206 
207  }; // class Projection
208 
209  } // namespace geom
210 
211 } // namespace osmium
212 
213 #endif // OSMIUM_GEOM_PROJECTION_HPP
Definition: location.hpp:271
double lon() const
Definition: location.hpp:396
double lat() const
Definition: location.hpp:415
Definition: projection.hpp:71
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:79
CRS(const std::string &crs)
Definition: projection.hpp:90
CRS(int epsg)
Definition: projection.hpp:94
projPJ get() const noexcept
Definition: projection.hpp:101
bool is_latlong() const noexcept
Definition: projection.hpp:105
CRS(const char *crs)
Definition: projection.hpp:83
bool is_geocent() const noexcept
Definition: projection.hpp:109
Definition: projection.hpp:146
Projection(const std::string &proj_string)
Definition: projection.hpp:155
int epsg() const noexcept
Definition: projection.hpp:199
CRS m_crs_wgs84
Definition: projection.hpp:150
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:179
Projection(const char *proj_string)
Definition: projection.hpp:161
Projection(int epsg)
Definition: projection.hpp:167
std::string m_proj_string
Definition: projection.hpp:149
std::string proj_string() const
Definition: projection.hpp:203
int m_epsg
Definition: projection.hpp:148
CRS m_crs_user
Definition: projection.hpp:151
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
OSMIUM_DEPRECATED Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:125
constexpr double deg_to_rad(double degree) noexcept
Convert angle from degrees to radians.
Definition: util.hpp:62
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:551
Definition: projection.hpp:73
void operator()(void *crs)
Definition: projection.hpp:74
Definition: coordinates.hpp:48
double y
Definition: coordinates.hpp:51
double x
Definition: coordinates.hpp:50
Definition: util.hpp:45