Debian Codegraph @

src/atlas/projection/detail/MercatorProjection.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include <cmath>
12 #include <functional>
13 #include <limits>
14 #include <sstream>
15
16 #include "eckit/config/Parametrisation.h"
17 #include "eckit/utils/Hash.h"
18
19 #include "atlas/projection/detail/MercatorProjection.h"
20 #include "atlas/projection/detail/ProjectionFactory.h"
21 #include "atlas/runtime/Exception.h"
22 #include "atlas/util/Config.h"
23 #include "atlas/util/Constants.h"
24 #include "atlas/util/CoordinateEnums.h"
25 #include "atlas/util/Earth.h"
26 #include "atlas/util/NormaliseLongitude.h"
27
28 /*
29 Projection formula's for Mercator projection from "Map Projections: A Working
30 Manual"
31
32 The origin of the xy-system is at (lon0,0)
33
34 */
35
36 namespace {
37 static constexpr double D2R(const double x) {
38 return atlas::util::Constants::degreesToRadians() * x;
39 }
40 static constexpr double R2D(const double x) {
41 return atlas::util::Constants::radiansToDegrees() * x;
42 }
43 } // namespace
44
45 namespace atlas {
46 namespace projection {
47 namespace detail {
48
49 // constructors
50 template <typename Rotation>
51 MercatorProjectionT<Rotation>::MercatorProjectionT(const eckit::Parametrisation& params):
52 ProjectionImpl(), normalise_(params), rotation_(params) {
53 bool radius_provided = params.get("radius", radius_ = util::Earth::radius());
54 k_radius_ = radius_;
55
56 params.get("longitude0", lon0_ = 0.0);
57 normalise_mercator_ = util::NormaliseLongitude(lon0_ - 180., lon0_ + 180.);
58
59 if (params.get("latitude1", lat1_ = 0.0)) {
60 k_radius_ *= std::cos(D2R(lat1_));
61 }
62
63 params.get("false_northing", false_northing_ = 0.);
64 params.get("false_easting", false_easting_ = 0.);
65
66 eccentricity_ = 0.;
67 auto squared = [](double x) { return x * x; };
68 if (params.get("semi_major_axis", semi_major_axis_ = radius_) &&
69 params.get("semi_minor_axis", semi_minor_axis_ = radius_)) {
70 ATLAS_ASSERT(
71 not radius_provided,
72 "Ambiguous parameters provided to MercatorProjection: {radius} and {semi_major_axis,semi_minor_axis}");
73 eccentricity_ = std::sqrt(1. - squared(semi_minor_axis_ / semi_major_axis_));
74 }
75
76 if (eccentricity_ != 0.) {
77 k_radius_ /= std::sqrt(1. - squared(eccentricity_ * std::sin(D2R(lat1_))));
78 }
79
80 inv_k_radius_ = 1. / k_radius_;
81 }
82
83 template <typename Rotation>
84 void MercatorProjectionT<Rotation>::lonlat2xy(double crd[]) const {
85 auto t = [&](double& lat) -> double {
86 double sinlat = std::sin(D2R(lat));
87 double t = (1. + sinlat) / (1. - sinlat);
88 if (eccentricity_ > 0) { // --> ellipsoidal correction
89 double e = eccentricity_;
90 double e_sinlat = e * sinlat;
91 t *= std::pow((1. - e_sinlat) / (1. + e_sinlat), e);
92 }
93 return t;
94 };
95
96 // first unrotate
97 rotation_.unrotate(crd);
98
99 // then project
100 if (crd[LAT] >= 90. - 1e-3) {
101 crd[XX] = false_easting_;
102 crd[YY] = std::numeric_limits<double>::infinity();
103 }
104 else if (crd[LAT] <= -90. + 1e-3) {
105 crd[XX] = false_easting_;
106 crd[YY] = -std::numeric_limits<double>::infinity();
107 }
108 else {
109 crd[XX] = false_easting_ + k_radius_ * (D2R(normalise_mercator_(crd[LON]) - lon0_));
110 crd[YY] = false_northing_ + k_radius_ * 0.5 * std::log(t(crd[LAT]));
111 }
112 }
113
114 template <typename Rotation>
115 void MercatorProjectionT<Rotation>::xy2lonlat(double crd[]) const {
116 auto compute_lat = [&](double y) -> double {
117 // deepcode ignore FloatingPointEquals: We want exact comparison
118 if (eccentricity_ == 0.) {
119 return 90. - 2. * R2D(std::atan(std::exp(-y * inv_k_radius_)));
120 }
121 else { // eccentricity > 0 --> ellipsoidal correction
122 double e = eccentricity_;
123 /* Iterative procedure to compute the latitude for the inverse projection
124 * From the book "Map Projections-A Working Manual-John P. Snyder (1987)"
125 * Equation (7–9) involves rapidly converging iteration: Calculate t from (15-11)
126 * Then, assuming an initial trial phi equal to (pi/2 - 2*arctan t) in the right side of equation (7–9),
127 * calculate phi on the left side. Substitute the calculated phi into the right side,
128 * calculate a new phi, etc., until phi does not change significantly from the preceding trial value of phi
129 */
130 constexpr double EPSILON = 1.e-12;
131 constexpr int MAX_ITER = 15;
132
133 const double t = std::exp(-y * inv_k_radius_);
134 const double e_half = 0.5 * e;
135
136 double lat = 90. - 2 * R2D(std::atan(t));
137 for (int i = 0; i < MAX_ITER; ++i) {
138 double e_sinlat = e * std::sin(D2R(lat));
139 double dlat =
140 90. - 2. * R2D(std::atan(t * (std::pow(((1.0 - e_sinlat) / (1.0 + e_sinlat)), e_half)))) - lat;
141 lat += dlat;
142 if (std::abs(dlat) < EPSILON) {
143 return lat;
144 }
145 }
146 ATLAS_THROW_EXCEPTION("Convergence failed in computing latitude in MercatorProjection");
147 }
148 };
149
150 const double x = crd[XX] - false_easting_;
151 const double y = crd[YY] - false_northing_;
152
153 // first projection
154 crd[LON] = lon0_ + R2D(x * inv_k_radius_);
155 crd[LAT] = compute_lat(y);
156
157 // then rotate
158 rotation_.rotate(crd);
159
160 // then normalise
161 normalise_(crd);
162 }
163
164 template <typename Rotation>
165 ProjectionImpl::Jacobian MercatorProjectionT<Rotation>::jacobian(const PointLonLat&) const {
166 throw_NotImplemented("MercatorProjectionT::jacobian", Here());
167 }
168
169 // specification
170 template <typename Rotation>
171 typename MercatorProjectionT<Rotation>::Spec MercatorProjectionT<Rotation>::spec() const {
172 Spec proj;
173 proj.set("type", static_type());
174 proj.set("longitude0", lon0_);
175 proj.set("latitude1", lat1_);
176 proj.set("radius", radius_);
177 proj.set("false_easting", false_easting_);
178 proj.set("false_northing", false_northing_);
179 normalise_.spec(proj);
180 rotation_.spec(proj);
181 return proj;
182 }
183
184 template <typename Rotation>
185 void MercatorProjectionT<Rotation>::hash(eckit::Hash& hsh) const {
186 hsh.add(static_type());
187 rotation_.hash(hsh);
188 normalise_.hash(hsh);
189 hsh.add(lon0_);
190 hsh.add(lat1_);
191 hsh.add(radius_);
192 }
193
194 template class MercatorProjectionT<NotRotated>;
195 template class MercatorProjectionT<Rotated>;
196
197 namespace {
198 static ProjectionBuilder<MercatorProjection> register_1(MercatorProjection::static_type());
199 static ProjectionBuilder<RotatedMercatorProjection> register_2(RotatedMercatorProjection::static_type());
200 } // namespace
201
202 } // namespace detail
203 } // namespace projection
204 } // namespace atlas