Libosmium  2.12.2
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  inline Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
115  const int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
116  if (result != 0) {
117  throw osmium::projection_error{std::string("projection failed: ") + pj_strerrno(result)};
118  }
119  return c;
120  }
121 
133  class Projection {
134 
135  int m_epsg;
136  std::string m_proj_string;
137  CRS m_crs_wgs84{4326};
139 
140  public:
141 
142  explicit Projection(const std::string& proj_string) :
143  m_epsg(-1),
144  m_proj_string(proj_string),
145  m_crs_user(proj_string) {
146  }
147 
148  explicit Projection(const char* proj_string) :
149  m_epsg(-1),
150  m_proj_string(proj_string),
151  m_crs_user(proj_string) {
152  }
153 
154  explicit Projection(int epsg) :
155  m_epsg(epsg),
156  m_proj_string(std::string{"+init=epsg:"} + std::to_string(epsg)),
157  m_crs_user(epsg) {
158  }
159 
161  if (m_epsg == 4326) {
162  return Coordinates{location.lon(), location.lat()};
163  } else if (m_epsg == 3857) {
164  return Coordinates{detail::lon_to_x(location.lon()),
165  detail::lat_to_y(location.lat())};
166  }
167 
168  Coordinates c{transform(m_crs_wgs84, m_crs_user, Coordinates{deg_to_rad(location.lon()),
169  deg_to_rad(location.lat())})};
170  if (m_crs_user.is_latlong()) {
171  c.x = rad_to_deg(c.x);
172  c.y = rad_to_deg(c.y);
173  }
174 
175  return c;
176  }
177 
178  int epsg() const noexcept {
179  return m_epsg;
180  }
181 
182  std::string proj_string() const {
183  return m_proj_string;
184  }
185 
186  }; // class Projection
187 
188  } // namespace geom
189 
190 } // namespace osmium
191 
192 #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:371
std::string proj_string() const
Definition: projection.hpp:182
bool is_latlong() const noexcept
Definition: projection.hpp:96
Definition: projection.hpp:64
double lat() const
Definition: location.hpp:390
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:154
CRS(const std::string &crs)
Definition: projection.hpp:81
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:114
Namespace for everything in the Osmium library.
Definition: assembler.hpp:63
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:136
Definition: coordinates.hpp:48
CRS m_crs_user
Definition: projection.hpp:138
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:70
CRS(const char *crs)
Definition: projection.hpp:74
Definition: projection.hpp:133
Definition: location.hpp:266
int m_epsg
Definition: projection.hpp:135
void operator()(void *crs)
Definition: projection.hpp:65
Definition: projection.hpp:62
Projection(const std::string &proj_string)
Definition: projection.hpp:142
Projection(const char *proj_string)
Definition: projection.hpp:148
int epsg() const noexcept
Definition: projection.hpp:178
double x
Definition: coordinates.hpp:50
Definition: util.hpp:45
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:160
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67