Libosmium  2.7.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-2016 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 
51 #include <osmium/geom/util.hpp>
52 #include <osmium/osm/location.hpp>
53 
54 namespace osmium {
55 
56  namespace geom {
57 
61  class CRS {
62 
63  struct ProjCRSDeleter {
64  void operator()(void* crs) {
65  pj_free(crs);
66  }
67  }; // struct ProjCRSDeleter
68 
69  std::unique_ptr<void, ProjCRSDeleter> m_crs;
70 
71  public:
72 
73  CRS(const std::string& crs) :
74  m_crs(pj_init_plus(crs.c_str()), ProjCRSDeleter()) {
75  if (!m_crs) {
76  throw osmium::projection_error(std::string("creation of CRS failed: ") + pj_strerrno(*pj_get_errno_ref()));
77  }
78  }
79 
80  CRS(int epsg) :
81  CRS(std::string("+init=epsg:") + std::to_string(epsg)) {
82  }
83 
87  projPJ get() const {
88  return m_crs.get();
89  }
90 
91  bool is_latlong() const {
92  return pj_is_latlong(m_crs.get()) != 0;
93  }
94 
95  bool is_geocent() const {
96  return pj_is_geocent(m_crs.get()) != 0;
97  }
98 
99  }; // class CRS
100 
109  inline Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
110  int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
111  if (result != 0) {
112  throw osmium::projection_error(std::string("projection failed: ") + pj_strerrno(result));
113  }
114  return c;
115  }
116 
121  class Projection {
122 
123  int m_epsg;
124  std::string m_proj_string;
125  CRS m_crs_wgs84 {4326};
127 
128  public:
129 
130  Projection(const std::string& proj_string) :
131  m_epsg(-1),
132  m_proj_string(proj_string),
133  m_crs_user(proj_string) {
134  }
135 
136  Projection(int epsg) :
137  m_epsg(epsg),
138  m_proj_string(std::string("+init=epsg:") + std::to_string(epsg)),
139  m_crs_user(epsg) {
140  }
141 
143  Coordinates c {location.lon(), location.lat()};
144 
145  if (m_epsg != 4326) {
146  c = transform(m_crs_wgs84, m_crs_user, Coordinates(deg_to_rad(location.lon()), deg_to_rad(location.lat())));
147  if (m_crs_user.is_latlong()) {
148  c.x = rad_to_deg(c.x);
149  c.y = rad_to_deg(c.y);
150  }
151  }
152 
153  return c;
154  }
155 
156  int epsg() const noexcept {
157  return m_epsg;
158  }
159 
160  std::string proj_string() const {
161  return m_proj_string;
162  }
163 
164  }; // class Projection
165 
166  } // namespace geom
167 
168 } // namespace osmium
169 
170 #endif // OSMIUM_GEOM_PROJECTION_HPP
double y
Definition: coordinates.hpp:49
Definition: projection.hpp:63
Definition: reader_iterator.hpp:39
CRS(int epsg)
Definition: projection.hpp:80
bool is_latlong() const
Definition: projection.hpp:91
Projection(int epsg)
Definition: projection.hpp:136
CRS(const std::string &crs)
Definition: projection.hpp:73
std::string proj_string() const
Definition: projection.hpp:160
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:109
double lat() const
Definition: location.hpp:206
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
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:124
Definition: coordinates.hpp:46
CRS m_crs_user
Definition: projection.hpp:126
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:69
projPJ get() const
Definition: projection.hpp:87
Definition: projection.hpp:121
Definition: location.hpp:80
int m_epsg
Definition: projection.hpp:123
void operator()(void *crs)
Definition: projection.hpp:64
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:142
double lon() const
Definition: location.hpp:187
Definition: projection.hpp:61
Projection(const std::string &proj_string)
Definition: projection.hpp:130
int epsg() const noexcept
Definition: projection.hpp:156
double x
Definition: coordinates.hpp:48
bool is_geocent() const
Definition: projection.hpp:95
Definition: util.hpp:45
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67