|
| 1 | +/* |
| 2 | + * Copyright (C) 2012 United States Government as represented by the Administrator of the |
| 3 | + * National Aeronautics and Space Administration. |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
1 | 6 | package gov.nasa.worldwind.geom.coords;
|
2 | 7 |
|
3 | 8 | import gov.nasa.worldwind.geom.LatLon;
|
4 | 9 | import org.junit.Test;
|
5 | 10 |
|
6 |
| -public class CoordTest { |
| 11 | +import static org.junit.Assert.*; |
7 | 12 |
|
8 |
| - private LatLon[] input0 = { |
9 |
| - LatLon.fromDegrees(-74.37916, 155.02235), |
10 |
| - LatLon.fromDegrees(0, 0), |
11 |
| - LatLon.fromDegrees(0.1300, -0.2324), |
12 |
| - LatLon.fromDegrees(-45.6456, 23.3545), |
13 |
| - LatLon.fromDegrees(-12.7650, -33.8765), |
14 |
| - LatLon.fromDegrees(23.4578, -135.4545), |
15 |
| - LatLon.fromDegrees(77.3450,156.9876), |
16 |
| - }; |
| 13 | +public class CoordTest |
| 14 | +{ |
| 15 | + private static boolean isClose(double x, double y, double limit) |
| 16 | + { |
| 17 | + return (Math.abs(x - y) < limit); |
| 18 | + } |
17 | 19 |
|
18 |
| - private LatLon[] MGRS_only = { |
19 |
| - LatLon.fromDegrees(-89.3454, -48.9306), |
20 |
| - LatLon.fromDegrees(-80.5434, -170.6540), |
21 |
| - }; |
22 |
| - |
23 |
| - private LatLon[] noInverse = { |
24 |
| - LatLon.fromDegrees(90.0000, 177.0000), |
25 |
| - LatLon.fromDegrees(-90.0000, -177.0000), |
26 |
| - LatLon.fromDegrees(90.0000, 3.0000), |
27 |
| - }; |
28 |
| - private String[] noInverseToMgrs = { |
29 |
| - "ZAH 00000 00000", "BAN 00000 00000", "ZAH 00000 00000" |
30 |
| - }; |
31 |
| - |
32 |
| - private boolean isClose(double x, double y, double limit) { |
33 |
| - return (Math.abs(x - y) < limit); |
34 |
| - } |
35 |
| - |
36 |
| - private boolean isClose(LatLon a, LatLon b) { |
37 |
| - double epsilonRad = Math.toRadians(9.0e-6); |
38 |
| - return isClose(a, b, epsilonRad); |
39 |
| - } |
40 |
| - |
41 |
| - private boolean isClose(LatLon a, LatLon b, double limit) { |
42 |
| - return isClose(a.latitude.radians, b.latitude.radians, limit) |
43 |
| - && isClose(a.longitude.radians, b.longitude.radians, limit); |
44 |
| - } |
| 20 | + private static boolean isClose(LatLon a, LatLon b) |
| 21 | + { |
| 22 | + double epsilonRad = Math.toRadians(9.0e-6); |
| 23 | + return isClose(a, b, epsilonRad); |
| 24 | + } |
45 | 25 |
|
46 |
| - @Test |
47 |
| - public void test() { |
48 |
| - for (LatLon p : input0) { |
| 26 | + private static boolean isClose(LatLon a, LatLon b, double limit) |
| 27 | + { |
| 28 | + return isClose(a.latitude.radians, b.latitude.radians, limit) |
| 29 | + && isClose(a.longitude.radians, b.longitude.radians, limit); |
| 30 | + } |
| 31 | + |
| 32 | + private static final LatLon[] TEST_POSITIONS = |
| 33 | + { |
| 34 | + LatLon.fromDegrees(-74.37916, 155.02235), |
| 35 | + LatLon.fromDegrees(0, 0), |
| 36 | + LatLon.fromDegrees(0.1300, -0.2324), |
| 37 | + LatLon.fromDegrees(-45.6456, 23.3545), |
| 38 | + LatLon.fromDegrees(-12.7650, -33.8765), |
| 39 | + LatLon.fromDegrees(23.4578, -135.4545), |
| 40 | + LatLon.fromDegrees(77.3450, 156.9876) |
| 41 | + }; |
| 42 | + |
| 43 | + @Test |
| 44 | + public void utmConstructionTest() |
| 45 | + { |
| 46 | + for (LatLon input : TEST_POSITIONS) |
| 47 | + { |
| 48 | + UTMCoord fromLatLon = UTMCoord.fromLatLon(input.latitude, input.longitude); |
| 49 | + UTMCoord utmCoord = UTMCoord.fromUTM(fromLatLon.getZone(), fromLatLon.getHemisphere(), fromLatLon.getEasting(), fromLatLon.getNorthing()); |
| 50 | + LatLon position = LatLon.fromRadians(utmCoord.getLatitude().radians, utmCoord.getLongitude().radians); |
| 51 | + assertTrue(isClose(input, position)); |
| 52 | + } |
| 53 | + } |
49 | 54 |
|
50 |
| - UTMCoord utm = UTMCoord.fromLatLon(p.latitude, p.longitude); |
51 |
| - MGRSCoord mgrs = MGRSCoord.fromLatLon(p.latitude, p.longitude); |
52 |
| - UTMCoord coord1 = UTMCoord.fromUTM(utm.getZone(), utm.getHemisphere(), utm.getEasting(), utm.getNorthing()); |
53 |
| - System.out.println(p + " ==> " + " UTM: " + utm.toString() + ", MGRS: " + mgrs.toString()); |
54 |
| - |
55 |
| - |
56 |
| - LatLon p1 = LatLon.fromRadians(coord1.getLatitude().radians, coord1.getLongitude().radians); |
57 |
| - assert(isClose(p, p1)); |
58 |
| - |
59 |
| - MGRSCoord coord2 = MGRSCoord.fromString(mgrs.toString(), null); |
60 |
| - LatLon p2 = LatLon.fromRadians(coord2.getLatitude().radians, coord2.getLongitude().radians); |
61 |
| - assert(isClose(p.getLatitude().radians, p2.getLatitude().radians, 0.000020)); |
62 |
| - assert(isClose(p.getLongitude().radians, p2.getLongitude().radians, 0.000020)); |
63 |
| - } |
64 |
| - |
65 |
| - for (LatLon p : MGRS_only) { |
66 |
| - MGRSCoord mgrs = MGRSCoord.fromLatLon(p.latitude, p.longitude); |
67 |
| - |
68 |
| - System.out.println(p + " ==> " + "MGRS: " + mgrs.toString()); |
69 |
| - |
70 |
| - MGRSCoord coord2 = MGRSCoord.fromString(mgrs.toString(), null); |
71 |
| - LatLon p2 = LatLon.fromRadians(coord2.getLatitude().radians, coord2.getLongitude().radians); |
72 |
| - assert(isClose(p, p2, 0.000020)); |
73 |
| - } |
74 |
| - |
75 |
| - for (int i=0; i < noInverse.length; i++) { |
76 |
| - LatLon p = noInverse[i]; |
77 |
| - MGRSCoord mgrs = MGRSCoord.fromLatLon(p.latitude, p.longitude); |
78 |
| - |
79 |
| - System.out.print(p + " ==> " + "MGRS: " + mgrs.toString()); |
80 |
| - |
81 |
| - MGRSCoord coord2 = MGRSCoord.fromString(mgrs.toString(), null); |
82 |
| - LatLon p2 = LatLon.fromRadians(coord2.getLatitude().radians, coord2.getLongitude().radians); |
83 |
| - System.out.println(" ==> " + p2); |
84 |
| - assert(mgrs.toString().trim().equals(noInverseToMgrs[i])); |
85 |
| - } |
86 |
| - } |
| 55 | + @Test |
| 56 | + public void mgrsConstructionTest() |
| 57 | + { |
| 58 | + for (LatLon input : TEST_POSITIONS) |
| 59 | + { |
| 60 | + MGRSCoord fromLatLon = MGRSCoord.fromLatLon(input.latitude, input.longitude); |
| 61 | + MGRSCoord fromString = MGRSCoord.fromString(fromLatLon.toString(), null); |
| 62 | + LatLon position = LatLon.fromRadians(fromString.getLatitude().radians, fromString.getLongitude().radians); |
| 63 | + assertTrue(isClose(input, position, 000020)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static final LatLon[] MGRS_ONLY_POSITIONS = |
| 68 | + { |
| 69 | + LatLon.fromDegrees(-89.3454, -48.9306), |
| 70 | + LatLon.fromDegrees(-80.5434, -170.6540), |
| 71 | + }; |
| 72 | + |
| 73 | + @Test |
| 74 | + public void mgrsOnlyConstructionTest() |
| 75 | + { |
| 76 | + for (LatLon input : MGRS_ONLY_POSITIONS) |
| 77 | + { |
| 78 | + MGRSCoord fromLatLon = MGRSCoord.fromLatLon(input.latitude, input.longitude); |
| 79 | + MGRSCoord fromString = MGRSCoord.fromString(fromLatLon.toString(), null); |
| 80 | + LatLon position = LatLon.fromRadians(fromString.getLatitude().radians, fromString.getLongitude().radians); |
| 81 | + assertTrue(isClose(input, position, 000020)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static final LatLon[] NO_INVERSE_POSITIONS = |
| 86 | + { |
| 87 | + LatLon.fromDegrees(90.0000, 177.0000), |
| 88 | + LatLon.fromDegrees(-90.0000, -177.0000), |
| 89 | + LatLon.fromDegrees(90.0000, 3.0000) |
| 90 | + }; |
| 91 | + |
| 92 | + private static final String[] NO_INVERSE_TO_MGRS = |
| 93 | + { |
| 94 | + "ZAH 00000 00000", "BAN 00000 00000", "ZAH 00000 00000" |
| 95 | + }; |
| 96 | + |
| 97 | + @Test |
| 98 | + public void noInverseToMGRSTest() |
| 99 | + { |
| 100 | + for (int i = 0; i < NO_INVERSE_POSITIONS.length; i++) |
| 101 | + { |
| 102 | + LatLon input = NO_INVERSE_POSITIONS[i]; |
| 103 | + MGRSCoord fromLatLon = MGRSCoord.fromLatLon(input.latitude, input.longitude); |
| 104 | + String mgrsString = fromLatLon.toString().trim(); |
| 105 | + assertEquals(mgrsString, NO_INVERSE_TO_MGRS[i]); |
| 106 | + } |
| 107 | + } |
87 | 108 | }
|
0 commit comments