Libosmium  2.2.0
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-2015 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  projPJ get() const {
72  return m_crs.get();
73  }
74 
75  public:
76 
77  CRS(const std::string& crs) :
78  m_crs(pj_init_plus(crs.c_str()), ProjCRSDeleter()) {
79  if (!m_crs) {
80  throw osmium::projection_error(std::string("creation of CRS failed: ") + pj_strerrno(*pj_get_errno_ref()));
81  }
82  }
83 
84  CRS(int epsg) :
85  CRS(std::string("+init=epsg:") + std::to_string(epsg)) {
86  }
87 
88  bool is_latlong() const {
89  return pj_is_latlong(m_crs.get()) != 0;
90  }
91 
92  bool is_geocent() const {
93  return pj_is_geocent(m_crs.get()) != 0;
94  }
95 
104  friend Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
105  int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
106  if (result != 0) {
107  throw osmium::projection_error(std::string("projection failed: ") + pj_strerrno(result));
108  }
109  return c;
110  }
111 
112  }; // class CRS
113 
118  class Projection {
119 
120  int m_epsg;
121  std::string m_proj_string;
122  CRS m_crs_wgs84 {4326};
124 
125  public:
126 
127  Projection(const std::string& proj_string) :
128  m_epsg(-1),
129  m_proj_string(proj_string),
130  m_crs_user(proj_string) {
131  }
132 
134  m_epsg(epsg),
135  m_proj_string(std::string("+init=epsg:") + std::to_string(epsg)),
136  m_crs_user(epsg) {
137  }
138 
140  Coordinates c {location.lon(), location.lat()};
141 
142  if (m_epsg != 4326) {
143  c = transform(m_crs_wgs84, m_crs_user, Coordinates(deg_to_rad(location.lon()), deg_to_rad(location.lat())));
144  if (m_crs_user.is_latlong()) {
145  c.x = rad_to_deg(c.x);
146  c.y = rad_to_deg(c.y);
147  }
148  }
149 
150  return c;
151  }
152 
153  int epsg() const noexcept {
154  return m_epsg;
155  }
156 
157  std::string proj_string() const {
158  return m_proj_string;
159  }
160 
161  }; // class Projection
162 
163  } // namespace geom
164 
165 } // namespace osmium
166 
167 #endif // OSMIUM_GEOM_PROJECTION_HPP
double y
Definition: coordinates.hpp:50
Definition: projection.hpp:63
CRS m_crs_wgs84
Definition: projection.hpp:122
Definition: reader_iterator.hpp:39
CRS(int epsg)
Definition: projection.hpp:84
bool is_latlong() const
Definition: projection.hpp:88
Projection(int epsg)
Definition: projection.hpp:133
CRS(const std::string &crs)
Definition: projection.hpp:77
std::string proj_string() const
Definition: projection.hpp:157
double lat() const
Definition: location.hpp:205
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
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:121
Definition: coordinates.hpp:47
CRS m_crs_user
Definition: projection.hpp:123
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:69
projPJ get() const
Definition: projection.hpp:71
Definition: projection.hpp:118
friend Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:104
Definition: location.hpp:79
int m_epsg
Definition: projection.hpp:120
void operator()(void *crs)
Definition: projection.hpp:64
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:139
double lon() const
Definition: location.hpp:186
Definition: projection.hpp:61
Projection(const std::string &proj_string)
Definition: projection.hpp:127
int epsg() const noexcept
Definition: projection.hpp:153
double x
Definition: coordinates.hpp:49
bool is_geocent() const
Definition: projection.hpp:92
Definition: util.hpp:45
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:67