MeshKernel
ProjectionConversions.hpp
1 //---- GPL ---------------------------------------------------------------------
2 //
3 // Copyright (C) Stichting Deltares, 2011-2023.
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation version 3.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // contact: delft3d.support@deltares.nl
18 // Stichting Deltares
19 // P.O. Box 177
20 // 2600 MH Delft, The Netherlands
21 //
22 // All indications and logos of, and references to, "Delft3D" and "Deltares"
23 // are registered trademarks of Stichting Deltares, and remain the property of
24 // Stichting Deltares. All rights reserved.
25 //
26 //------------------------------------------------------------------------------
27 
28 #pragma once
29 
30 #include <string>
31 
32 #if defined(__linux__) || defined(__APPLE__)
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
35 #endif
36 
37 #define BOOST_ALLOW_DEPRECATED_HEADERS
38 #include <boost/geometry.hpp>
39 #include <boost/geometry/srs/epsg.hpp>
40 #undef BOOST_ALLOW_DEPRECATED_HEADERS
41 
42 #if defined(__linux__) || defined(__APPLE__)
43 #pragma GCC diagnostic pop
44 #endif
45 
46 namespace meshkernel
47 {
48 
50  namespace bg = boost::geometry;
51 
53  template <typename ProjectionConversion>
55  {
56  public:
58  using LongLat = bg::model::d2::point_xy<double, bg::cs::geographic<bg::degree>>;
59 
61  using UTM = bg::model::d2::point_xy<double, Projection>;
62 
64  explicit ConvertSphericalToCartesianBase(const ProjectionConversion& proj) : m_projection(proj) {}
65 
67  virtual ~ConvertSphericalToCartesianBase() = default;
68 
71  {
72  return Projection::spherical;
73  }
74 
77  {
78  return Projection::cartesian;
79  }
80 
82  Point operator()(const Point& pnt) const
83  {
84  LongLat longLat(pnt.x, pnt.y);
85  UTM utm{0.0, 0.0};
86  m_projection.forward(longLat, utm);
87 
88  Point result(utm.x(), utm.y());
89  return result;
90  }
91 
92  private:
94  ProjectionConversion m_projection;
95  };
96 
98  template <const int EpsgCode>
99  class ConvertSphericalToCartesianEPSG : public ConvertSphericalToCartesianBase<bg::srs::projection<bg::srs::static_epsg<EpsgCode>>>
100  {
101  public:
103  using EpsgProjection = bg::srs::projection<bg::srs::static_epsg<EpsgCode>>;
104 
106  ConvertSphericalToCartesianEPSG() : ConvertSphericalToCartesianBase<bg::srs::projection<bg::srs::static_epsg<EpsgCode>>>(EpsgProjection()) {}
107  };
108 
110  class ConvertSphericalToCartesian : public ConvertSphericalToCartesianBase<bg::srs::projection<>>
111  {
112  public:
114  explicit ConvertSphericalToCartesian(const std::string& zone) : ConvertSphericalToCartesianBase<bg::srs::projection<>>(bg::srs::proj4(zone)) {}
115  };
116 
117  //--------------------------------
118 
120  template <typename ProjectionConversion>
122  {
123  public:
125  using LongLat = bg::model::d2::point_xy<double, bg::cs::geographic<bg::degree>>;
126 
128  using UTM = bg::model::d2::point_xy<double>;
129 
131  explicit ConvertCartesianToSphericalBase(const ProjectionConversion& proj) : m_projection(proj) {}
132 
134  virtual ~ConvertCartesianToSphericalBase() = default;
135 
138  {
139  return Projection::cartesian;
140  }
141 
144  {
145  return Projection::spherical;
146  }
147 
149  Point operator()(const Point& pnt) const
150  {
151  UTM utm{pnt.x, pnt.y};
152  LongLat longLat{0.0, 0.0};
153  m_projection.inverse(utm, longLat);
154 
155  Point result(longLat.x(), longLat.y());
156  return result;
157  }
158 
159  private:
161  ProjectionConversion m_projection;
162  };
163 
165  template <const int EpsgCode>
166  class ConvertCartesianToSphericalEPSG : public ConvertCartesianToSphericalBase<boost::geometry::srs::projection<boost::geometry::srs::static_epsg<EpsgCode>>>
167  {
168  public:
170  using EpsgProjection = boost::geometry::srs::projection<boost::geometry::srs::static_epsg<EpsgCode>>;
171 
173  ConvertCartesianToSphericalEPSG() : ConvertCartesianToSphericalBase<boost::geometry::srs::projection<boost::geometry::srs::static_epsg<EpsgCode>>>(EpsgProjection()) {}
174  };
175 
177  class ConvertCartesianToSpherical : public ConvertCartesianToSphericalBase<bg::srs::projection<>>
178  {
179  public:
181  explicit ConvertCartesianToSpherical(const std::string& zone) : ConvertCartesianToSphericalBase<bg::srs::projection<>>(bg::srs::proj4(zone)) {}
182  };
183 
184 } // namespace meshkernel
meshkernel::Projection
Projection
Enumerator describing the supported projections.
Definition: Definitions.hpp:41
meshkernel::ConvertSphericalToCartesian
Converts points from spherical to Cartesian coordinate system.
Definition: ProjectionConversions.hpp:110
meshkernel::ConvertCartesianToSphericalBase::SourceProjection
Projection SourceProjection() const
The coordinate system of the point parameter to the conversion operation.
Definition: ProjectionConversions.hpp:137
meshkernel::ConvertSphericalToCartesianEPSG
Converts points from spherical to Cartesian coordinate system using an ESPG code.
Definition: ProjectionConversions.hpp:99
meshkernel::ConvertSphericalToCartesianBase::ConvertSphericalToCartesianBase
ConvertSphericalToCartesianBase(const ProjectionConversion &proj)
Constructor with projection.
Definition: ProjectionConversions.hpp:64
meshkernel::ConvertSphericalToCartesianBase::TargetProjection
Projection TargetProjection() const
The coordinate system of the point result of the conversion operation.
Definition: ProjectionConversions.hpp:76
meshkernel::ConvertCartesianToSphericalBase< bg::srs::projection<> >::UTM
bg::model::d2::point_xy< double > UTM
Point in x-y space.
Definition: ProjectionConversions.hpp:128
meshkernel::ConvertCartesianToSphericalBase::~ConvertCartesianToSphericalBase
virtual ~ConvertCartesianToSphericalBase()=default
Default destructor.
meshkernel::Point
A struct describing a point in a two-dimensional space.
Definition: Point.hpp:40
meshkernel::ConvertCartesianToSphericalEPSG::EpsgProjection
boost::geometry::srs::projection< boost::geometry::srs::static_epsg< EpsgCode > > EpsgProjection
The EPSG projection.
Definition: ProjectionConversions.hpp:170
meshkernel::ConvertCartesianToSphericalBase::TargetProjection
Projection TargetProjection() const
The coordinate system of the point result of the conversion operation.
Definition: ProjectionConversions.hpp:143
meshkernel::Point::x
double x
X-coordinate.
Definition: Point.hpp:43
meshkernel::ConvertCartesianToSpherical
Converts points from spherical to Cartesian coordinate system.
Definition: ProjectionConversions.hpp:177
meshkernel::Point::y
double y
Y-coordinate.
Definition: Point.hpp:44
meshkernel::ConvertSphericalToCartesianEPSG::ConvertSphericalToCartesianEPSG
ConvertSphericalToCartesianEPSG()
Construct spherical to Cartesian with an EPSG code.
Definition: ProjectionConversions.hpp:106
meshkernel::ConvertCartesianToSphericalBase::ConvertCartesianToSphericalBase
ConvertCartesianToSphericalBase(const ProjectionConversion &proj)
Constructor with projection.
Definition: ProjectionConversions.hpp:131
meshkernel::ConvertSphericalToCartesianBase
Namespace alias for boost::geometry.
Definition: ProjectionConversions.hpp:54
meshkernel::ConvertSphericalToCartesian::ConvertSphericalToCartesian
ConvertSphericalToCartesian(const std::string &zone)
Construct spherical to Cartesian with an zone string.
Definition: ProjectionConversions.hpp:114
meshkernel::ConvertCartesianToSphericalBase
Converts points from spherical to Cartesian coordinate system.
Definition: ProjectionConversions.hpp:121
meshkernel::ConvertCartesianToSphericalEPSG::ConvertCartesianToSphericalEPSG
ConvertCartesianToSphericalEPSG()
Construct spherical to Cartesian with an EPSG code.
Definition: ProjectionConversions.hpp:173
meshkernel
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
meshkernel::ConvertSphericalToCartesianBase::~ConvertSphericalToCartesianBase
virtual ~ConvertSphericalToCartesianBase()=default
Default destructor.
meshkernel::ConvertSphericalToCartesianBase< bg::srs::projection<> >::UTM
bg::model::d2::point_xy< double, Projection > UTM
Point in x-y space.
Definition: ProjectionConversions.hpp:61
meshkernel::ConvertCartesianToSpherical::ConvertCartesianToSpherical
ConvertCartesianToSpherical(const std::string &zone)
Construct spherical to Cartesian with an zone string.
Definition: ProjectionConversions.hpp:181
meshkernel::ConvertCartesianToSphericalBase::operator()
Point operator()(const Point &pnt) const
Apply the conversion of a point in Cartesian coordinate system to spherical.
Definition: ProjectionConversions.hpp:149
meshkernel::ConvertSphericalToCartesianBase::SourceProjection
Projection SourceProjection() const
The coordinate system of the point parameter to the conversion operation.
Definition: ProjectionConversions.hpp:70
meshkernel::ConvertSphericalToCartesianBase< bg::srs::projection<> >::LongLat
bg::model::d2::point_xy< double, bg::cs::geographic< bg::degree > > LongLat
point in longitude-latitude space
Definition: ProjectionConversions.hpp:58
meshkernel::ConvertSphericalToCartesianEPSG::EpsgProjection
bg::srs::projection< bg::srs::static_epsg< EpsgCode > > EpsgProjection
The EPSG projection.
Definition: ProjectionConversions.hpp:103
meshkernel::ConvertSphericalToCartesianBase::operator()
Point operator()(const Point &pnt) const
Apply the conversion of a point in Spherical coordinate system to Cartesian.
Definition: ProjectionConversions.hpp:82
meshkernel::ConvertCartesianToSphericalBase< bg::srs::projection<> >::LongLat
bg::model::d2::point_xy< double, bg::cs::geographic< bg::degree > > LongLat
point in longitude-latitude space
Definition: ProjectionConversions.hpp:125
meshkernel::ConvertCartesianToSphericalEPSG
Converts points from spherical to Cartesian coordinate system using an EPSG code.
Definition: ProjectionConversions.hpp:166