Libosmium  2.20.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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
47#include <osmium/geom/util.hpp>
50
51#ifdef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
52# include <proj_api.h>
53#else
54# define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
55# include <proj_api.h>
56# undef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
57#endif
58
59#include <memory>
60#include <string>
61
62namespace osmium {
63
64 namespace geom {
65
72
74 void operator()(void* crs) {
75 pj_free(crs);
76 }
77 }; // struct ProjCRSDeleter
78
79 std::unique_ptr<void, ProjCRSDeleter> m_crs;
80
81 public:
82
83 explicit CRS(const char* crs) :
84 m_crs(pj_init_plus(crs), ProjCRSDeleter()) {
85 if (!m_crs) {
86 throw osmium::projection_error{std::string{"creation of CRS failed: "} + pj_strerrno(*pj_get_errno_ref())};
87 }
88 }
89
90 explicit CRS(const std::string& crs) :
91 CRS(crs.c_str()) {
92 }
93
94 explicit CRS(int epsg) :
95 CRS(std::string{"+init=epsg:"} + std::to_string(epsg)) {
96 }
97
101 projPJ get() const noexcept {
102 return m_crs.get();
103 }
104
105 bool is_latlong() const noexcept {
106 return pj_is_latlong(m_crs.get()) != 0;
107 }
108
109 bool is_geocent() const noexcept {
110 return pj_is_geocent(m_crs.get()) != 0;
111 }
112
113 }; // class CRS
114
125 inline OSMIUM_DEPRECATED Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
126 const int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
127 if (result != 0) {
128 throw osmium::projection_error{std::string{"projection failed: "} + pj_strerrno(result)};
129 }
130 return c;
131 }
132
147
149 std::string m_proj_string;
150 CRS m_crs_wgs84{4326};
152
153 public:
154
155 explicit Projection(std::string proj_string) :
156 m_epsg(-1),
157 m_proj_string(std::move(proj_string)),
158 m_crs_user(proj_string) {
159 }
160
161 explicit Projection(const char* proj_string) :
162 m_epsg(-1),
163 m_proj_string(proj_string),
164 m_crs_user(proj_string) {
165 }
166
167 explicit Projection(int epsg) :
168 m_epsg(epsg),
169 m_proj_string(std::string{"+init=epsg:"} + std::to_string(epsg)),
170 m_crs_user(epsg) {
171 }
172
180 if (m_epsg == 4326) {
181 return Coordinates{location.lon(), location.lat()};
182 }
183
184 if (m_epsg == 3857) {
185 return Coordinates{detail::lon_to_x(location.lon()),
186 detail::lat_to_y(location.lat())};
187 }
188
189 Coordinates c{transform(m_crs_wgs84, m_crs_user, Coordinates{deg_to_rad(location.lon()),
190 deg_to_rad(location.lat())})};
191 if (m_crs_user.is_latlong()) {
192 c.x = rad_to_deg(c.x);
193 c.y = rad_to_deg(c.y);
194 }
195
196 return c;
197 }
198
199 int epsg() const noexcept {
200 return m_epsg;
201 }
202
203 std::string proj_string() const {
204 return m_proj_string;
205 }
206
207 }; // class Projection
208
209 } // namespace geom
210
211} // namespace osmium
212
213#endif // OSMIUM_GEOM_PROJECTION_HPP
Definition: location.hpp:271
double lon() const
Definition: location.hpp:400
double lat() const
Definition: location.hpp:419
Definition: projection.hpp:71
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition: projection.hpp:79
CRS(const std::string &crs)
Definition: projection.hpp:90
CRS(int epsg)
Definition: projection.hpp:94
projPJ get() const noexcept
Definition: projection.hpp:101
bool is_latlong() const noexcept
Definition: projection.hpp:105
CRS(const char *crs)
Definition: projection.hpp:83
bool is_geocent() const noexcept
Definition: projection.hpp:109
Definition: projection.hpp:146
int epsg() const noexcept
Definition: projection.hpp:199
Coordinates operator()(osmium::Location location) const
Definition: projection.hpp:179
Projection(const char *proj_string)
Definition: projection.hpp:161
Projection(int epsg)
Definition: projection.hpp:167
std::string m_proj_string
Definition: projection.hpp:149
Projection(std::string proj_string)
Definition: projection.hpp:155
std::string proj_string() const
Definition: projection.hpp:203
int m_epsg
Definition: projection.hpp:148
CRS m_crs_user
Definition: projection.hpp:151
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:42
OSMIUM_DEPRECATED Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:125
constexpr double deg_to_rad(double degree) noexcept
Convert angle from degrees to radians.
Definition: util.hpp:64
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition: util.hpp:69
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555
Definition: projection.hpp:73
void operator()(void *crs)
Definition: projection.hpp:74
Definition: coordinates.hpp:48
double y
Definition: coordinates.hpp:51
double x
Definition: coordinates.hpp:50
Definition: util.hpp:47