translate_test.go 2.68 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
4
5
6
7
package radolan

import (
	"math"
	"testing"
)

Jonny Schäfer's avatar
Jonny Schäfer committed
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
func TestResolution(t *testing.T) {
	equal := func(a, b float64) bool {
		epsilon := 0.000001 // inaccuracy by 1mm
		return math.Abs(a-b) < epsilon
	}

	var (
		srcLat, srcLon = 48.173146, 11.546604 // Munich
		dstLat, dstLon = 53.534366, 08.576135 // Bremerhaven
		expDist        = 663.629945199998     // km
	)
	dummys := []*Composite{
		NewDummy("SF", 900, 900),
		NewDummy("SF", 450, 450),
		NewDummy("SF", 225, 225),
		NewDummy("SF", 112, 112),
		NewDummy("SF", 627, 212),
	}

	for _, comp := range dummys {
		srcX, srcY := comp.Translate(srcLat, srcLon)
		dstX, dstY := comp.Translate(dstLat, dstLon)

		vecX, vecY := (srcX-dstX)*comp.Rx, (srcY-dstY)*comp.Ry
		resDist := math.Sqrt(vecX*vecX + vecY*vecY)

		if !equal(resDist, expDist) {
			t.Errorf("dummy.Rx = %#v, dummy.Ry = %#v; distance: %#v expected: %#v)",
				comp.Rx, comp.Ry, resDist, expDist)
		}
	}

}

Jonny Schäfer's avatar
Jonny Schäfer committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
func TestTranslate(t *testing.T) {
	equal := func(a, b float64) bool {
		epsilon := 0.1 // inaccuracy by 100 meters
		return math.Abs(a-b) < epsilon
	}

	nationalGridPG := [][]float64{
		[]float64{54.6547, 01.9178, 0.0, 0.0},
		[]float64{54.8110, 15.8706, 1.0, 0.0},
		[]float64{51.0000, 09.0000, 0.5, 0.5},
		[]float64{46.8695, 03.4921, 0.0, 1.0},
		[]float64{46.9894, 14.7218, 1.0, 1.0},
	}

	nationalGrid := [][]float64{
		[]float64{54.5877, 02.0715, 0.0, 0.0},
		[]float64{54.7405, 15.7208, 1.0, 0.0},
		[]float64{51.0000, 09.0000, 0.5, 0.5},
		[]float64{46.9526, 03.5889, 0.0, 1.0},
		[]float64{47.0705, 14.6209, 1.0, 1.0},
	}

	extendedGrid := [][]float64{
		[]float64{56.5423, -0.8654, 0.0, 0.0},
		[]float64{56.4505, 21.6986, 1.0, 0.0},
		[]float64{51.0000, 09.0000, 3 / 7.0, 7 / 15.0},
		[]float64{43.9336, 02.3419, 0.0, 1.0},
		[]float64{43.8736, 18.2536, 1.0, 1.0},
	}

Jonny Schäfer's avatar
Jonny Schäfer committed
72
73
74
75
	dummyPG := NewDummy("PG", 460, 460)
	dummyFZ := NewDummy("FZ", 450, 450)
	dummyRX := NewDummy("RX", 900, 900)
	dummyEX := NewDummy("EX", 1400, 1500)
Jonny Schäfer's avatar
Jonny Schäfer committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

	testcases := []struct {
		comp *Composite
		edge [][]float64
	}{
		{dummyPG, nationalGridPG},
		{dummyFZ, nationalGrid},
		{dummyRX, nationalGrid},
		{dummyEX, extendedGrid},
	}

	for _, test := range testcases {
		t.Logf("dummy%s: Rx = %f; Ry = %f\n",
			test.comp.Product, test.comp.Rx, test.comp.Ry)
		t.Logf("dummy%s: offx = %f; offy = %f\n",
			test.comp.Product, test.comp.offx, test.comp.offy)

		for _, edge := range test.edge {
			// result
			rx, ry := test.comp.Translate(edge[0], edge[1])
			//expected
			ex := float64(test.comp.Dx-1) * edge[2]
			ey := float64(test.comp.Dy-1) * edge[3]

			if !equal(rx, ex) || !equal(ry, ey) {
				t.Errorf("dummy%s.Translate(%#v, %#v) = (%#v, %#v); expected: (%#v, %#v)",
					test.comp.Product, edge[0], edge[1], rx, ry, ex, ey)
			}
		}
	}
}