aboutsummaryrefslogtreecommitdiff
path: root/src/pj_inv.c
blob: ed05618a76f8a77c79859e94326c83ec399308e4 (plain)
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
/* general inverse projection */
#define PJ_LIB__
#include <projects.h>
#include <errno.h>
# define EPS 1.0e-12

/* inverse projection entry */
LP pj_inv(XY xy, PJ *P) {
    LP lp;

    /* can't do as much preliminary checking as with forward */
    if (xy.x == HUGE_VAL || xy.y == HUGE_VAL) {
        lp.lam = lp.phi = HUGE_VAL;
        pj_ctx_set_errno( P->ctx, -15);
        return lp;
    }

    errno = pj_errno = 0;
    P->ctx->last_errno = 0;

    /* descale and de-offset */
    xy.x = (xy.x * P->to_meter - P->x0) * P->ra;
    xy.y = (xy.y * P->to_meter - P->y0) * P->ra;

    /* Check for NULL pointer */
    if (P->inv != NULL) {
        lp = (*P->inv)(xy, P); /* inverse project */
        if (P->ctx->last_errno ) {
            lp.lam = lp.phi = HUGE_VAL;
        } else {
            lp.lam += P->lam0; /* reduce from del lp.lam */
            if (!P->over)
                lp.lam = adjlon(lp.lam); /* adjust longitude to CM */
            if (P->geoc && fabs(fabs(lp.phi)-M_HALFPI) > EPS)
                lp.phi = atan(P->one_es * tan(lp.phi));
        }
    } else {
       lp.lam = lp.phi = HUGE_VAL;
    }

    return lp;
}