summaryrefslogtreecommitdiff
path: root/src/convertcoords.c
blob: 3297d8f9e208ec6397826c4cb8864a550923e558 (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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
        double x = atof(argv[1]);
        double y = atof(argv[2]);

        double R   = 6365000;
        double fe  = 5200000;
        double fn  = 1200000;
        double ph0 = 0.5235977; // 30deg
        double ph1 = 0.7853980; // 45deg
        double ph2 = 0.9599309; // 55deg
        double l0  = 0.1745329; // 10deg

        double xs = (x-fe)/R;
        double ys = (y-fn)/R;
        double ph0_s = 0.25*M_PI+0.5*ph0;
        double ph1_s = 0.25*M_PI+0.5*ph1;
        double ph2_s = 0.25*M_PI+0.5*ph2;

        double n  = log(cos(ph1)/cos(ph2))/log(tan(ph2_s)/tan(ph1_s));
        double F  = cos(ph1)*pow(tan(ph1_s),n)/n;
        double r0 = F / pow(tan(ph0_s),n);
        double r  = sqrt(pow(xs,2)+pow(r0-ys,2));
        double th = atan(xs/(r0-ys));

        double lon = l0+th/n;
        double lat = 2.0*atan(pow(F/r,1.0/n))-0.5*M_PI;

        printf("%f %f", lat*180.0/M_PI, lon*180.0/M_PI);
        return 0;
}