summaryrefslogtreecommitdiff
path: root/src/export/convert_coords.c
blob: abbbc22571170c73e14fe5fd86c720e6e7d2f48d (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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main( )
{
        double x, y;
        char buf[64];

        while( fgets( buf, sizeof(buf), stdin ) )
        {
            if( sscanf( buf, "%lf %lf", &x, &y ) == 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("%lf\t%lf\n", lat*180.0/M_PI, lon*180.0/M_PI);
            } else
                printf("\t\n");
        }
        return 0;
}