Libosmium  2.15.6
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-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 
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  // cppcheck-suppress passedByValue (because c is small and we want to change it)
126  inline OSMIUM_DEPRECATED Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
127  const int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
128  if (result != 0) {
129  throw osmium::projection_error{std::string{"projection failed: "} + pj_strerrno(result)};
130  }
131  return c;
132  }
133 
147  class Projection {
148 
149  int m_epsg;
150  std::string m_proj_string;
153 
154  public:
155 
156  explicit Projection(const std::string& proj_string) :
157  m_epsg(-1),
160  }
161 
162  explicit Projection(const char* proj_string) :
163  m_epsg(-1),
166  }
167 
168  explicit Projection(int epsg) :
169  m_epsg(epsg),
170  m_proj_string(std::string{"+init=epsg:"} + std::to_string(epsg)),
171  m_crs_user(epsg) {
172  }
173 
181  if (m_epsg == 4326) {
182  return Coordinates{location.lon(), location.lat()};
183  }
184 
185  if (m_epsg == 3857) {
186  return Coordinates{detail::lon_to_x(location.lon()),
187  detail::lat_to_y(location.lat())};
188  }
189 
191  deg_to_rad(location.lat())})};
192  if (m_crs_user.is_latlong()) {
193  c.x = rad_to_deg(c.x);
194  c.y = rad_to_deg(c.y);
195  }
196 
197  return c;
198  }
199 
200  int epsg() const noexcept {
201  return m_epsg;
202  }
203 
204  std::string proj_string() const {
205  return m_proj_string;
206  }
207 
208  }; // class Projection
209 
210  } // namespace geom
211 
212 } // namespace osmium
213 
214 #endif // OSMIUM_GEOM_PROJECTION_HPP
osmium::geom::Projection::m_crs_user
CRS m_crs_user
Definition: projection.hpp:152
osmium::geom::CRS::m_crs
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:79
osmium::geom::CRS::CRS
CRS(const std::string &crs)
Definition: projection.hpp:90
mercator_projection.hpp
osmium::geom::CRS::CRS
CRS(const char *crs)
Definition: projection.hpp:83
OSMIUM_DEPRECATED
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
osmium::geom::CRS::ProjCRSDeleter
Definition: projection.hpp:73
osmium::geom::Projection::m_epsg
int m_epsg
Definition: projection.hpp:149
osmium::geom::CRS
Definition: projection.hpp:71
osmium::Location::lon
double lon() const
Definition: location.hpp:396
osmium::geom::Projection::Projection
Projection(const char *proj_string)
Definition: projection.hpp:162
osmium::geom::CRS::is_geocent
bool is_geocent() const noexcept
Definition: projection.hpp:109
osmium::geom::Coordinates::x
double x
Definition: coordinates.hpp:50
osmium::geom::CRS::is_latlong
bool is_latlong() const noexcept
Definition: projection.hpp:105
osmium::projection_error
Definition: util.hpp:45
util.hpp
osmium::geom::Projection::Projection
Projection(const std::string &proj_string)
Definition: projection.hpp:156
osmium::geom::Projection::epsg
int epsg() const noexcept
Definition: projection.hpp:200
osmium::Location::lat
double lat() const
Definition: location.hpp:415
compatibility.hpp
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::geom::CRS::get
projPJ get() const noexcept
Definition: projection.hpp:101
coordinates.hpp
osmium::geom::Projection::operator()
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:180
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::Projection::m_crs_wgs84
CRS m_crs_wgs84
Definition: projection.hpp:151
osmium::geom::CRS::ProjCRSDeleter::operator()
void operator()(void *crs)
Definition: projection.hpp:74
location.hpp
osmium::geom::Projection
Definition: projection.hpp:147
osmium::Location
Definition: location.hpp:271
osmium::geom::Projection::proj_string
std::string proj_string() const
Definition: projection.hpp:204
std
Definition: location.hpp:551
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::CRS::CRS
CRS(int epsg)
Definition: projection.hpp:94
osmium::geom::Projection::m_proj_string
std::string m_proj_string
Definition: projection.hpp:150
osmium::geom::Projection::Projection
Projection(int epsg)
Definition: projection.hpp:168
osmium::geom::transform
OSMIUM_DEPRECATED Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:126