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;
146 
147  public:
148 
149  explicit Projection(const std::string& proj_string) :
150  m_epsg(-1),
153  }
154 
155  explicit Projection(const char* proj_string) :
156  m_epsg(-1),
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 
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
osmium::geom::Projection::m_crs_user
CRS m_crs_user
Definition: projection.hpp:145
osmium::geom::CRS::m_crs
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:76
osmium::geom::CRS::CRS
CRS(const std::string &crs)
Definition: projection.hpp:87
mercator_projection.hpp
osmium::geom::CRS::CRS
CRS(const char *crs)
Definition: projection.hpp:80
osmium::geom::CRS::ProjCRSDeleter
Definition: projection.hpp:70
osmium::geom::Projection::m_epsg
int m_epsg
Definition: projection.hpp:142
osmium::geom::CRS
Definition: projection.hpp:68
osmium::Location::lon
double lon() const
Definition: location.hpp:395
osmium::geom::Projection::Projection
Projection(const char *proj_string)
Definition: projection.hpp:155
osmium::geom::CRS::is_geocent
bool is_geocent() const noexcept
Definition: projection.hpp:106
osmium::geom::Coordinates::x
double x
Definition: coordinates.hpp:50
osmium::geom::CRS::is_latlong
bool is_latlong() const noexcept
Definition: projection.hpp:102
osmium::projection_error
Definition: util.hpp:45
util.hpp
osmium::geom::Projection::Projection
Projection(const std::string &proj_string)
Definition: projection.hpp:149
osmium::geom::Projection::epsg
int epsg() const noexcept
Definition: projection.hpp:193
osmium::Location::lat
double lat() const
Definition: location.hpp:414
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::geom::CRS::get
projPJ get() const noexcept
Definition: projection.hpp:98
coordinates.hpp
osmium::geom::Projection::operator()
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:173
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:144
osmium::geom::CRS::ProjCRSDeleter::operator()
void operator()(void *crs)
Definition: projection.hpp:71
location.hpp
osmium::geom::Projection
Definition: projection.hpp:140
osmium::Location
Definition: location.hpp:271
osmium::geom::Projection::proj_string
std::string proj_string() const
Definition: projection.hpp:197
std
Definition: location.hpp:550
osmium::geom::transform
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:121
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:91
osmium::geom::Projection::m_proj_string
std::string m_proj_string
Definition: projection.hpp:143
osmium::geom::Projection::Projection
Projection(int epsg)
Definition: projection.hpp:161