1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertSame;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 |
|
---|
7 | import org.junit.jupiter.api.BeforeAll;
|
---|
8 | import org.junit.jupiter.api.Test;
|
---|
9 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
11 | import org.openstreetmap.josm.testutils.annotations.ProjectionNadGrids;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Unit tests for the Swiss projection grid.
|
---|
15 | */
|
---|
16 | @ProjectionNadGrids
|
---|
17 | class SwissGridTest {
|
---|
18 | private static final String SWISS_EPSG_CODE = "EPSG:21781";
|
---|
19 | private final boolean debug = false;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Setup test.
|
---|
23 | */
|
---|
24 | @BeforeAll
|
---|
25 | public static void setUp() {
|
---|
26 | ProjectionRegistry.setProjection(Projections.getProjectionByCode(SWISS_EPSG_CODE)); // Swiss grid
|
---|
27 | }
|
---|
28 |
|
---|
29 | // CHECKSTYLE.OFF: LineLength
|
---|
30 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * source: https://www.swisstopo.admin.ch/content/swisstopo-internet/en/topics/survey/reference-systems/projections/_jcr_content/contentPar/tabs/items/dokumente_publikatio/tabPar/downloadlist/downloadItems/463_1459341821844.download/refsyse.pdf
|
---|
34 | */
|
---|
35 | ProjData[] data = {
|
---|
36 | new ProjData("Zimmerwald", d(7, 27, 54.983506), d(46, 52, 37.540562), 947.149, 602030.680, 191775.030, 897.915),
|
---|
37 | new ProjData("Chrischona", d(7, 40, 6.983077), d(47, 34, 1.385301), 504.935, 617306.300, 268507.300, 456.064),
|
---|
38 | new ProjData("Pfaender", d(9, 47, 3.697723), d(47, 30, 55.172797), 1089.372, 776668.105, 265372.681, 1042.624),
|
---|
39 | new ProjData("La Givrine", d(6, 6, 7.326361), d(46, 27, 14.690021), 1258.274, 497313.292, 145625.438, 1207.434),
|
---|
40 | new ProjData("Monte Generoso", d(9, 1, 16.389053), d(45, 55, 45.438020), 1685.027, 722758.810, 87649.670, 1636.600) };
|
---|
41 |
|
---|
42 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
43 | // CHECKSTYLE.ON: LineLength
|
---|
44 |
|
---|
45 | private double d(double deg, double min, double sec) {
|
---|
46 | return deg + min / 60. + sec / 3600.;
|
---|
47 | }
|
---|
48 |
|
---|
49 | private static class ProjData {
|
---|
50 | public String name;
|
---|
51 | public LatLon ll;
|
---|
52 | public EastNorth en;
|
---|
53 |
|
---|
54 | ProjData(String name, double lon, double lat, double h1, double x, double y, double h2) {
|
---|
55 | this.name = name;
|
---|
56 | ll = new LatLon(lat, lon);
|
---|
57 | en = new EastNorth(x, y);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static final double EPSILON_ACCURATE = 0.05;
|
---|
62 |
|
---|
63 | private void projReferenceTest(final double epsilon) {
|
---|
64 | Projection swiss = Projections.getProjectionByCode("EPSG:21781"); // Swiss grid
|
---|
65 | StringBuilder errs = new StringBuilder();
|
---|
66 | for (ProjData pd : data) {
|
---|
67 | EastNorth en2 = swiss.latlon2eastNorth(pd.ll);
|
---|
68 | if (Math.abs(pd.en.east() - en2.east()) > epsilon || Math.abs(pd.en.north() - en2.north()) > epsilon) {
|
---|
69 | errs.append(String.format("%s should be: %s but is: %s%n", pd.name, pd.en, en2));
|
---|
70 | }
|
---|
71 | }
|
---|
72 | assertSame(errs.length(), 0, errs::toString);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Test projection accuracy.
|
---|
77 | */
|
---|
78 | @Test
|
---|
79 | void testProjReferenceTestAccurate() {
|
---|
80 | projReferenceTest(EPSILON_ACCURATE);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Unit test A: lat/lon => east/north
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testAlatlon2eastNorth() {
|
---|
88 | LatLon ll = new LatLon(46.518, 6.567);
|
---|
89 | EastNorth en = ProjectionRegistry.getProjection().latlon2eastNorth(ll);
|
---|
90 | if (debug) {
|
---|
91 | System.out.println(en);
|
---|
92 | }
|
---|
93 | assertTrue(Math.abs(en.east() - 533112.13) < 0.1, "Lausanne");
|
---|
94 | assertTrue(Math.abs(en.north() - 152227.35) < 0.1, "Lausanne");
|
---|
95 |
|
---|
96 | ll = new LatLon(47.78, 8.58);
|
---|
97 | en = ProjectionRegistry.getProjection().latlon2eastNorth(ll);
|
---|
98 | if (debug) {
|
---|
99 | System.out.println(en);
|
---|
100 | }
|
---|
101 | assertTrue(Math.abs(en.east() - 685542.97) < 0.1, "Schafouse");
|
---|
102 | assertTrue(Math.abs(en.north() - 292783.21) < 0.1, "Schafouse");
|
---|
103 |
|
---|
104 | ll = new LatLon(46.58, 10.48);
|
---|
105 | en = ProjectionRegistry.getProjection().latlon2eastNorth(ll);
|
---|
106 | if (debug) {
|
---|
107 | System.out.println(en);
|
---|
108 | }
|
---|
109 | assertTrue(Math.abs(en.east() - 833066.95) < 0.1, "Grinson");
|
---|
110 | assertTrue(Math.abs(en.north() - 163265.32) < 0.1, "Grinson");
|
---|
111 |
|
---|
112 | ll = new LatLon(46.0 + 57.0 / 60 + 3.89813884505 / 3600, 7.0 + 26.0 / 60 + 19.076595154147 / 3600);
|
---|
113 | en = ProjectionRegistry.getProjection().latlon2eastNorth(ll);
|
---|
114 | if (debug) {
|
---|
115 | System.out.println(en);
|
---|
116 | }
|
---|
117 | assertTrue(Math.abs(en.east() - 600000.0) < 0.1, "Berne");
|
---|
118 | assertTrue(Math.abs(en.north() - 200000.0) < 0.1, "Berne");
|
---|
119 |
|
---|
120 | // http://geodesy.geo.admin.ch/reframe/lv03towgs84?easting=700000&northing=100000
|
---|
121 | ll = new LatLon(46.04412093223244, 8.730497366167727);
|
---|
122 | en = ProjectionRegistry.getProjection().latlon2eastNorth(ll);
|
---|
123 | if (debug) {
|
---|
124 | System.out.println(en);
|
---|
125 | }
|
---|
126 | assertTrue(Math.abs(en.east() - 700000.0) < 0.1, "Ref");
|
---|
127 | assertTrue(Math.abs(en.north() - 100000.0) < 0.1, "Ref");
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Unit test B: east/north => lat/lon
|
---|
132 | */
|
---|
133 | @Test
|
---|
134 | void testBeastNorth2latlon() {
|
---|
135 | EastNorth en = new EastNorth(533112.13, 152227.35);
|
---|
136 | LatLon ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
137 | if (debug) {
|
---|
138 | System.out.println(ll);
|
---|
139 | }
|
---|
140 | assertTrue(Math.abs(ll.lat() - 46.518) < 0.00001, "Lausanne");
|
---|
141 | assertTrue(Math.abs(ll.lon() - 6.567) < 0.00001, "Lausanne");
|
---|
142 |
|
---|
143 | en = new EastNorth(685542.97, 292783.21);
|
---|
144 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
145 | if (debug) {
|
---|
146 | System.out.println(ll);
|
---|
147 | }
|
---|
148 | assertTrue(Math.abs(ll.lat() - 47.78) < 0.00001, "Schafouse");
|
---|
149 | assertTrue(Math.abs(ll.lon() - 8.58) < 0.00001, "Schafouse");
|
---|
150 |
|
---|
151 | en = new EastNorth(833066.95, 163265.32);
|
---|
152 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
153 | if (debug) {
|
---|
154 | System.out.println(ll);
|
---|
155 | }
|
---|
156 | assertTrue(Math.abs(ll.lat() - 46.58) < 0.00001, "Grinson");
|
---|
157 | assertTrue(Math.abs(ll.lon() - 10.48) < 0.00001, "Grinson");
|
---|
158 |
|
---|
159 | en = new EastNorth(600000.0, 200000.0);
|
---|
160 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
161 | if (debug) {
|
---|
162 | System.out.println(ll);
|
---|
163 | }
|
---|
164 | assertTrue(Math.abs(ll.lat() - (46.0 + 57.0 / 60 + 3.89813884505 / 3600)) < 0.00001, "Berne");
|
---|
165 | assertTrue(Math.abs(ll.lon() - (7.0 + 26.0 / 60 + 19.076595154147 / 3600)) < 0.00001, "Berne");
|
---|
166 |
|
---|
167 | // http://geodesy.geo.admin.ch/reframe/lv03towgs84?easting=700000&northing=100000
|
---|
168 | en = new EastNorth(700000.0, 100000.0);
|
---|
169 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
170 | if (debug) {
|
---|
171 | System.out.println(ll);
|
---|
172 | }
|
---|
173 | assertTrue(Math.abs(ll.lat() - 46.04412093223244) < 0.00001, "Ref");
|
---|
174 | assertTrue(Math.abs(ll.lon() - 8.730497366167727) < 0.00001, "Ref");
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Send and return should have less than 2mm of difference.
|
---|
179 | */
|
---|
180 | @Test
|
---|
181 | void testCsendandreturn() {
|
---|
182 | EastNorth en = new EastNorth(533111.69, 152227.85);
|
---|
183 | LatLon ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
184 | EastNorth en2 = ProjectionRegistry.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(Math.abs(en.east() - en2.east()) < 0.002, "Lausanne");
|
---|
192 | assertTrue(Math.abs(en.north() - en2.north()) < 0.002, "Lausanne");
|
---|
193 |
|
---|
194 | en = new EastNorth(685544.16, 292782.91);
|
---|
195 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
196 | en2 = ProjectionRegistry.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(Math.abs(en.east() - en2.east()) < 0.002, "Schafouse");
|
---|
204 | assertTrue(Math.abs(en.north() - en2.north()) < 0.002, "Schafouse");
|
---|
205 |
|
---|
206 | en = new EastNorth(833068.04, 163265.39);
|
---|
207 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
208 | en2 = ProjectionRegistry.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(Math.abs(en.east() - en2.east()) < 0.002, "Grinson");
|
---|
216 | assertTrue(Math.abs(en.north() - en2.north()) < 0.002, "Grinson");
|
---|
217 |
|
---|
218 | en = new EastNorth(600000.0, 200000.0);
|
---|
219 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
220 | en2 = ProjectionRegistry.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(Math.abs(en.east() - en2.east()) < 0.002, "Berne");
|
---|
228 | assertTrue(Math.abs(en.north() - en2.north()) < 0.002, "Berne");
|
---|
229 |
|
---|
230 | en = new EastNorth(700000.0, 100000.0);
|
---|
231 | ll = ProjectionRegistry.getProjection().eastNorth2latlon(en);
|
---|
232 | en2 = ProjectionRegistry.getProjection().latlon2eastNorth(ll);
|
---|
233 | if (debug) {
|
---|
234 | System.out.println(en.east() - en2.east());
|
---|
235 | }
|
---|
236 | if (debug) {
|
---|
237 | System.out.println(en.north() - en2.north());
|
---|
238 | }
|
---|
239 | assertTrue(Math.abs(en.east() - en2.east()) < 0.002, "Ref");
|
---|
240 | assertTrue(Math.abs(en.north() - en2.north()) < 0.002, "Ref");
|
---|
241 | }
|
---|
242 | }
|
---|