Loading...
Searching...
No Matches
projection.hpp
Go to the documentation of this file.
#ifndef OSMIUM_GEOM_PROJECTION_HPP
#define OSMIUM_GEOM_PROJECTION_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/geom/coordinates.hpp>
#include <osmium/geom/mercator_projection.hpp>
#include <osmium/geom/util.hpp>
#include <osmium/osm/location.hpp>
#include <osmium/util/compatibility.hpp>
#ifdef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
# include <proj_api.h>
#else
# define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
# include <proj_api.h>
# undef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
#endif
#include <memory>
#include <string>
namespace osmium {
namespace geom {
class OSMIUM_DEPRECATED CRS {
struct ProjCRSDeleter {
void operator()(void* crs) {
pj_free(crs);
}
}; // struct ProjCRSDeleter
std::unique_ptr<void, ProjCRSDeleter> m_crs;
public:
explicit CRS(const char* crs) :
m_crs(pj_init_plus(crs), ProjCRSDeleter()) {
if (!m_crs) {
throw osmium::projection_error{std::string{"creation of CRS failed: "} + pj_strerrno(*pj_get_errno_ref())};
}
}
explicit CRS(const std::string& crs) :
CRS(crs.c_str()) {
}
explicit CRS(int epsg) :
CRS(std::string{"+init=epsg:"} + std::to_string(epsg)) {
}
projPJ get() const noexcept {
return m_crs.get();
}
bool is_latlong() const noexcept {
return pj_is_latlong(m_crs.get()) != 0;
}
bool is_geocent() const noexcept {
return pj_is_geocent(m_crs.get()) != 0;
}
}; // class CRS
inline OSMIUM_DEPRECATED Coordinates transform(const CRS& src, const CRS& dest, Coordinates c) {
const int result = pj_transform(src.get(), dest.get(), 1, 1, &c.x, &c.y, nullptr);
if (result != 0) {
throw osmium::projection_error{std::string{"projection failed: "} + pj_strerrno(result)};
}
return c;
}
class OSMIUM_DEPRECATED Projection {
int m_epsg;
std::string m_proj_string;
CRS m_crs_wgs84{4326};
CRS m_crs_user;
public:
explicit Projection(std::string proj_string) :
m_epsg(-1),
m_proj_string(std::move(proj_string)),
m_crs_user(proj_string) {
}
explicit Projection(const char* proj_string) :
m_epsg(-1),
m_proj_string(proj_string),
m_crs_user(proj_string) {
}
explicit Projection(int epsg) :
m_epsg(epsg),
m_proj_string(std::string{"+init=epsg:"} + std::to_string(epsg)),
m_crs_user(epsg) {
}
Coordinates operator()(osmium::Location location) const {
if (m_epsg == 4326) {
return Coordinates{location.lon(), location.lat()};
}
if (m_epsg == 3857) {
return Coordinates{detail::lon_to_x(location.lon()),
detail::lat_to_y(location.lat())};
}
Coordinates c{transform(m_crs_wgs84, m_crs_user, Coordinates{deg_to_rad(location.lon()),
deg_to_rad(location.lat())})};
if (m_crs_user.is_latlong()) {
c.x = rad_to_deg(c.x);
c.y = rad_to_deg(c.y);
}
return c;
}
int epsg() const noexcept {
return m_epsg;
}
std::string proj_string() const {
return m_proj_string;
}
}; // class Projection
} // namespace geom
} // namespace osmium
#endif // OSMIUM_GEOM_PROJECTION_HPP
osmium::Location
Definition location.hpp:271
osmium::Location::lon
double lon() const
Definition location.hpp:400
osmium::Location::lat
double lat() const
Definition location.hpp:419
osmium::geom::CRS
Definition projection.hpp:71
osmium::geom::CRS::m_crs
std::unique_ptr< void, ProjCRSDeleter > m_crs
Definition projection.hpp:79
osmium::geom::CRS::CRS
CRS(const std::string &crs)
Definition projection.hpp:90
osmium::geom::CRS::CRS
CRS(int epsg)
Definition projection.hpp:94
osmium::geom::CRS::get
projPJ get() const noexcept
Definition projection.hpp:101
osmium::geom::CRS::is_latlong
bool is_latlong() const noexcept
Definition projection.hpp:105
osmium::geom::CRS::CRS
CRS(const char *crs)
Definition projection.hpp:83
osmium::geom::CRS::is_geocent
bool is_geocent() const noexcept
Definition projection.hpp:109
osmium::geom::Projection
Definition projection.hpp:146
osmium::geom::Projection::epsg
int epsg() const noexcept
Definition projection.hpp:199
osmium::geom::Projection::operator()
Coordinates operator()(osmium::Location location) const
Definition projection.hpp:179
osmium::geom::Projection::Projection
Projection(const char *proj_string)
Definition projection.hpp:161
osmium::geom::Projection::Projection
Projection(int epsg)
Definition projection.hpp:167
osmium::geom::Projection::m_proj_string
std::string m_proj_string
Definition projection.hpp:149
osmium::geom::Projection::Projection
Projection(std::string proj_string)
Definition projection.hpp:155
osmium::geom::Projection::proj_string
std::string proj_string() const
Definition projection.hpp:203
osmium::geom::Projection::m_epsg
int m_epsg
Definition projection.hpp:148
osmium::geom::Projection::m_crs_user
CRS m_crs_user
Definition projection.hpp:151
compatibility.hpp
OSMIUM_DEPRECATED
#define OSMIUM_DEPRECATED
Definition compatibility.hpp:42
coordinates.hpp
util.hpp
location.hpp
mercator_projection.hpp
osmium::geom::transform
OSMIUM_DEPRECATED Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition projection.hpp:125
osmium::geom::deg_to_rad
constexpr double deg_to_rad(double degree) noexcept
Convert angle from degrees to radians.
Definition util.hpp:64
osmium::geom::rad_to_deg
constexpr double rad_to_deg(double radians) noexcept
Convert angle from radians to degrees.
Definition util.hpp:69
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555
osmium::geom::CRS::ProjCRSDeleter
Definition projection.hpp:73
osmium::geom::CRS::ProjCRSDeleter::operator()
void operator()(void *crs)
Definition projection.hpp:74
osmium::geom::Coordinates
Definition coordinates.hpp:48
osmium::geom::Coordinates::y
double y
Definition coordinates.hpp:51
osmium::geom::Coordinates::x
double x
Definition coordinates.hpp:50
osmium::projection_error
Definition util.hpp:47