Debian Codegraph @

test/uk/me/parabola/imgfmt/app/CoordTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2014.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import java.util.List;
20
21 import org.junit.Test;
22
23 import uk.me.parabola.mkgmap.filters.ShapeMergeFilter;
24 import uk.me.parabola.mkgmap.reader.osm.Way;
25
26 /**
27 * Test some basic methods of the Coord class regarding distance and bearing calculations
28 * @author GerdP
29 *
30 */
31 public class CoordTest {
32 Coord pLAX = new Coord(33.95, -118.4);
33 Coord pJFK = new Coord(40.6333333333, -73.7833333333333333);
34 Coord pD = new Coord(34.5, -116.5);
35 Coord p0_10 = new Coord (0.0, 10.0);
36 Coord p1_10 = new Coord (1.0, 10.0);
37 Coord p1_11 = new Coord (1.0, 11.0);
38 Coord p60_10 = new Coord (60.0, 10.0);
39 Coord p61_11 = new Coord (61.0, 11.0);
40 Coord russia1 = new Coord(3069580,8388608);
41 Coord russia2 = new Coord(3105677,8388608);
42
43 /**
44 */
45 @Test
46 public void testBearingGC() {
47 assertEquals(65.892222, pLAX.bearingToOnGreatCircle(pJFK, true), 0.1);
48 assertEquals(0.0, p0_10.bearingToOnGreatCircle(p1_10, true), 0.001);
49 assertEquals(89.991388, p1_10.bearingToOnGreatCircle(p1_11, true), 0.001);
50 assertEquals(44.99555, p0_10.bearingToOnGreatCircle(p1_11, true), 0.001);
51 }
52 @Test
53 public void testBearingRhumb() {
54 assertEquals(79.32388, pLAX.bearingToOnRhumbLine(pJFK, true), 0.1);
55 assertEquals(0, p0_10.bearingToOnRhumbLine(p1_10, true), 0.001);
56 assertEquals(90, p1_10.bearingToOnRhumbLine(p1_11, true), 0.001);
57 assertEquals(44.99861, p0_10.bearingToOnRhumbLine(p1_11, true), 0.001);
58 assertEquals(26.214722, p60_10.bearingToOnRhumbLine(p61_11, true), 0.001);
59 }
60
61 @Test
62 public void testDistanceRhumb() {
63 // http://www.movable-type.co.uk/scripts/latlong.html says 4011 km for R=6371 km
64 assertEquals(4011000 * Coord.R / 6371000, pLAX.distanceOnRhumbLine(pJFK), 1000);
65 assertEquals(4011000 * Coord.R / 6371000, pJFK.distanceOnRhumbLine(pLAX), 1000);
66 assertEquals(Coord.U/360, p1_10.distanceOnRhumbLine(p1_11), 20);
67 assertEquals(Coord.U/360, p0_10.distanceOnRhumbLine(p1_10), 20);
68 assertEquals(157200*Coord.R / 6371000, p0_10.distanceOnRhumbLine(p1_11), 200);
69 assertEquals(123900*Coord.R / 6371000, p60_10.distanceOnRhumbLine(p61_11), 200);
70 }
71
72 @Test
73 public void testDistanceGC() {
74 // http://www.movable-type.co.uk/scripts/latlong.html says 3973 km for R=6371 km
75 assertEquals(3973000 * Coord.R / 6371000, pLAX.distanceHaversine(pJFK), 1000);
76 assertEquals(3973000 * Coord.R / 6371000, pJFK.distanceHaversine(pLAX), 1000);
77 assertEquals(111300, p1_10.distanceHaversine(p1_11), 100);
78 assertEquals(111300, p0_10.distanceHaversine(p1_10), 100);
79 assertEquals(157400, p0_10.distanceHaversine(p1_11), 100);
80 assertEquals(124100, p60_10.distanceHaversine(p61_11), 100);
81 }
82
83 @Test
84 public void testMakeBetweenAt180(){
85 Coord russia3 = russia1.makeBetweenPoint(russia2, 0.5);
86 assertEquals(russia3.getLongitude(), russia1.getLongitude());
87 }
88
89 @Test
90 public void destOnRhumbLineAt180(){
91 Coord russia3 = russia1.destOnRhumbLine(1, 0.0);
92 assertEquals(russia3.getLongitude(), russia1.getLongitude());
93 Coord russia4 = russia1.destOnRhumbLine(10000, 0.0);
94 assertEquals(russia4.getLongitude(), russia1.getLongitude());
95 }
96
97 @Test
98 public void testOffset() {
99 assertEquals(pLAX, pLAX.offset(60, 100).offset(120, 100).offset(180, 100).offset(240, 100).offset(300, 100).offset(360, 100));
100 }
101
102 @Test
103 public void testOverflow() {
104 Coord c1 = new Coord(90.0,180.0);
105 Coord c2 = new Coord(90.0,-180.0);
106 assertFalse(c1.equals(c2));
107 assertFalse(c1.highPrecEquals(c2));
108 assertEquals(0,c1.distance(c2),0.0000001);
109 assertEquals(0x800000, c1.getLongitude());
110 }
111
112 @SuppressWarnings("unused")
113 @Test
114 public void testRounding() {
115 if (Coord.DELTA_SHIFT + 24 == Integer.SIZE) {
116 Coord c1 = new Coord(61.0000001, -23.0000001);
117 Coord c2 = new Coord(61.0000000, -23.0000001);
118 Coord c3 = new Coord(61.0000001, -23.0000000);
119 assertFalse(c1.highPrecEquals(c2));
120 assertFalse(c1.highPrecEquals(c3));
121 assertFalse(c2.highPrecEquals(c3));
122 assertTrue(c1.equals(c2));
123 assertTrue(c1.equals(c3));
124 assertTrue(c2.equals(c3));
125 assertEquals(61.0000001d,c1.getLatDegrees(),0.0000001);
126 assertEquals(61.0000000d,c2.getLatDegrees(),0.0000001);
127 assertEquals(61.0000001d,c3.getLatDegrees(),0.0000001);
128 assertEquals(-23.0000001d,c1.getLonDegrees(),0.0000001);
129 assertEquals(-23.0000001d,c2.getLonDegrees(),0.0000001);
130 assertEquals(-23.0000000d,c3.getLonDegrees(),0.0000001);
131 double lat1 = -1;
132 double lat2 = 1;
133 double lon1 = -1;
134 double lon2 = 1;
135 for (int i = 0; i < 1000; i++) {
136 Coord ct1 = new Coord(lat1,lon1);
137 Coord ct2 = new Coord(lat2,lon2);
138 assertEquals(lat1,ct1.getLatDegrees(),0.0000001);
139 assertEquals(lat2,ct2.getLatDegrees(),0.0000001);
140 assertEquals(lon1,ct1.getLonDegrees(),0.0000001);
141 assertEquals(lon2,ct2.getLonDegrees(),0.0000001);
142 lat1 = Math.nextAfter((float) lat1, 90);
143 lat2 = Math.nextAfter((float) lat2, 90);
144 lon1 = Math.nextAfter((float) lon1, 180);
145 lon2 = Math.nextAfter((float) lon2, 180);
146 }
147 }
148 }
149
150 @Test
151 public void alternativePos() {
152 Coord c1 = new Coord(61.0000001, -23.0000001);
153 List<Coord> alt1 = c1.getAlternativePositions();
154 assertEquals(1, alt1.size());
155 for (Coord c : alt1) {
156 assertTrue(c.distance(c1) < 3);
157 assertTrue(c.hasAlternativePos());
158 List<Coord> altx = c.getAlternativePositions();
159 assertFalse(altx.isEmpty());
160 }
161 Coord c2 = new Coord(61.1, -23.3);
162 List<Coord> alt2 = c2.getAlternativePositions();
163 assertEquals(3, alt2.size());
164 for (Coord c : alt2) {
165 assertTrue(c.distance(c2) < 3);
166 assertTrue(c.hasAlternativePos());
167 List<Coord> altx = c.getAlternativePositions();
168 assertFalse(altx.isEmpty());
169 }
170 }
171
172
173 @Test
174 public void testPlanet() throws Exception {
175 final uk.me.parabola.imgfmt.app.Area planet = uk.me.parabola.imgfmt.app.Area.PLANET;
176 long testVal = ShapeMergeFilter.calcAreaSizeTestVal(planet.toCoords());
177 double areaSizeCoords = (double) testVal / (2 * (1<<Coord.DELTA_SHIFT) * (1<<Coord.DELTA_SHIFT));
178 areaSizeCoords = Math.abs(areaSizeCoords);
179 double areaSizeBounds = (double) planet.getHeight() * planet.getWidth();
180 assertEquals(areaSizeBounds, areaSizeCoords, 0.0001);
181 boolean dir = Way.clockwise(planet.toCoords());
182 assertFalse(dir);
183 }
184
185 }