1 /******************************************************************************
2 * Project: PROJ.4
3 * Purpose: Implementation of the aitoff (Aitoff) and wintri (Winkel Tripel)
4 * projections.
5 * Author: Gerald Evenden (1995)
6 * Drazen Tutic, Lovro Gradiser (2015) - add inverse
7 * Thomas Knudsen (2016) - revise/add regression tests
8 *
9 ******************************************************************************
10 * Copyright (c) 1995, Gerald Evenden
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 *****************************************************************************/
30
31 #define PJ_LIB__
32
33 #include <errno.h>
34 #include <math.h>
35
36 #include "proj.h"
37 #include "proj_internal.h"
38
39
40 namespace { // anonymous namespace
41 enum Mode {
42 AITOFF = 0,
43 WINKEL_TRIPEL = 1
44 };
45 } // anonymous namespace
46
47 namespace { // anonymous namespace
48 struct pj_opaque {
49 double cosphi1;
50 enum Mode mode;
51 };
52 } // anonymous namespace
53
54
55 PROJ_HEAD(aitoff, "Aitoff") "\n\tMisc Sph";
56 PROJ_HEAD(wintri, "Winkel Tripel") "\n\tMisc Sph\n\tlat_1";
57
58
59
60 #if 0
61 FORWARD(aitoff_s_forward); /* spheroid */
62 #endif
63
64
aitoff_s_forward(PJ_LP lp,PJ * P)65 static PJ_XY aitoff_s_forward (PJ_LP lp, PJ *P) { /* Spheroidal, forward */
66 PJ_XY xy = {0.0,0.0};
67 struct pj_opaque *Q = static_cast<struct pj_opaque*>(P->opaque);
68 double c, d;
69
70 c = 0.5 * lp.lam;
71 d = acos(cos(lp.phi) * cos(c));
72 if(d != 0.0) {/* basic Aitoff */
73 xy.x = 2. * d * cos(lp.phi) * sin(c) * (xy.y = 1. / sin(d));
74 xy.y *= d * sin(lp.phi);
75 } else
76 xy.x = xy.y = 0.;
77 if (Q->mode == WINKEL_TRIPEL) {
78 xy.x = (xy.x + lp.lam * Q->cosphi1) * 0.5;
79 xy.y = (xy.y + lp.phi) * 0.5;
80 }
81 return (xy);
82 }
83
84 /***********************************************************************************
85 *
86 * Inverse functions added by Drazen Tutic and Lovro Gradiser based on paper:
87 *
88 * I.Özbug Biklirici and Cengizhan Ipbüker. A General Algorithm for the Inverse
89 * Transformation of Map Projections Using Jacobian Matrices. In Proceedings of the
90 * Third International Symposium Mathematical & Computational Applications,
91 * pages 175{182, Turkey, September 2002.
92 *
93 * Expected accuracy is defined by EPSILON = 1e-12. Should be appropriate for
94 * most applications of Aitoff and Winkel Tripel projections.
95 *
96 * Longitudes of 180W and 180E can be mixed in solution obtained.
97 *
98 * Inverse for Aitoff projection in poles is undefined, longitude value of 0 is assumed.
99 *
100 * Contact : dtutic@geof.hr
101 * Date: 2015-02-16
102 *
103 ************************************************************************************/
104
aitoff_s_inverse(PJ_XY xy,PJ * P)105 static PJ_LP aitoff_s_inverse (PJ_XY xy, PJ *P) { /* Spheroidal, inverse */
106 PJ_LP lp = {0.0,0.0};
107 struct pj_opaque *Q = static_cast<struct pj_opaque*>(P->opaque);
108 int iter, MAXITER = 10, round = 0, MAXROUND = 20;
109 double EPSILON = 1e-12, D, C, f1, f2, f1p, f1l, f2p, f2l, dp, dl, sl, sp, cp, cl, x, y;
110
111 if ((fabs(xy.x) < EPSILON) && (fabs(xy.y) < EPSILON )) { lp.phi = 0.; lp.lam = 0.; return lp; }
112
113 /* initial values for Newton-Raphson method */
114 lp.phi = xy.y; lp.lam = xy.x;
115 do {
116 iter = 0;
117 do {
118 sl = sin(lp.lam * 0.5);
119 cl = cos(lp.lam * 0.5);
120 sp = sin(lp.phi);
121 cp = cos(lp.phi);
122 D = cp * cl;
123 C = 1. - D * D;
124 const double denom = pow(C, 1.5);
125 if( denom == 0 ) {
126 proj_errno_set(P, PJD_ERR_NON_CONVERGENT);
127 return lp;
128 }
129 D = acos(D) / denom;
130 f1 = 2. * D * C * cp * sl;
131 f2 = D * C * sp;
132 f1p = 2.* (sl * cl * sp * cp / C - D * sp * sl);
133 f1l = cp * cp * sl * sl / C + D * cp * cl * sp * sp;
134 f2p = sp * sp * cl / C + D * sl * sl * cp;
135 f2l = 0.5 * (sp * cp * sl / C - D * sp * cp * cp * sl * cl);
136 if (Q->mode == WINKEL_TRIPEL) {
137 f1 = 0.5 * (f1 + lp.lam * Q->cosphi1);
138 f2 = 0.5 * (f2 + lp.phi);
139 f1p *= 0.5;
140 f1l = 0.5 * (f1l + Q->cosphi1);
141 f2p = 0.5 * (f2p + 1.);
142 f2l *= 0.5;
143 }
144 f1 -= xy.x;
145 f2 -= xy.y;
146 dp = f1p * f2l - f2p * f1l;
147 dl = (f2 * f1p - f1 * f2p) / dp;
148 dp = (f1 * f2l - f2 * f1l) / dp;
149 dl = fmod(dl, M_PI); /* set to interval [-M_PI, M_PI] */
150 lp.phi -= dp;
151 lp.lam -= dl;
152 } while ((fabs(dp) > EPSILON || fabs(dl) > EPSILON) && (iter++ < MAXITER));
153 if (lp.phi > M_PI_2) lp.phi -= 2.*(lp.phi-M_PI_2); /* correct if symmetrical solution for Aitoff */
154 if (lp.phi < -M_PI_2) lp.phi -= 2.*(lp.phi+M_PI_2); /* correct if symmetrical solution for Aitoff */
155 if ((fabs(fabs(lp.phi) - M_PI_2) < EPSILON) && (Q->mode == AITOFF)) lp.lam = 0.; /* if pole in Aitoff, return longitude of 0 */
156
157 /* calculate x,y coordinates with solution obtained */
158 if((D = acos(cos(lp.phi) * cos(C = 0.5 * lp.lam))) != 0.0) {/* Aitoff */
159 y = 1. / sin(D);
160 x = 2. * D * cos(lp.phi) * sin(C) * y;
161 y *= D * sin(lp.phi);
162 } else
163 x = y = 0.;
164 if (Q->mode == WINKEL_TRIPEL) {
165 x = (x + lp.lam * Q->cosphi1) * 0.5;
166 y = (y + lp.phi) * 0.5;
167 }
168 /* if too far from given values of x,y, repeat with better approximation of phi,lam */
169 } while (((fabs(xy.x-x) > EPSILON) || (fabs(xy.y-y) > EPSILON)) && (round++ < MAXROUND));
170
171 if (iter == MAXITER && round == MAXROUND)
172 {
173 pj_ctx_set_errno( P->ctx, PJD_ERR_NON_CONVERGENT );
174 /* fprintf(stderr, "Warning: Accuracy of 1e-12 not reached. Last increments: dlat=%e and dlon=%e\n", dp, dl); */
175 }
176
177 return lp;
178 }
179
180
setup(PJ * P)181 static PJ *setup(PJ *P) {
182 P->inv = aitoff_s_inverse;
183 P->fwd = aitoff_s_forward;
184 P->es = 0.;
185 return P;
186 }
187
188
PROJECTION(aitoff)189 PJ *PROJECTION(aitoff) {
190 struct pj_opaque *Q = static_cast<struct pj_opaque*>(pj_calloc (1, sizeof (struct pj_opaque)));
191 if (nullptr==Q)
192 return pj_default_destructor(P, ENOMEM);
193 P->opaque = Q;
194
195 Q->mode = AITOFF;
196 return setup(P);
197 }
198
199
PROJECTION(wintri)200 PJ *PROJECTION(wintri) {
201 struct pj_opaque *Q = static_cast<struct pj_opaque*>(pj_calloc (1, sizeof (struct pj_opaque)));
202 if (nullptr==Q)
203 return pj_default_destructor(P, ENOMEM);
204 P->opaque = Q;
205
206 Q->mode = WINKEL_TRIPEL;
207 if (pj_param(P->ctx, P->params, "tlat_1").i) {
208 if ((Q->cosphi1 = cos(pj_param(P->ctx, P->params, "rlat_1").f)) == 0.)
209 return pj_default_destructor (P, PJD_ERR_LAT_LARGER_THAN_90);
210 }
211 else /* 50d28' or acos(2/pi) */
212 Q->cosphi1 = 0.636619772367581343;
213 return setup(P);
214 }
215