1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/******************************************************************************
* Project: PROJ
* Purpose: Implementing the S2 family of projections in PROJ
* Author: Marcus Elia, <marcus at geopi.pe>
*
******************************************************************************
* Copyright (c) 2021, Marcus Elia, <marcus at geopi.pe>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* 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 AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************
*
* This implements the S2 projection. This code is similar
* to the QSC projection.
*
*
* You have to choose one of the following projection centers,
* corresponding to the centers of the six cube faces:
* phi0 = 0.0, lam0 = 0.0 ("front" face)
* phi0 = 0.0, lam0 = 90.0 ("right" face)
* phi0 = 0.0, lam0 = 180.0 ("back" face)
* phi0 = 0.0, lam0 = -90.0 ("left" face)
* phi0 = 90.0 ("top" face)
* phi0 = -90.0 ("bottom" face)
* Other projection centers will not work!
*
* You must also choose which conversion from UV to ST coordinates
* is used (linear, quadratic, tangent). Read about them in
* https://github.com/google/s2geometry/blob/0c4c460bdfe696da303641771f9def900b3e440f/src/s2/s2coords.h
* The S2 projection functions are adapted from the above link and the S2
* Math util functions are adapted from
* https://github.com/google/s2geometry/blob/0c4c460bdfe696da303641771f9def900b3e440f/src/s2/util/math/vector.h
****************************************************************************/
#define PJ_LIB__
#define _USE_MATH_DEFINES // needed for M_1_PI availability with MSVC
#include <errno.h>
#include <cmath>
#include "proj.h"
#include "proj_internal.h"
/* The six cube faces. */
namespace { // anonymous namespace
enum Face {
FACE_FRONT = 0,
FACE_RIGHT = 1,
FACE_TOP = 2,
FACE_BACK = 3,
FACE_LEFT = 4,
FACE_BOTTOM = 5
};
} // anonymous namespace
enum S2ProjectionType {Linear, Quadratic, Tangent, NoUVtoST};
std::map<std::string, S2ProjectionType> stringToS2ProjectionType { {"linear", Linear}, {"quadratic", Quadratic}, {"tangent", Tangent}, {"none", NoUVtoST} };
namespace { // anonymous namespace
struct pj_opaque {
enum Face face;
double a_squared;
double one_minus_f;
double one_minus_f_squared;
S2ProjectionType UVtoST;
};
} // anonymous namespace
PROJ_HEAD(s2, "S2") "\n\tMisc, Sph&Ell";
#define EPS10 1.e-10
/* The four areas on a cube face. AREA_0 is the area of definition,
* the other three areas are counted counterclockwise. */
namespace { // anonymous namespace
enum Area {
AREA_0 = 0,
AREA_1 = 1,
AREA_2 = 2,
AREA_3 = 3
};
} // anonymous namespace
// =================================================
//
// S2 Math Util
//
// =================================================
static PJ_XYZ Abs(const PJ_XYZ& p) {
return {std::fabs(p.x), std::fabs(p.y), std::fabs(p.z)};
}
// return the index of the largest component (fabs)
static int LargestAbsComponent(const PJ_XYZ& p) {
PJ_XYZ temp = Abs(p);
return temp.x > temp.y ?
temp.x > temp.z ? 0 : 2 :
temp.y > temp.z ? 1 : 2;
}
// =================================================
//
// S2 Projection Functions
//
// =================================================
// Unfortunately, tan(M_PI_4) is slightly less than 1.0. This isn't due to
// a flaw in the implementation of tan(), it's because the derivative of
// tan(x) at x=pi/4 is 2, and it happens that the two adjacent floating
// point numbers on either side of the infinite-precision value of pi/4 have
// tangents that are slightly below and slightly above 1.0 when rounded to
// the nearest double-precision result.
static double STtoUV(double s, S2ProjectionType s2_projection) {
switch(s2_projection) {
case Linear:
return 2 * s - 1;
break;
case Quadratic:
if (s >= 0.5) return (1/3.) * (4*s*s - 1);
else return (1/3.) * (1 - 4*(1-s)*(1-s));
break;
case Tangent:
s = std::tan(M_PI_2 * s - M_PI_4);
return s + (1.0 / static_cast<double>(static_cast<std::int64_t>(1) << 53)) * s;
break;
default:
return s;
}
}
static double UVtoST(double u, S2ProjectionType s2_projection) {
switch(s2_projection) {
case Linear:
return 0.5 * (u + 1);
break;
case Quadratic:
if (u >= 0) return 0.5 * std::sqrt(1 + 3*u);
else return 1 - 0.5 * std::sqrt(1 - 3*u);
break;
case Tangent:
{
volatile double a = std::atan(u);
return (2 * M_1_PI) * (a + M_PI_4);
}
break;
default:
return u;
}
}
inline PJ_XYZ FaceUVtoXYZ(int face, double u, double v) {
switch (face) {
case 0: return { 1, u, v};
case 1: return {-u, 1, v};
case 2: return {-u, -v, 1};
case 3: return {-1, -v, -u};
case 4: return { v, -1, -u};
default: return { v, u, -1};
}
}
inline PJ_XYZ FaceUVtoXYZ(int face, const PJ_XY& uv) {
return FaceUVtoXYZ(face, uv.x, uv.y);
}
inline void ValidFaceXYZtoUV(int face, const PJ_XYZ& p,
double* pu, double* pv) {
switch (face) {
case 0: *pu = p.y / p.x; *pv = p.z / p.x; break;
case 1: *pu = -p.x / p.y; *pv = p.z / p.y; break;
case 2: *pu = -p.x / p.z; *pv = -p.y / p.z; break;
case 3: *pu = p.z / p.x; *pv = p.y / p.x; break;
case 4: *pu = p.z / p.y; *pv = -p.x / p.y; break;
default: *pu = -p.y / p.z; *pv = -p.x / p.z; break;
}
}
inline void ValidFaceXYZtoUV(int face, const PJ_XYZ& p, PJ_XY* puv) {
ValidFaceXYZtoUV(face, p, &(*puv).x, &(*puv).y);
}
inline int GetFace(const PJ_XYZ& p) {
int face = LargestAbsComponent(p);
double pFace;
switch (face) {
case 0: pFace = p.x; break;
case 1: pFace = p.y; break;
default: pFace = p.z; break;
}
if (pFace < 0) face += 3;
return face;
}
inline int XYZtoFaceUV(const PJ_XYZ& p, double* pu, double* pv) {
int face = GetFace(p);
ValidFaceXYZtoUV(face, p, pu, pv);
return face;
}
inline int XYZtoFaceUV(const PJ_XYZ& p, PJ_XY* puv) {
return XYZtoFaceUV(p, &(*puv).x, &(*puv).y);
}
inline bool FaceXYZtoUV(int face, const PJ_XYZ& p,
double* pu, double* pv) {
double pFace;
switch(face) {
case 0: pFace = p.x; break;
case 1: pFace = p.y; break;
case 2: pFace = p.z; break;
case 3: pFace = p.x; break;
case 4: pFace = p.y; break;
default: pFace = p.z; break;
}
if (face < 3) {
if (pFace <= 0) return false;
} else {
if (pFace >= 0) return false;
}
ValidFaceXYZtoUV(face, p, pu, pv);
return true;
}
inline bool FaceXYZtoUV(int face, const PJ_XYZ& p, PJ_XY* puv) {
return FaceXYZtoUV(face, p, &(*puv).x, &(*puv).y);
}
// This function inverts ValidFaceXYZtoUV()
inline bool UVtoSphereXYZ(int face, double u, double v, PJ_XYZ* xyz) {
double major_coord = 1 / sqrt(1 + u*u + v*v);
double minor_coord_1 = u*major_coord;
double minor_coord_2 = v*major_coord;
switch(face) {
case 0: xyz->x = major_coord;
xyz->y = minor_coord_1;
xyz->z = minor_coord_2; break;
case 1: xyz->x = -minor_coord_1;
xyz->y = major_coord;
xyz->z = minor_coord_2; break;
case 2: xyz->x = -minor_coord_1;
xyz->y = -minor_coord_2;
xyz->z = major_coord; break;
case 3: xyz->x = -major_coord;
xyz->y = -minor_coord_2;
xyz->z = -minor_coord_1; break;
case 4: xyz->x = minor_coord_2;
xyz->y = -major_coord;
xyz->z = -minor_coord_1; break;
default:xyz->x = minor_coord_2;
xyz->y = minor_coord_1;
xyz->z = -major_coord; break;
}
return true;
}
// ============================================
//
// The Forward and Inverse Functions
//
// ============================================
static PJ_XY s2_forward (PJ_LP lp, PJ *P) {
struct pj_opaque *Q = static_cast<struct pj_opaque*>(P->opaque);
double lat, lon;
/* Convert the geodetic latitude to a geocentric latitude.
* This corresponds to the shift from the ellipsoid to the sphere
* described in [LK12]. */
if (P->es != 0.0) {
lat = atan(Q->one_minus_f_squared * tan(lp.phi));
} else {
lat = lp.phi;
}
lon = lp.lam;
// Convert the lat/lon to x,y,z on the unit sphere
double x, y, z;
double sinlat, coslat;
double sinlon, coslon;
sinlat = sin(lat);
coslat = cos(lat);
sinlon = sin(lon);
coslon = cos(lon);
x = coslat * coslon;
y = coslat * sinlon;
z = sinlat;
PJ_XYZ spherePoint {x, y, z};
PJ_XY uvCoords;
ValidFaceXYZtoUV(Q->face, spherePoint, &uvCoords.x, &uvCoords.y);
double s = UVtoST(uvCoords.x, Q->UVtoST);
double t = UVtoST(uvCoords.y, Q->UVtoST);
return {s, t};
}
static PJ_LP s2_inverse (PJ_XY xy, PJ *P) {
PJ_LP lp = {0.0,0.0};
struct pj_opaque *Q = static_cast<struct pj_opaque*>(P->opaque);
// Do the S2 projections to get from s,t to u,v to x,y,z
double u = STtoUV(xy.x, Q->UVtoST);
double v = STtoUV(xy.y, Q->UVtoST);
PJ_XYZ sphereCoords;
UVtoSphereXYZ(Q->face, u, v, &sphereCoords);
double q = sphereCoords.x;
double r = sphereCoords.y;
double s = sphereCoords.z;
// Get the spherical angles from the x y z
lp.phi = acos(-s) - M_HALFPI;
lp.lam = atan2(r, q);
/* Apply the shift from the sphere to the ellipsoid as described
* in [LK12]. */
if (P->es != 0.0) {
int invert_sign;
volatile double tanphi, xa;
invert_sign = (lp.phi < 0.0 ? 1 : 0);
tanphi = tan(lp.phi);
xa = P->b / sqrt(tanphi * tanphi + Q->one_minus_f_squared);
lp.phi = atan(sqrt(Q->a_squared - xa * xa) / (Q->one_minus_f * xa));
if (invert_sign) {
lp.phi = -lp.phi;
}
}
return lp;
}
PJ *PROJECTION(s2) {
struct pj_opaque *Q = static_cast<struct pj_opaque*>(calloc (1, sizeof (struct pj_opaque)));
if (nullptr==Q)
return pj_default_destructor (P, PROJ_ERR_OTHER /*ENOMEM*/);
P->opaque = Q;
/* Determine which UVtoST function is to be used */
PROJVALUE maybeUVtoST = pj_param(P->ctx, P->params, "sUVtoST");
if (nullptr != maybeUVtoST.s) {
try {
Q->UVtoST = stringToS2ProjectionType.at(maybeUVtoST.s);
} catch (const std::out_of_range&) {
proj_log_error(P, _("Invalid value for s2 parameter: should be linear, quadratic, tangent, or none."));
return pj_default_destructor (P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
}
} else {
Q->UVtoST = Quadratic;
}
P->left = PJ_IO_UNITS_RADIANS;
P->right = PJ_IO_UNITS_PROJECTED;
P->from_greenwich = -P->lam0;
P->inv = s2_inverse;
P->fwd = s2_forward;
/* Determine the cube face from the center of projection. */
if (P->phi0 >= M_HALFPI - M_FORTPI / 2.0) {
Q->face = FACE_TOP;
} else if (P->phi0 <= -(M_HALFPI - M_FORTPI / 2.0)) {
Q->face = FACE_BOTTOM;
} else if (fabs(P->lam0) <= M_FORTPI) {
Q->face = FACE_FRONT;
} else if (fabs(P->lam0) <= M_HALFPI + M_FORTPI) {
Q->face = (P->lam0 > 0.0 ? FACE_RIGHT : FACE_LEFT);
} else {
Q->face = FACE_BACK;
}
/* Fill in useful values for the ellipsoid <-> sphere shift
* described in [LK12]. */
if (P->es != 0.0) {
Q->a_squared = P->a * P->a;
Q->one_minus_f = 1.0 - (P->a - P->b) / P->a;
Q->one_minus_f_squared = Q->one_minus_f * Q->one_minus_f;
}
return P;
}
|