Skip to content

Commit

Permalink
Add inverse lagrange projection
Browse files Browse the repository at this point in the history
Courtesy of Michael Stumpf <mi12st34@gmail.com>
  • Loading branch information
kbevers committed Jun 25, 2018
1 parent 5c192b0 commit 6e38026
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
44 changes: 35 additions & 9 deletions src/PJ_lagrng.c
@@ -1,19 +1,19 @@
#define PJ_LIB__

#include <errno.h>
#include <math.h>

#include "proj.h"
#include <proj.h>
#include "projects.h"

PROJ_HEAD(lagrng, "Lagrange") "\n\tMisc Sph, no inv.\n\tW=";
PROJ_HEAD(lagrng, "Lagrange") "\n\tMisc Sph\n\tW=";

#define TOL 1e-10

struct pj_opaque {
double a1;
double a2;
double hrw;
double hw;
double rw;
double w;
};


Expand All @@ -39,27 +39,53 @@ static XY s_forward (LP lp, PJ *P) { /* Spheroidal, forward */
}


static LP s_inverse (XY xy, PJ *P) { /* Spheroidal, inverse */
LP lp = {0.0,0.0};
struct pj_opaque *Q = P->opaque;
double c, x2, y2p, y2m;

if (fabs(fabs(xy.y) - 2.) < TOL) {
lp.phi = xy.y < 0 ? -M_HALFPI : M_HALFPI;
lp.lam = 0;
} else {
x2 = xy.x * xy.x;
y2p = 2. + xy.y;
y2m = 2. - xy.y;
if (fabs(c = y2p * y2m - x2) < TOL) {
proj_errno_set(P, PJD_ERR_TOLERANCE_CONDITION);
return lp;
}
lp.phi = 2. * atan(pow((y2p * y2p + x2) / (Q->a2 * (y2m * y2m + x2)), Q->hw)) - M_HALFPI;
lp.lam = Q->w * atan2(4. * xy.x, c);
}
return lp;
}


PJ *PROJECTION(lagrng) {
double phi1;
struct pj_opaque *Q = pj_calloc (1, sizeof (struct pj_opaque));
if (0==Q)
return pj_default_destructor (P, ENOMEM);
P->opaque = Q;

Q->rw = pj_param(P->ctx, P->params, "dW").f;
if (Q->rw <= 0)
Q->w = pj_param(P->ctx, P->params, "dW").f;
if (Q->w <= 0)
return pj_default_destructor(P, PJD_ERR_W_OR_M_ZERO_OR_LESS);

Q->rw = 1. / Q->rw;
Q->hw = 0.5 * Q->w;
Q->rw = 1. / Q->w;
Q->hrw = 0.5 * Q->rw;
phi1 = sin(pj_param(P->ctx, P->params, "rlat_1").f);
if (fabs(fabs(phi1) - 1.) < TOL)
return pj_default_destructor(P, PJD_ERR_LAT_LARGER_THAN_90);

Q->a1 = pow((1. - phi1)/(1. + phi1), Q->hrw);
Q->a2 = Q->a1 * Q->a1;

P->es = 0.;
P->inv = s_inverse;
P->fwd = s_forward;

return P;
}

22 changes: 13 additions & 9 deletions test/gie/builtins.gie
Expand Up @@ -2418,15 +2418,19 @@ Lagrange
-------------------------------------------------------------------------------
operation +proj=lagrng +a=6400000 +W=2 +lat_1=0.5 +lat_2=2
-------------------------------------------------------------------------------
tolerance 0.1 mm
accept 2 1
expect 111703.375917226 27929.831908033
accept 2 -1
expect 111699.122088816 -83784.178013358
accept -2 1
expect -111703.375917226 27929.831908033
accept -2 -1
expect -111699.122088816 -83784.178013358
tolerance 0.1 mm
accept 2 1
expect 111703.375917226 27929.831908033
roundtrip 100
accept 2 -1
expect 111699.122088816 -83784.178013358
roundtrip 100
accept -2 1
expect -111703.375917226 27929.831908033
roundtrip 100
accept -2 -1
expect -111699.122088816 -83784.178013358
roundtrip 100


===============================================================================
Expand Down

0 comments on commit 6e38026

Please sign in to comment.