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 | LatLon ll = new LatLon(46.518, 6.567);
|
---|
82 | EastNorth en = Main.getProjection().latlon2eastNorth(ll);
|
---|
83 | if (debug) {
|
---|
84 | System.out.println(en);
|
---|
85 | }
|
---|
86 | assertTrue("Lausanne", Math.abs(en.east() - 533111.69) < 0.1);
|
---|
87 | assertTrue("Lausanne", Math.abs(en.north() - 152227.85) < 0.1);
|
---|
88 |
|
---|
89 | ll = new LatLon(47.78, 8.58);
|
---|
90 | en = Main.getProjection().latlon2eastNorth(ll);
|
---|
91 | if (debug) {
|
---|
92 | System.out.println(en);
|
---|
93 | }
|
---|
94 | assertTrue("Schafouse", Math.abs(en.east() - 685544.16) < 0.1);
|
---|
95 | assertTrue("Schafouse", Math.abs(en.north() - 292782.91) < 0.1);
|
---|
96 |
|
---|
97 | ll = new LatLon(46.58, 10.48);
|
---|
98 | en = Main.getProjection().latlon2eastNorth(ll);
|
---|
99 | if (debug) {
|
---|
100 | System.out.println(en);
|
---|
101 | }
|
---|
102 | assertTrue("Grinson", Math.abs(en.east() - 833068.04) < 0.1);
|
---|
103 | assertTrue("Grinson", Math.abs(en.north() - 163265.39) < 0.1);
|
---|
104 |
|
---|
105 | ll = new LatLon(46.0 + 57.0 / 60 + 3.89813884505 / 3600, 7.0 + 26.0 / 60 + 19.076595154147 / 3600);
|
---|
106 | en = Main.getProjection().latlon2eastNorth(ll);
|
---|
107 | if (debug) {
|
---|
108 | System.out.println(en);
|
---|
109 | }
|
---|
110 | assertTrue("Berne", Math.abs(en.east() - 600000.0) < 0.1);
|
---|
111 | assertTrue("Berne", Math.abs(en.north() - 200000.0) < 0.1);
|
---|
112 |
|
---|
113 | ll = new LatLon(46.0 + 2.0 / 60 + 38.87 / 3600, 8.0 + 43.0 / 60 + 49.79 / 3600);
|
---|
114 | en = Main.getProjection().latlon2eastNorth(ll);
|
---|
115 | if (debug) {
|
---|
116 | System.out.println(en);
|
---|
117 | }
|
---|
118 | assertTrue("Ref", Math.abs(en.east() - 700000.0) < 0.1);
|
---|
119 | assertTrue("Ref", Math.abs(en.north() - 100000.0) < 0.1);
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Test
|
---|
123 | public void b_eastNorth2latlon_test() {
|
---|
124 | EastNorth en = new EastNorth(533111.69, 152227.85);
|
---|
125 | LatLon ll = Main.getProjection().eastNorth2latlon(en);
|
---|
126 | if (debug) {
|
---|
127 | System.out.println(ll);
|
---|
128 | }
|
---|
129 | assertTrue("Lausanne", Math.abs(ll.lat() - 46.518) < 0.00001);
|
---|
130 | assertTrue("Lausanne", Math.abs(ll.lon() - 6.567) < 0.00001);
|
---|
131 |
|
---|
132 | en = new EastNorth(685544.16, 292782.91);
|
---|
133 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
134 | if (debug) {
|
---|
135 | System.out.println(ll);
|
---|
136 | }
|
---|
137 | assertTrue("Schafouse", Math.abs(ll.lat() - 47.78) < 0.00001);
|
---|
138 | assertTrue("Schafouse", Math.abs(ll.lon() - 8.58) < 0.00001);
|
---|
139 |
|
---|
140 | en = new EastNorth(833068.04, 163265.39);
|
---|
141 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
142 | if (debug) {
|
---|
143 | System.out.println(ll);
|
---|
144 | }
|
---|
145 | assertTrue("Grinson", Math.abs(ll.lat() - 46.58) < 0.00001);
|
---|
146 | assertTrue("Grinson", Math.abs(ll.lon() - 10.48) < 0.00001);
|
---|
147 |
|
---|
148 | en = new EastNorth(600000.0, 200000.0);
|
---|
149 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
150 | if (debug) {
|
---|
151 | System.out.println(ll);
|
---|
152 | }
|
---|
153 | assertTrue("Berne", Math.abs(ll.lat() - (46.0 + 57.0 / 60 + 3.89813884505 / 3600)) < 0.00001);
|
---|
154 | assertTrue("Berne", Math.abs(ll.lon() - (7.0 + 26.0 / 60 + 19.076595154147 / 3600)) < 0.00001);
|
---|
155 |
|
---|
156 | en = new EastNorth(700000.0, 100000.0);
|
---|
157 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
158 | if (debug) {
|
---|
159 | System.out.println(ll);
|
---|
160 | }
|
---|
161 | assertTrue("Ref", Math.abs(ll.lat() - (46.0 + 2.0 / 60 + 38.87 / 3600)) < 0.00001);
|
---|
162 | assertTrue("Ref", Math.abs(ll.lon() - (8.0 + 43.0 / 60 + 49.79 / 3600)) < 0.00001);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Send and return should have less than 2mm of difference.
|
---|
167 | */
|
---|
168 | @Test
|
---|
169 | public void c_sendandreturn_test() {
|
---|
170 | EastNorth en = new EastNorth(533111.69, 152227.85);
|
---|
171 | LatLon ll = Main.getProjection().eastNorth2latlon(en);
|
---|
172 | EastNorth en2 = Main.getProjection().latlon2eastNorth(ll);
|
---|
173 | if (debug) {
|
---|
174 | System.out.println(en.east() - en2.east());
|
---|
175 | }
|
---|
176 | if (debug) {
|
---|
177 | System.out.println(en.north() - en2.north());
|
---|
178 | }
|
---|
179 | assertTrue("Lausanne", Math.abs(en.east() - en2.east()) < 0.002);
|
---|
180 | assertTrue("Lausanne", Math.abs(en.north() - en2.north()) < 0.002);
|
---|
181 |
|
---|
182 | en = new EastNorth(685544.16, 292782.91);
|
---|
183 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
184 | en2 = Main.getProjection().latlon2eastNorth(ll);
|
---|
185 | if (debug) {
|
---|
186 | System.out.println(en.east() - en2.east());
|
---|
187 | }
|
---|
188 | if (debug) {
|
---|
189 | System.out.println(en.north() - en2.north());
|
---|
190 | }
|
---|
191 | assertTrue("Schafouse", Math.abs(en.east() - en2.east()) < 0.002);
|
---|
192 | assertTrue("Schafouse", Math.abs(en.north() - en2.north()) < 0.002);
|
---|
193 |
|
---|
194 | en = new EastNorth(833068.04, 163265.39);
|
---|
195 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
196 | en2 = Main.getProjection().latlon2eastNorth(ll);
|
---|
197 | if (debug) {
|
---|
198 | System.out.println(en.east() - en2.east());
|
---|
199 | }
|
---|
200 | if (debug) {
|
---|
201 | System.out.println(en.north() - en2.north());
|
---|
202 | }
|
---|
203 | assertTrue("Grinson", Math.abs(en.east() - en2.east()) < 0.002);
|
---|
204 | assertTrue("Grinson", Math.abs(en.north() - en2.north()) < 0.002);
|
---|
205 |
|
---|
206 | en = new EastNorth(600000.0, 200000.0);
|
---|
207 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
208 | en2 = Main.getProjection().latlon2eastNorth(ll);
|
---|
209 | if (debug) {
|
---|
210 | System.out.println(en.east() - en2.east());
|
---|
211 | }
|
---|
212 | if (debug) {
|
---|
213 | System.out.println(en.north() - en2.north());
|
---|
214 | }
|
---|
215 | assertTrue("Berne", Math.abs(en.east() - en2.east()) < 0.002);
|
---|
216 | assertTrue("Berne", Math.abs(en.north() - en2.north()) < 0.002);
|
---|
217 |
|
---|
218 | en = new EastNorth(700000.0, 100000.0);
|
---|
219 | ll = Main.getProjection().eastNorth2latlon(en);
|
---|
220 | en2 = Main.getProjection().latlon2eastNorth(ll);
|
---|
221 | if (debug) {
|
---|
222 | System.out.println(en.east() - en2.east());
|
---|
223 | }
|
---|
224 | if (debug) {
|
---|
225 | System.out.println(en.north() - en2.north());
|
---|
226 | }
|
---|
227 | assertTrue("Ref", Math.abs(en.east() - en2.east()) < 0.002);
|
---|
228 | assertTrue("Ref", Math.abs(en.north() - en2.north()) < 0.002);
|
---|
229 | }
|
---|
230 | }
|
---|