source: josm/trunk/test/unit/org/openstreetmap/josm/data/projection/SwissGridTest.java@ 9130

Last change on this file since 9130 was 8836, checked in by Don-vip, 9 years ago

fix Checkstyle issues

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import static org.junit.Assert.assertSame;
5import static org.junit.Assert.assertTrue;
6
7import org.junit.BeforeClass;
8import org.junit.Ignore;
9import org.junit.Test;
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.coor.EastNorth;
12import org.openstreetmap.josm.data.coor.LatLon;
13
14public class SwissGridTest {
15 public static final String SWISS_EPSG_CODE = "EPSG:21781";
16 private boolean debug = false;
17
18 /**
19 * Setup test.
20 */
21 @BeforeClass
22 public static void setUp() {
23 Main.setProjection(Projections.getProjectionByCode(SWISS_EPSG_CODE)); // Swiss grid
24 }
25
26 // CHECKSTYLE.OFF: LineLength
27
28 /**
29 * source: http://www.swisstopo.admin.ch/internet/swisstopo/en/home/topics/survey/sys/refsys/switzerland.parsysrelated1.37696.downloadList.97912.DownloadFile.tmp/swissprojectionen.pdf
30 */
31 ProjData[] data = {
32 new ProjData("Zimmerwald", d(7, 27, 54.983506), d(46, 52, 37.540562), 947.149, 602030.680, 191775.030, 897.915),
33 new ProjData("Chrischona", d(7, 40, 6.983077), d(47, 34, 1.385301), 504.935, 617306.300, 268507.300, 456.064),
34 new ProjData("Pfaender", d(9, 47, 3.697723), d(47, 30, 55.172797), 1089.372, 776668.105, 265372.681, 1042.624),
35 new ProjData("La Givrine", d(6, 6, 7.326361), d(46, 27, 14.690021), 1258.274, 497313.292, 145625.438, 1207.434),
36 new ProjData("Monte Generoso", d(9, 1, 16.389053), d(45, 55, 45.438020), 1685.027, 722758.810, 87649.670, 1636.600) };
37
38 // CHECKSTYLE.ON: LineLength
39
40 private double d(double deg, double min, double sec) {
41 return deg + min / 60. + sec / 3600.;
42 }
43
44 private static class ProjData {
45 public String name;
46 public LatLon ll;
47 public EastNorth en;
48
49 ProjData(String name, double lon, double lat, double h1, double x, double y, double h2) {
50 this.name = name;
51 ll = new LatLon(lat, lon);
52 en = new EastNorth(x, y);
53 }
54 }
55
56 public static final double EPSILON_APPROX = 1.5;
57 public static final double EPSILON_ACCURATE = 0.05;
58
59 public void projReferenceTest(final double epsilon) {
60 Projection swiss = Projections.getProjectionByCode("EPSG:21781"); // Swiss grid
61 StringBuilder errs = new StringBuilder();
62 for (ProjData pd : data) {
63 EastNorth en2 = swiss.latlon2eastNorth(pd.ll);
64 if (Math.abs(pd.en.east() - en2.east()) > epsilon || Math.abs(pd.en.north() - en2.north()) > epsilon) {
65 errs.append(String.format("%s should be: %s but is: %s%n", pd.name, pd.en, en2));
66 }
67 }
68 assertSame(errs.toString(), errs.length(), 0);
69 }
70
71 @Test
72 public void projReferenceTestApprox() {
73 projReferenceTest(EPSILON_APPROX);
74 }
75
76 @Test
77 @Ignore("high accuracy of epsilon=" + EPSILON_ACCURATE + " is not met")
78 public void projReferenceTestAccurate() {
79 // TODO make this test pass
80 projReferenceTest(EPSILON_ACCURATE);
81 }
82
83 @Test
84 public void a_latlon2eastNorth_test() {
85 LatLon ll = new LatLon(46.518, 6.567);
86 EastNorth en = Main.getProjection().latlon2eastNorth(ll);
87 if (debug) {
88 System.out.println(en);
89 }
90 assertTrue("Lausanne", Math.abs(en.east() - 533111.69) < 0.1);
91 assertTrue("Lausanne", Math.abs(en.north() - 152227.85) < 0.1);
92
93 ll = new LatLon(47.78, 8.58);
94 en = Main.getProjection().latlon2eastNorth(ll);
95 if (debug) {
96 System.out.println(en);
97 }
98 assertTrue("Schafouse", Math.abs(en.east() - 685544.16) < 0.1);
99 assertTrue("Schafouse", Math.abs(en.north() - 292782.91) < 0.1);
100
101 ll = new LatLon(46.58, 10.48);
102 en = Main.getProjection().latlon2eastNorth(ll);
103 if (debug) {
104 System.out.println(en);
105 }
106 assertTrue("Grinson", Math.abs(en.east() - 833068.04) < 0.1);
107 assertTrue("Grinson", Math.abs(en.north() - 163265.39) < 0.1);
108
109 ll = new LatLon(46.0 + 57.0 / 60 + 3.89813884505 / 3600, 7.0 + 26.0 / 60 + 19.076595154147 / 3600);
110 en = Main.getProjection().latlon2eastNorth(ll);
111 if (debug) {
112 System.out.println(en);
113 }
114 assertTrue("Berne", Math.abs(en.east() - 600000.0) < 0.1);
115 assertTrue("Berne", Math.abs(en.north() - 200000.0) < 0.1);
116
117 ll = new LatLon(46.0 + 2.0 / 60 + 38.87 / 3600, 8.0 + 43.0 / 60 + 49.79 / 3600);
118 en = Main.getProjection().latlon2eastNorth(ll);
119 if (debug) {
120 System.out.println(en);
121 }
122 assertTrue("Ref", Math.abs(en.east() - 700000.0) < 0.1);
123 assertTrue("Ref", Math.abs(en.north() - 100000.0) < 0.1);
124 }
125
126 @Test
127 public void b_eastNorth2latlon_test() {
128 EastNorth en = new EastNorth(533111.69, 152227.85);
129 LatLon ll = Main.getProjection().eastNorth2latlon(en);
130 if (debug) {
131 System.out.println(ll);
132 }
133 assertTrue("Lausanne", Math.abs(ll.lat() - 46.518) < 0.00001);
134 assertTrue("Lausanne", Math.abs(ll.lon() - 6.567) < 0.00001);
135
136 en = new EastNorth(685544.16, 292782.91);
137 ll = Main.getProjection().eastNorth2latlon(en);
138 if (debug) {
139 System.out.println(ll);
140 }
141 assertTrue("Schafouse", Math.abs(ll.lat() - 47.78) < 0.00001);
142 assertTrue("Schafouse", Math.abs(ll.lon() - 8.58) < 0.00001);
143
144 en = new EastNorth(833068.04, 163265.39);
145 ll = Main.getProjection().eastNorth2latlon(en);
146 if (debug) {
147 System.out.println(ll);
148 }
149 assertTrue("Grinson", Math.abs(ll.lat() - 46.58) < 0.00001);
150 assertTrue("Grinson", Math.abs(ll.lon() - 10.48) < 0.00001);
151
152 en = new EastNorth(600000.0, 200000.0);
153 ll = Main.getProjection().eastNorth2latlon(en);
154 if (debug) {
155 System.out.println(ll);
156 }
157 assertTrue("Berne", Math.abs(ll.lat() - (46.0 + 57.0 / 60 + 3.89813884505 / 3600)) < 0.00001);
158 assertTrue("Berne", Math.abs(ll.lon() - (7.0 + 26.0 / 60 + 19.076595154147 / 3600)) < 0.00001);
159
160 en = new EastNorth(700000.0, 100000.0);
161 ll = Main.getProjection().eastNorth2latlon(en);
162 if (debug) {
163 System.out.println(ll);
164 }
165 assertTrue("Ref", Math.abs(ll.lat() - (46.0 + 2.0 / 60 + 38.87 / 3600)) < 0.00001);
166 assertTrue("Ref", Math.abs(ll.lon() - (8.0 + 43.0 / 60 + 49.79 / 3600)) < 0.00001);
167 }
168
169 /**
170 * Send and return should have less than 2mm of difference.
171 */
172 @Test
173 public void c_sendandreturn_test() {
174 EastNorth en = new EastNorth(533111.69, 152227.85);
175 LatLon ll = Main.getProjection().eastNorth2latlon(en);
176 EastNorth en2 = Main.getProjection().latlon2eastNorth(ll);
177 if (debug) {
178 System.out.println(en.east() - en2.east());
179 }
180 if (debug) {
181 System.out.println(en.north() - en2.north());
182 }
183 assertTrue("Lausanne", Math.abs(en.east() - en2.east()) < 0.002);
184 assertTrue("Lausanne", Math.abs(en.north() - en2.north()) < 0.002);
185
186 en = new EastNorth(685544.16, 292782.91);
187 ll = Main.getProjection().eastNorth2latlon(en);
188 en2 = Main.getProjection().latlon2eastNorth(ll);
189 if (debug) {
190 System.out.println(en.east() - en2.east());
191 }
192 if (debug) {
193 System.out.println(en.north() - en2.north());
194 }
195 assertTrue("Schafouse", Math.abs(en.east() - en2.east()) < 0.002);
196 assertTrue("Schafouse", Math.abs(en.north() - en2.north()) < 0.002);
197
198 en = new EastNorth(833068.04, 163265.39);
199 ll = Main.getProjection().eastNorth2latlon(en);
200 en2 = Main.getProjection().latlon2eastNorth(ll);
201 if (debug) {
202 System.out.println(en.east() - en2.east());
203 }
204 if (debug) {
205 System.out.println(en.north() - en2.north());
206 }
207 assertTrue("Grinson", Math.abs(en.east() - en2.east()) < 0.002);
208 assertTrue("Grinson", Math.abs(en.north() - en2.north()) < 0.002);
209
210 en = new EastNorth(600000.0, 200000.0);
211 ll = Main.getProjection().eastNorth2latlon(en);
212 en2 = Main.getProjection().latlon2eastNorth(ll);
213 if (debug) {
214 System.out.println(en.east() - en2.east());
215 }
216 if (debug) {
217 System.out.println(en.north() - en2.north());
218 }
219 assertTrue("Berne", Math.abs(en.east() - en2.east()) < 0.002);
220 assertTrue("Berne", Math.abs(en.north() - en2.north()) < 0.002);
221
222 en = new EastNorth(700000.0, 100000.0);
223 ll = Main.getProjection().eastNorth2latlon(en);
224 en2 = Main.getProjection().latlon2eastNorth(ll);
225 if (debug) {
226 System.out.println(en.east() - en2.east());
227 }
228 if (debug) {
229 System.out.println(en.north() - en2.north());
230 }
231 assertTrue("Ref", Math.abs(en.east() - en2.east()) < 0.002);
232 assertTrue("Ref", Math.abs(en.north() - en2.north()) < 0.002);
233 }
234}
Note: See TracBrowser for help on using the repository browser.