1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.text.DecimalFormat;
|
---|
9 |
|
---|
10 | import org.junit.Rule;
|
---|
11 | import org.junit.Test;
|
---|
12 | import org.openstreetmap.josm.data.Bounds;
|
---|
13 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
14 |
|
---|
15 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
16 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Unit tests for class {@link LatLon}.
|
---|
20 | */
|
---|
21 | public class LatLonTest {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Setup test.
|
---|
25 | */
|
---|
26 | @Rule
|
---|
27 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
28 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
29 |
|
---|
30 | private static final double EPSILON = 1e-6;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Lat/Lon sample values for unit tests
|
---|
34 | */
|
---|
35 | @SuppressFBWarnings(value = "MS_PKGPROTECT")
|
---|
36 | public static final double[] SAMPLE_VALUES = new double[]{
|
---|
37 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
38 | -180.0, -179.9, -179.6, -179.5, -179.4, -179.1, -179.0, -100.0, -99.9, -10.0, -9.9, -1.0, -0.1,
|
---|
39 | 180.0, 179.9, 179.6, 179.5, 179.4, 179.1, 179.0, 100.0, 99.9, 10.0, 9.9, 1.0, 0.1,
|
---|
40 | 0.12, 0.123, 0.1234, 0.12345, 0.123456, 0.1234567,
|
---|
41 | 1.12, 1.123, 1.1234, 1.12345, 1.123456, 1.1234567,
|
---|
42 | 10.12, 10.123, 10.1234, 10.12345, 10.123456, 10.1234567,
|
---|
43 | 100.12, 100.123, 100.1234, 100.12345, 100.123456, 100.1234567
|
---|
44 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
45 | };
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Test of {@link LatLon#roundToOsmPrecision}
|
---|
49 | */
|
---|
50 | @Test
|
---|
51 | public void testRoundToOsmPrecision() {
|
---|
52 |
|
---|
53 | for (double value : SAMPLE_VALUES) {
|
---|
54 | assertEquals(LatLon.roundToOsmPrecision(value), value, 0);
|
---|
55 | }
|
---|
56 |
|
---|
57 | assertEquals(LatLon.roundToOsmPrecision(0.0), 0.0, 0);
|
---|
58 | assertEquals(LatLon.roundToOsmPrecision(-0.0), 0.0, 0);
|
---|
59 |
|
---|
60 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
61 | assertEquals(LatLon.roundToOsmPrecision(0.12345678), 0.1234568, 0);
|
---|
62 | assertEquals(LatLon.roundToOsmPrecision(0.123456789), 0.1234568, 0);
|
---|
63 |
|
---|
64 | assertEquals(LatLon.roundToOsmPrecision(1.12345678), 1.1234568, 0);
|
---|
65 | assertEquals(LatLon.roundToOsmPrecision(1.123456789), 1.1234568, 0);
|
---|
66 |
|
---|
67 | assertEquals(LatLon.roundToOsmPrecision(10.12345678), 10.1234568, 0);
|
---|
68 | assertEquals(LatLon.roundToOsmPrecision(10.123456789), 10.1234568, 0);
|
---|
69 |
|
---|
70 | assertEquals(LatLon.roundToOsmPrecision(100.12345678), 100.1234568, 0);
|
---|
71 | assertEquals(LatLon.roundToOsmPrecision(100.123456789), 100.1234568, 0);
|
---|
72 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
73 |
|
---|
74 | assertEquals(LatLon.roundToOsmPrecision(100.00000001), 100.0000000, 0);
|
---|
75 | assertEquals(LatLon.roundToOsmPrecision(100.000000001), 100.0000000, 0);
|
---|
76 | assertEquals(LatLon.roundToOsmPrecision(100.0000000001), 100.0000000, 0);
|
---|
77 | assertEquals(LatLon.roundToOsmPrecision(100.00000000001), 100.0000000, 0);
|
---|
78 | assertEquals(LatLon.roundToOsmPrecision(100.000000000001), 100.0000000, 0);
|
---|
79 | assertEquals(LatLon.roundToOsmPrecision(100.0000000000001), 100.0000000, 0);
|
---|
80 | assertEquals(LatLon.roundToOsmPrecision(100.00000000000001), 100.0000000, 0);
|
---|
81 | assertEquals(LatLon.roundToOsmPrecision(100.000000000000001), 100.0000000, 0);
|
---|
82 | assertEquals(LatLon.roundToOsmPrecision(100.0000000000000001), 100.0000000, 0);
|
---|
83 | assertEquals(LatLon.roundToOsmPrecision(100.00000000000000001), 100.0000000, 0);
|
---|
84 | assertEquals(LatLon.roundToOsmPrecision(100.000000000000000001), 100.0000000, 0);
|
---|
85 | assertEquals(LatLon.roundToOsmPrecision(100.0000000000000000001), 100.0000000, 0);
|
---|
86 | assertEquals(LatLon.roundToOsmPrecision(100.00000000000000000001), 100.0000000, 0);
|
---|
87 |
|
---|
88 | assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999999), 100.0000000, 0);
|
---|
89 | assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999999), 100.0000000, 0);
|
---|
90 | assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999999), 100.0000000, 0);
|
---|
91 | assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999), 100.0000000, 0);
|
---|
92 | assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999), 100.0000000, 0);
|
---|
93 | assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999), 100.0000000, 0);
|
---|
94 | assertEquals(LatLon.roundToOsmPrecision(99.999999999999999), 100.0000000, 0);
|
---|
95 | assertEquals(LatLon.roundToOsmPrecision(99.99999999999999), 100.0000000, 0);
|
---|
96 | assertEquals(LatLon.roundToOsmPrecision(99.9999999999999), 100.0000000, 0);
|
---|
97 | assertEquals(LatLon.roundToOsmPrecision(99.999999999999), 100.0000000, 0);
|
---|
98 | assertEquals(LatLon.roundToOsmPrecision(99.99999999999), 100.0000000, 0);
|
---|
99 | assertEquals(LatLon.roundToOsmPrecision(99.9999999999), 100.0000000, 0);
|
---|
100 | assertEquals(LatLon.roundToOsmPrecision(99.999999999), 100.0000000, 0);
|
---|
101 | assertEquals(LatLon.roundToOsmPrecision(99.99999999), 100.0000000, 0);
|
---|
102 | assertEquals(LatLon.roundToOsmPrecision(99.9999999), 99.9999999, 0);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Test {@link LatLon#toIntervalLat}
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | public void testToIntervalLat() {
|
---|
110 | assertEquals(-90.0, LatLon.toIntervalLat(-90.0), 0);
|
---|
111 | assertEquals(0.0, LatLon.toIntervalLat(0.0), 0);
|
---|
112 | assertEquals(90.0, LatLon.toIntervalLat(90.0), 0);
|
---|
113 |
|
---|
114 | assertEquals(-90.0, LatLon.toIntervalLat(-91.0), 0);
|
---|
115 | assertEquals(90.0, LatLon.toIntervalLat(91.0), 0);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Test of {@link LatLon#toIntervalLon}
|
---|
120 | */
|
---|
121 | @Test
|
---|
122 | public void testToIntervalLon() {
|
---|
123 | assertEquals(-180.0, LatLon.toIntervalLon(-180.0), 0);
|
---|
124 | assertEquals(0.0, LatLon.toIntervalLon(0.0), 0);
|
---|
125 | assertEquals(180.0, LatLon.toIntervalLon(180.0), 0);
|
---|
126 |
|
---|
127 | assertEquals(179.0, LatLon.toIntervalLon(-181.0), 0);
|
---|
128 | assertEquals(-179.0, LatLon.toIntervalLon(181.0), 0);
|
---|
129 |
|
---|
130 | assertEquals(-1.0, LatLon.toIntervalLon(359.0), 0);
|
---|
131 | assertEquals(1.0, LatLon.toIntervalLon(-359.0), 0);
|
---|
132 |
|
---|
133 | assertEquals(1.0, LatLon.toIntervalLon(361.0), 0);
|
---|
134 | assertEquals(-1.0, LatLon.toIntervalLon(-361.0), 0);
|
---|
135 |
|
---|
136 | assertEquals(179.0, LatLon.toIntervalLon(539.0), 0);
|
---|
137 | assertEquals(-179.0, LatLon.toIntervalLon(-539.0), 0);
|
---|
138 |
|
---|
139 | assertEquals(-179.0, LatLon.toIntervalLon(541.0), 0);
|
---|
140 | assertEquals(179.0, LatLon.toIntervalLon(-541.0), 0);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Unit test of methods {@link LatLon#equals} and {@link LatLon#hashCode}.
|
---|
145 | */
|
---|
146 | @Test
|
---|
147 | public void testEqualsContract() {
|
---|
148 | EqualsVerifier.forClass(LatLon.class).usingGetClass()
|
---|
149 | .withPrefabValues(DecimalFormat.class, new DecimalFormat("00.0"), new DecimalFormat("00.000"))
|
---|
150 | .verify();
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Unit test of {@link LatLon#LatLon(LatLon)}.
|
---|
155 | */
|
---|
156 | @Test
|
---|
157 | public void testCopyConstructor() {
|
---|
158 | assertEquals(LatLon.NORTH_POLE, new LatLon(LatLon.NORTH_POLE));
|
---|
159 | assertEquals(LatLon.SOUTH_POLE, new LatLon(LatLon.SOUTH_POLE));
|
---|
160 | assertEquals(new LatLon(1, 2), new LatLon(new LatLon(1, 2)));
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Test of {@link LatLon#bearing}
|
---|
165 | */
|
---|
166 | @Test
|
---|
167 | public void testBearing() {
|
---|
168 | LatLon c = new LatLon(47.000000, 19.000000);
|
---|
169 | LatLon e = new LatLon(47.000000, 19.000001);
|
---|
170 | LatLon n = new LatLon(47.000001, 19.000000);
|
---|
171 | assertEquals(0, Math.toDegrees(c.bearing(n)), EPSILON);
|
---|
172 | assertEquals(90, Math.toDegrees(c.bearing(e)), EPSILON);
|
---|
173 | assertEquals(180, Math.toDegrees(n.bearing(c)), EPSILON);
|
---|
174 | assertEquals(270, Math.toDegrees(e.bearing(c)), EPSILON);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Test of {@link LatLon#distance}
|
---|
179 | */
|
---|
180 | @Test
|
---|
181 | public void testDistance() {
|
---|
182 | assertEquals(0.0, LatLon.ZERO.distance(LatLon.ZERO), 0);
|
---|
183 | assertEquals(90.0, LatLon.ZERO.distance(LatLon.NORTH_POLE), 0);
|
---|
184 | assertEquals(180.0, LatLon.SOUTH_POLE.distance(LatLon.NORTH_POLE), 0);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Test of {@link LatLon#distanceSq}
|
---|
189 | */
|
---|
190 | @Test
|
---|
191 | public void testDistanceSq() {
|
---|
192 | assertEquals(0.0, LatLon.ZERO.distanceSq(LatLon.ZERO), 0);
|
---|
193 | assertEquals(90d*90d, LatLon.ZERO.distanceSq(LatLon.NORTH_POLE), 0);
|
---|
194 | assertEquals(180d*180d, LatLon.SOUTH_POLE.distanceSq(LatLon.NORTH_POLE), 0);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Tests the methods {@link LatLon#latToString}, {@link LatLon#lonToString}, {@link LatLon#toStringCSV}.
|
---|
199 | */
|
---|
200 | @Test
|
---|
201 | public void testFormatting() {
|
---|
202 | LatLon c = new LatLon(47.000000, 19.000000);
|
---|
203 | assertEquals("47.0;19.0", c.toStringCSV(";"));
|
---|
204 | assertEquals("47.0", c.latToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
205 | assertEquals("19.0", c.lonToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
206 | assertEquals("47°00'00.0\"N", c.latToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
|
---|
207 | assertEquals("19°00'00.0\"E", c.lonToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
|
---|
208 | assertEquals("47°00.000'N", c.latToString(CoordinateFormat.NAUTICAL));
|
---|
209 | assertEquals("19°00.000'E", c.lonToString(CoordinateFormat.NAUTICAL));
|
---|
210 | assertEquals("5942074.0724311", c.latToString(CoordinateFormat.EAST_NORTH));
|
---|
211 | assertEquals("2115070.3250722", c.lonToString(CoordinateFormat.EAST_NORTH));
|
---|
212 | c = new LatLon(-47.000000, -19.000000);
|
---|
213 | assertEquals("47°00'00.0\"S", c.latToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
|
---|
214 | assertEquals("19°00'00.0\"W", c.lonToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
|
---|
215 | assertEquals("47°00.000'S", c.latToString(CoordinateFormat.NAUTICAL));
|
---|
216 | assertEquals("19°00.000'W", c.lonToString(CoordinateFormat.NAUTICAL));
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Test {@link LatLon#interpolate(LatLon, double)}
|
---|
221 | */
|
---|
222 | @Test
|
---|
223 | public void testInterpolate() {
|
---|
224 | LatLon ll1 = new LatLon(0, 0);
|
---|
225 | LatLon ll2 = new LatLon(30, 60);
|
---|
226 | LatLon ll3 = new LatLon(-70, -40);
|
---|
227 | // lat:
|
---|
228 | assertEquals(15, ll1.interpolate(ll2, 0.5).lat(), 1e-10);
|
---|
229 | assertEquals(0, ll1.interpolate(ll2, 0).lat(), 1e-10);
|
---|
230 | assertEquals(30, ll1.interpolate(ll2, 1).lat(), 1e-10);
|
---|
231 | assertEquals(0, ll3.interpolate(ll2, .7).lat(), 1e-10);
|
---|
232 | // lon
|
---|
233 | assertEquals(30, ll1.interpolate(ll2, 0.5).lon(), 1e-10);
|
---|
234 | assertEquals(0, ll1.interpolate(ll2, 0).lon(), 1e-10);
|
---|
235 | assertEquals(60, ll1.interpolate(ll2, 1).lon(), 1e-10);
|
---|
236 | assertEquals(0, ll3.interpolate(ll2, .4).lon(), 1e-10);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Test {@link LatLon#isOutSideWorld}
|
---|
241 | */
|
---|
242 | @Test
|
---|
243 | public void testIsOutSideWorld() {
|
---|
244 | assertFalse(LatLon.ZERO.isOutSideWorld());
|
---|
245 | assertTrue(LatLon.NORTH_POLE.isOutSideWorld());
|
---|
246 | assertTrue(LatLon.SOUTH_POLE.isOutSideWorld());
|
---|
247 | assertTrue(new LatLon(-181, 0).isOutSideWorld());
|
---|
248 | assertTrue(new LatLon(181, 0).isOutSideWorld());
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Test {@link LatLon#isValid}
|
---|
253 | */
|
---|
254 | @Test
|
---|
255 | public void testIsValid() {
|
---|
256 | assertTrue(LatLon.ZERO.isValid());
|
---|
257 | assertTrue(LatLon.NORTH_POLE.isValid());
|
---|
258 | assertTrue(LatLon.SOUTH_POLE.isValid());
|
---|
259 |
|
---|
260 | assertFalse(new LatLon(-91, 0).isValid());
|
---|
261 | assertFalse(new LatLon(91, 0).isValid());
|
---|
262 | assertFalse(new LatLon(0, -181).isValid());
|
---|
263 | assertFalse(new LatLon(0, 181).isValid());
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Test {@link LatLon#isWithin}
|
---|
268 | */
|
---|
269 | @Test
|
---|
270 | public void testIsWithin() {
|
---|
271 | assertTrue(LatLon.ZERO.isWithin(new Bounds(LatLon.ZERO)));
|
---|
272 | assertFalse(LatLon.ZERO.isWithin(new Bounds(LatLon.NORTH_POLE)));
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Test {@link LatLon#getCenter(LatLon)}
|
---|
277 | */
|
---|
278 | @Test
|
---|
279 | public void testGetCenter() {
|
---|
280 | LatLon ll1 = new LatLon(0, 0);
|
---|
281 | LatLon ll2 = new LatLon(30, 60);
|
---|
282 | LatLon ll3 = new LatLon(-70, -40);
|
---|
283 |
|
---|
284 | assertEquals(15, ll1.getCenter(ll2).lat(), 1e-10);
|
---|
285 | assertEquals(15, ll2.getCenter(ll1).lat(), 1e-10);
|
---|
286 | assertEquals(-20, ll3.getCenter(ll2).lat(), 1e-10);
|
---|
287 |
|
---|
288 | assertEquals(30, ll1.getCenter(ll2).lon(), 1e-10);
|
---|
289 | assertEquals(30, ll2.getCenter(ll1).lon(), 1e-10);
|
---|
290 | assertEquals(10, ll3.getCenter(ll2).lon(), 1e-10);
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Unit test of {@link LatLon#parse} method.
|
---|
295 | */
|
---|
296 | @Test
|
---|
297 | public void testParse() {
|
---|
298 | assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918° 19.24788°"));
|
---|
299 | assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("N 49.29918 E 19.24788°"));
|
---|
300 | assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918° 19.24788°"));
|
---|
301 | assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("N 49.29918 E 19.24788"));
|
---|
302 | assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("n 49.29918 e 19.24788"));
|
---|
303 | assertEquals(new LatLon(-19 - 24.788 / 60, -49 - 29.918 / 60), LatLon.parse("W 49°29.918' S 19°24.788'"));
|
---|
304 | assertEquals(new LatLon(-19 - 24.788 / 60, -49 - 29.918 / 60), LatLon.parse("w 49°29.918' s 19°24.788'"));
|
---|
305 | assertEquals(new LatLon(49 + 29. / 60 + 04. / 3600, 19 + 24. / 60 + 43. / 3600), LatLon.parse("N 49°29'04\" E 19°24'43\""));
|
---|
306 | assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918 N, 19.24788 E"));
|
---|
307 | assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918 n, 19.24788 e"));
|
---|
308 | assertEquals(new LatLon(49 + 29. / 60 + 21. / 3600, 19 + 24. / 60 + 38. / 3600), LatLon.parse("49°29'21\" N 19°24'38\" E"));
|
---|
309 | assertEquals(new LatLon(49 + 29. / 60 + 51. / 3600, 19 + 24. / 60 + 18. / 3600), LatLon.parse("49 29 51, 19 24 18"));
|
---|
310 | assertEquals(new LatLon(49 + 29. / 60, 19 + 24. / 60), LatLon.parse("49 29, 19 24"));
|
---|
311 | assertEquals(new LatLon(19 + 24. / 60, 49 + 29. / 60), LatLon.parse("E 49 29, N 19 24"));
|
---|
312 | assertEquals(new LatLon(49 + 29. / 60, 19 + 24. / 60), LatLon.parse("49° 29; 19° 24"));
|
---|
313 | assertEquals(new LatLon(49 + 29. / 60, 19 + 24. / 60), LatLon.parse("49° 29; 19° 24"));
|
---|
314 | assertEquals(new LatLon(49 + 29. / 60, -19 - 24. / 60), LatLon.parse("N 49° 29, W 19° 24"));
|
---|
315 | assertEquals(new LatLon(-49 - 29.5 / 60, 19 + 24.6 / 60), LatLon.parse("49° 29.5 S, 19° 24.6 E"));
|
---|
316 | assertEquals(new LatLon(49 + 29.918 / 60, 19 + 15.88 / 60), LatLon.parse("N 49 29.918 E 19 15.88"));
|
---|
317 | assertEquals(new LatLon(49 + 29.4 / 60, 19 + 24.5 / 60), LatLon.parse("49 29.4 19 24.5"));
|
---|
318 | assertEquals(new LatLon(-49 - 29.4 / 60, 19 + 24.5 / 60), LatLon.parse("-49 29.4 N -19 24.5 W"));
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Unit test of {@link LatLon#parse} method - invalid case 1.
|
---|
323 | */
|
---|
324 | @Test(expected = IllegalArgumentException.class)
|
---|
325 | public void testParseInvalid1() {
|
---|
326 | LatLon.parse("48°45'S 23°30'S");
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Unit test of {@link LatLon#parse} method - invalid case 2.
|
---|
331 | */
|
---|
332 | @Test(expected = IllegalArgumentException.class)
|
---|
333 | public void testParseInvalid2() {
|
---|
334 | LatLon.parse("47°45'N 24°00'S");
|
---|
335 | }
|
---|
336 | }
|
---|