source: josm/trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionTest.java@ 11241

Last change on this file since 11241 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: 5.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import java.util.Arrays;
5import java.util.Collection;
6import java.util.HashSet;
7import java.util.Random;
8
9import org.junit.Assert;
10import org.junit.Test;
11import org.openstreetmap.josm.data.Bounds;
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.data.coor.LatLon;
14
15/**
16 * Unit tests for class {@link Projection}.
17 */
18public class ProjectionTest {
19
20 private static Random rand = new Random(System.currentTimeMillis());
21
22 boolean error;
23 String text;
24
25 @Test
26 public void testProjections() {
27 error = false;
28 text = "";
29
30 testProjection(Projections.getProjectionByCode("EPSG:4326")); // WGS 84
31 testProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
32 testProjection(Projections.getProjectionByCode("EPSG:3301")); // Lambert EST
33
34 for (int i = 0; i <= 3; ++i) {
35 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(27561+i))); // Lambert 4 Zones France
36 }
37
38 for (int i = 0; i <= 4; ++i) {
39 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(2176+i))); // PUWG Poland
40 }
41
42 testProjection(Projections.getProjectionByCode("EPSG:21781")); // Swiss grid
43
44 for (int i = 0; i <= 60; ++i) {
45 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(32601+i))); // UTM North
46 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(32701+i))); // UTM South
47 }
48
49 for (String c : Arrays.asList("2969", "2970", "2972", "2973")) {
50 testProjection(Projections.getProjectionByCode("EPSG:"+c)); // UTM France DOM
51 }
52
53 for (int i = 0; i <= 8; ++i) {
54 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(3942+i))); // Lambert CC9 Zones France
55 }
56
57 if (error) {
58 System.err.println(text);
59 Assert.fail();
60 }
61 }
62
63 private void testProjection(Projection p) {
64 if (p != null) {
65 double maxErrLat = 0, maxErrLon = 0;
66 Bounds b = p.getWorldBoundsLatLon();
67
68 text += String.format("*** %s %s%n", p.toString(), p.toCode());
69 for (int num = 0; num < 1000; ++num) {
70
71 LatLon ll0 = random(b);
72 LatLon ll = ll0;
73
74 for (int i = 0; i < 10; ++i) {
75 EastNorth en = p.latlon2eastNorth(ll);
76 ll = p.eastNorth2latlon(en);
77 }
78 maxErrLat = Math.max(maxErrLat, Math.abs(ll0.lat() - ll.lat()));
79 maxErrLon = Math.max(maxErrLon, Math.abs(ll0.lon() - ll.lon()));
80 }
81
82 String mark = "";
83 if (maxErrLat + maxErrLon > 1e-5) {
84 mark = "--FAILED-- ";
85 error = true;
86 }
87 text += String.format("%s errorLat: %s errorLon: %s%n", mark, maxErrLat, maxErrLon);
88 }
89 }
90
91 private LatLon random(Bounds b) {
92 for (int i = 0; i < 20; i++) {
93 double lat = rand.nextDouble() * (b.getMax().lat() - b.getMin().lat()) + b.getMin().lat();
94 double lon = rand.nextDouble() * (b.getMax().lon() - b.getMin().lon()) + b.getMin().lon();
95 LatLon result = new LatLon(lat, lon);
96 if (result.isValid()) return result;
97 }
98 throw new RuntimeException();
99 }
100
101 boolean error2;
102 String text2;
103 Collection<String> projIds;
104
105 @Test
106 public void testProjs() {
107 error2 = false;
108 text2 = "";
109
110 projIds = new HashSet<>(Projections.getAllBaseProjectionIds());
111
112 final double EPS = 1e-6;
113 testProj("lonlat", EPS, "");
114 testProj("lcc", EPS, "+lat_0=34");
115 testProj("lcc", EPS, "+lat_1=87 +lat_2=83.6 +lat_0=85.43");
116 testProj("somerc", EPS, "+lat_0=47");
117 testProj("tmerc", 1e-5, "+bounds=-2.5,-89,2.5,89");
118 testProj("tmerc", 2e-3, "");
119 testProj("sterea", EPS, "+lat_0=52");
120 testProj("aea", EPS, "+lat_1=27.5 +lat_2=35 +lat_0=18");
121 testProj("stere", 1e-5, "+lat_0=-90 +lat_ts=-70");
122 testProj("stere", 1e-5, "+lat_0=90 +lat_ts=90");
123 testProj("omerc", EPS, "+lat_0=4 +lonc=115 +alpha=53 +no_uoff +gamma=53.130 +bounds=112,4,116,7");
124 testProj("cass", 1e-3, "+lat_0=11 +bounds=-1.0,-89,1.0,89");
125 testProj("laea", 3e-3, "+lat_0=34");
126 testProj("merc", 1e-5, "");
127 testProj("sinu", 1e-4, "");
128
129 if (error2) {
130 System.err.println(text2);
131 Assert.fail();
132 }
133 Assert.assertTrue("missing test: "+projIds, projIds.isEmpty());
134 }
135
136 private void testProj(String id, double eps, String prefAdd) {
137 final int NUM_IT = 1000;
138 projIds.remove(id);
139 String pref = String.format("+proj=%s +ellps=WGS84 +nadgrids=null "+prefAdd, id);
140 CustomProjection p = new CustomProjection();
141 try {
142 p.update(pref);
143 } catch (ProjectionConfigurationException ex) {
144 throw new RuntimeException(ex);
145 }
146 Bounds b = p.getWorldBoundsLatLon();
147 double maxDist = 0;
148 LatLon maxLatLon = null;
149 for (int i = 0; i < NUM_IT; i++) {
150 LatLon ll1 = random(b);
151 EastNorth en = p.latlon2eastNorth(ll1);
152 LatLon ll2 = p.eastNorth2latlon(en);
153 Assert.assertTrue(p.toCode() + " at " + ll1 + " is " + ll2, ll2.isValid());
154 double dist = ll1.greatCircleDistance(ll2);
155 if (dist > eps) {
156 error2 = true;
157 if (dist > maxDist) {
158 maxDist = dist;
159 maxLatLon = ll1;
160 }
161 }
162 }
163 if (maxDist > 0) {
164 text2 += id + ": dist " + maxDist + " at " + maxLatLon + "\n";
165 }
166 }
167}
Note: See TracBrowser for help on using the repository browser.