Libosmium  2.13.1
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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 
45 #include <memory>
46 #include <string>
47 
48 #include <proj_api.h>
49 
52 #include <osmium/geom/util.hpp>
53 #include <osmium/osm/location.hpp>
54 
55 namespace osmium {
56 
57  namespace geom {
58 
62  class CRS {
63 
64  struct ProjCRSDeleter {
65  void operator()(void* crs) {
66  pj_free(crs);
67  }
68  }; // struct ProjCRSDeleter
69 
70  std::unique_ptr<void, ProjCRSDeleter> m_crs;
71 
72  public:
73 
74  explicit CRS(const char* crs) :
75  m_crs(pj_init_plus(crs), ProjCRSDeleter()) {
76  if (!m_crs) {
77  throw osmium::projection_error{std::string{"creation of CRS failed: "} + pj_strerrno(*pj_get_errno_ref())};
78  }
79  }
80 
81  explicit CRS(const std::string& crs) :
82  CRS(crs.c_str()) {
83  }
84 
85  explicit CRS(int epsg) :
86  CRS(std::string{"+init=epsg:"} + std::to_string(epsg)) {
87  }
88 
92  projPJ get() const noexcept {
93  return m_crs.get();
94  }
95 
96  bool is_latlong() const noexcept {
97  return pj_is_latlong(m_crs.get()) != 0;
98  }
99 
100  bool is_geocent() const noexcept {
101  return pj_is_geocent(m_crs.get()) != 0;
102  }
103 
104  }; // class CRS
105 
114  // cppcheck-suppress passedByValue (because c is small and we want to change it)
115  inline Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
116  const int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
117  if (result != 0) {
118  throw osmium::projection_error{std::string{"projection failed: "} + pj_strerrno(result)};
119  }
120  return c;
121  }
122 
134  class Projection {
135 
136  int m_epsg;
137  std::string m_proj_string;
138  CRS m_crs_wgs84{4326};
140 
141  public:
142 
143  explicit Projection(const std::string& proj_string) :
144  m_epsg(-1),
145  m_proj_string(proj_string),
146  m_crs_user(proj_string) {
147  }
148 
149  explicit Projection(const char* proj_string) :
150  m_epsg(-1),
151  m_proj_string(proj_string),
152  m_crs_user(proj_string) {
153  }
154 
155  explicit Projection(int epsg) :
156  m_epsg(epsg),
157  m_proj_string(std::string{"+init=epsg:"} + std::to_string(epsg)),
158  m_crs_user(epsg) {
159  }
160 
162  if (m_epsg == 4326) {
163  return Coordinates{location.lon(), location.lat()};
164  } else if (m_epsg == 3857) {
165  return Coordinates{detail::lon_to_x(location.lon()),
166  detail::lat_to_y(location.lat())};
167  }
168 
169  Coordinates c{transform(m_crs_wgs84, m_crs_user, Coordinates{deg_to_rad(location.lon()),
170  deg_to_rad(location.lat())})};
171  if (m_crs_user.is_latlong()) {
172  c.x = rad_to_deg(c.x);
173  c.y = rad_to_deg(c.y);
174  }
175 
176  return c;
177  }
178 
179  int epsg() const noexcept {
180  return m_epsg;
181  }
182 
183  std::string proj_string() const {
184  return m_proj_string;
185  }
186 
187  }; // class Projection
188 
189  } // namespace geom
190 
191 } // namespace osmium
192 
193 #endif // OSMIUM_GEOM_PROJECTION_HPP
double y
Definition: coordinates.hpp:51
projPJ get() const noexcept
Definition: projection.hpp:92
double lon() const
Definition: location.hpp:401
std::string proj_string() const
Definition: projection.hpp:183
bool is_latlong() const noexcept
Definition: projection.hpp:96
Definition: projection.hpp:64
double lat() const
Definition: location.hpp:420
Definition: reader_iterator.hpp:39
CRS(int epsg)
Definition: projection.hpp:85
bool is_geocent() const noexcept
Definition: projection.hpp:100
Projection(int epsg)
Definition: projection.hpp:155
CRS(const std::string &crs)
Definition: projection.hpp:81
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:115
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:137
Definition: coordinates.hpp:48
CRS m_crs_user
Definition: projection.hpp:139
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:70
CRS(const char *crs)
Definition: projection.hpp:74
Definition: projection.hpp:134
Definition: location.hpp:273
int m_epsg
Definition: projection.hpp:136
void operator()(void *crs)
Definition: projection.hpp:65
Definition: projection.hpp:62
Projection(const std::string &proj_string)
Definition: projection.hpp:143
Projection(const char *proj_string)
Definition: projection.hpp:149
int epsg() const noexcept
Definition: projection.hpp:179
double x
Definition: coordinates.hpp:50
Definition: util.hpp:45
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:161
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67