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