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

Last change on this file since 10758 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

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