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

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

see #15229 - deprecate all Main methods related to projections. New ProjectionRegistry class

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