1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Random;
|
---|
8 |
|
---|
9 | import org.junit.Assert;
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Unit tests for class {@link Projection}.
|
---|
17 | */
|
---|
18 | public 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 projections() {
|
---|
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 projs() {
|
---|
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("josm:smerc", EPS, "");
|
---|
115 | testProj("lcc", EPS, "+lat_0=34");
|
---|
116 | testProj("lcc", EPS, "+lat_1=87 +lat_2=83.6 +lat_0=85.43");
|
---|
117 | testProj("somerc", EPS, "+lat_0=47");
|
---|
118 | testProj("tmerc", 2e-3, "");
|
---|
119 | testProj("sterea", EPS, "+lat_0=52");
|
---|
120 |
|
---|
121 | if (error2) {
|
---|
122 | System.err.println(text2);
|
---|
123 | Assert.fail();
|
---|
124 | }
|
---|
125 | Assert.assertTrue("missing test: "+projIds, projIds.isEmpty());
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void testProj(String id, double eps, String prefAdd) {
|
---|
129 | final int NUM_IT = 1000;
|
---|
130 | projIds.remove(id);
|
---|
131 | String pref = String.format("+proj=%s +ellps=WGS84 +nadgrids=null "+prefAdd, id);
|
---|
132 | CustomProjection p = new CustomProjection();
|
---|
133 | try {
|
---|
134 | p.update(pref);
|
---|
135 | } catch (ProjectionConfigurationException ex) {
|
---|
136 | throw new RuntimeException(ex);
|
---|
137 | }
|
---|
138 | Bounds b = p.getWorldBoundsLatLon();
|
---|
139 | for (int i = 0; i < NUM_IT; i++) {
|
---|
140 | LatLon ll1 = random(b);
|
---|
141 | EastNorth en = p.latlon2eastNorth(ll1);
|
---|
142 | LatLon ll2 = p.eastNorth2latlon(en);
|
---|
143 | Assert.assertTrue(p.toCode() + " at " + ll1 + " is " + ll2, ll2.isValid());
|
---|
144 | double dist = ll1.greatCircleDistance(ll2);
|
---|
145 | if (dist > eps) {
|
---|
146 | error2 = true;
|
---|
147 | text2 += id + ": dist " + dist + " at " + ll1 + "\n";
|
---|
148 | return;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|