Implemented KKJ<->WGS84 transformations in C.
[ptas] / tests / ut_coord_trans / ut_coord_trans.c
1 #include "coordinate-system.h"
2
3 #include <math.h>
4 #include <stdio.h>
5
6 struct TestCoordinate {
7     KKJ x, y;
8     double lon, lat;
9 };
10
11 // Test data extracted from example on page
12 // http://developer.reittiopas.fi/pages/fi/http-get-interface.php
13 struct TestCoordinate testData[] = {
14     { 2556686, 6682815, 25.02051, 60.2528 },
15     { 2546340, 6675352, 24.832, 60.18713 },
16     { 2557985, 6685213, 25.04465, 60.27414 },
17     { 2556532, 6682578, 25.01767, 60.2507 },
18     { 2524959, 6686629, 24.44804, 60.2902 },
19     { 2559094, 6693721, 25.06718, 60.35033 },
20     { 2556861, 6683030, 25.02373, 60.25471 },
21     { 2556888, 6682971, 25.0242, 60.25417 },
22     { 2560257, 6698983, 25.08981, 60.39737 },
23     { 2562518, 6686969, 25.12709, 60.28923 },
24     { 2536615, 6673635, 24.65643, 60.1727 },
25     { 2559118, 6693833, 25.06764, 60.35133 },
26     { 2559182, 6693629, 25.06874, 60.34949 },
27     { 2556947, 6682640, 25.02518, 60.25119 },
28     { 2556822, 6682723, 25.02294, 60.25196 },
29     { 2559089, 6693605, 25.06705, 60.34929 },
30     { 2546445, 6675512, 24.83393, 60.18855 },
31     { 2556964, 6682609, 25.02547, 60.25091 },
32     { 2556740, 6682861, 25.0215, 60.25321 },
33     { 2559002, 6694007, 25.06559, 60.35291 }};
34
35
36 int testKKJxytoWGS84lola() {
37     double lon, lat;
38     int result = 0;
39     int i;
40
41     for (i = 0; i < sizeof(testData) / sizeof(struct TestCoordinate); ++i) {
42         KKJxy_to_WGS84lola(testData[i].x, testData[i].y, &lon, &lat);
43
44         if (fabs(testData[i].lon - lon) < 0.001 && fabs(testData[i].lat - lat) < 0.001) {
45             ;
46         } else {
47             printf("Got: (%f, %f), expected: (%f, %f)\n", lon, lat, testData[i].lon, testData[i].lat);
48             result = -1;
49         }
50     }
51
52     return result;
53 }
54
55
56 int testWGS84lolatoKKJxy() {
57     KKJ x, y;
58     int result = 0;
59     int i;
60
61     for (i = 0; i < sizeof(testData) / sizeof(struct TestCoordinate); ++i) {
62         WGS84lola_to_KKJxy(testData[i].lon, testData[i].lat, &x, &y);
63
64         if (abs(testData[i].x - x) < 2 && abs(testData[i].y - y) < 2) {
65             ;
66         } else {
67             printf("Got: (%u, %u), expected: (%u, %u)\n", x, y, testData[i].x, testData[i].y);
68             result = -1;
69         }
70     }
71
72     return result;
73 }
74
75
76
77 int main(int argc, char* argv[]) {
78     int testResult = 0;
79
80     // Test transforming from KKJxy to WGS84lonlat
81     testResult = testKKJxytoWGS84lola();
82
83     if (testResult == 0) {
84         printf("All tests in testKKJxytoWGS84lola passed\n");
85     }
86
87     // Test transforming from WGS84lonlat to KKJxy
88     testResult = testWGS84lolatoKKJxy();
89
90     if (testResult == 0) {
91         printf("All tests in testWGS84lolatoKKJxy passed\n");
92     }
93
94
95     return 0;
96 }