source: josm/trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java@ 19182

Last change on this file since 19182 was 19182, checked in by stoecker, 6 months ago

reduce test threshold to get MacOS working

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import static org.junit.jupiter.api.Assertions.fail;
5
6import java.io.BufferedReader;
7import java.io.BufferedWriter;
8import java.io.File;
9import java.io.IOException;
10import java.io.OutputStreamWriter;
11import java.nio.charset.StandardCharsets;
12import java.nio.file.Files;
13import java.nio.file.Paths;
14import java.security.SecureRandom;
15import java.util.ArrayList;
16import java.util.List;
17import java.util.Map;
18import java.util.Random;
19import java.util.Set;
20import java.util.TreeSet;
21import java.util.stream.Collectors;
22
23import org.junit.jupiter.api.Test;
24import org.openstreetmap.josm.JOSMFixture;
25import org.openstreetmap.josm.data.Bounds;
26import org.openstreetmap.josm.data.coor.EastNorth;
27import org.openstreetmap.josm.data.coor.LatLon;
28import org.openstreetmap.josm.testutils.annotations.ProjectionNadGrids;
29import org.openstreetmap.josm.tools.Pair;
30
31/**
32 * This test is used to monitor changes in projection code.
33 * <p>
34 * It keeps a record of test data in the file nodist/data/projection/projection-regression-test-data.
35 * This record is generated from the current Projection classes available in JOSM. It needs to
36 * be updated, whenever a projection is added / removed or an algorithm is changed, such that
37 * the computed values are numerically different. There is no error threshold, every change is reported.
38 * <p>
39 * So when this test fails, first check if the change is intended. Then update the regression
40 * test data, by running the main method of this class and commit the new data file.
41 */
42class ProjectionRegressionTest {
43
44 private static final String PROJECTION_DATA_FILE = "nodist/data/projection/projection-regression-test-data";
45
46 private static final class TestData {
47 public String code;
48 public LatLon ll;
49 public EastNorth en;
50 public LatLon ll2;
51 }
52
53 /**
54 * Program entry point to update reference projection file.
55 * @param args not used
56 * @throws IOException if any I/O errors occurs
57 */
58 public static void main(String[] args) throws IOException {
59 JOSMFixture.createUnitTestFixture().init();
60
61 Map<String, Projection> supportedCodesMap = Projections.getAllProjectionCodes().stream()
62 .collect(Collectors.toMap(code -> code, Projections::getProjectionByCode));
63
64 List<TestData> prevData = new ArrayList<>();
65 if (new File(PROJECTION_DATA_FILE).exists()) {
66 prevData = readData();
67 }
68 Map<String, TestData> prevCodesMap = prevData.stream()
69 .collect(Collectors.toMap(data -> data.code, data -> data));
70
71 Set<String> codesToWrite = new TreeSet<>(supportedCodesMap.keySet());
72 prevData.stream()
73 .filter(data -> supportedCodesMap.containsKey(data.code)).map(data -> data.code)
74 .forEach(codesToWrite::add);
75
76 Random rand = new SecureRandom();
77 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
78 Files.newOutputStream(Paths.get(PROJECTION_DATA_FILE)), StandardCharsets.UTF_8))) {
79 out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
80 out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
81 for (String code : codesToWrite) {
82 Projection proj = supportedCodesMap.get(code);
83 Bounds b = proj.getWorldBoundsLatLon();
84 double lat, lon;
85 TestData prev = prevCodesMap.get(proj.toCode());
86 if (prev != null) {
87 lat = prev.ll.lat();
88 lon = prev.ll.lon();
89 } else {
90 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
91 lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
92 }
93 EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
94 LatLon ll2 = proj.eastNorth2latlon(en);
95 out.write(String.format(
96 "%s%n ll %s %s%n en %s %s%n ll2 %s %s%n", proj.toCode(), lat, lon, en.east(), en.north(), ll2.lat(), ll2.lon()));
97 }
98 }
99 System.out.println("Update successful.");
100 }
101
102 private static List<TestData> readData() throws IOException {
103 try (BufferedReader in = Files.newBufferedReader(Paths.get(PROJECTION_DATA_FILE), StandardCharsets.UTF_8)) {
104 List<TestData> result = new ArrayList<>();
105 String line;
106 while ((line = in.readLine()) != null) {
107 if (line.startsWith("#")) {
108 continue;
109 }
110 TestData next = new TestData();
111
112 Pair<Double, Double> ll = readLine("ll", in.readLine());
113 Pair<Double, Double> en = readLine("en", in.readLine());
114 Pair<Double, Double> ll2 = readLine("ll2", in.readLine());
115
116 next.code = line;
117 next.ll = new LatLon(ll.a, ll.b);
118 next.en = new EastNorth(en.a, en.b);
119 next.ll2 = new LatLon(ll2.a, ll2.b);
120
121 result.add(next);
122 }
123 return result;
124 }
125 }
126
127 private static Pair<Double, Double> readLine(String expectedName, String input) {
128 String[] fields = input.trim().split("[ ]+", -1);
129 if (fields.length != 3) throw new AssertionError();
130 if (!fields[0].equals(expectedName)) throw new AssertionError();
131 double a = Double.parseDouble(fields[1]);
132 double b = Double.parseDouble(fields[2]);
133 return Pair.create(a, b);
134 }
135
136 /**
137 * Non-regression unit test.
138 * @throws IOException if any I/O error occurs
139 */
140 @ProjectionNadGrids
141 @Test
142 void testNonRegression() throws IOException {
143 List<TestData> allData = readData();
144 Set<String> dataCodes = allData.stream().map(data -> data.code).collect(Collectors.toSet());
145
146 StringBuilder fail = new StringBuilder();
147
148 for (String code : Projections.getAllProjectionCodes()) {
149 if (!dataCodes.contains(code)) {
150 fail.append("Did not find projection ").append(code).append(" in test data!\n");
151 }
152 }
153
154 for (TestData data : allData) {
155 Projection proj = Projections.getProjectionByCode(data.code);
156 if (proj == null) {
157 fail.append("Projection ").append(data.code).append(" from test data was not found!\n");
158 continue;
159 }
160 EastNorth en = proj.latlon2eastNorth(data.ll);
161 LatLon ll2 = proj.eastNorth2latlon(data.en);
162 if (!equalsJava9(en, data.en)) {
163 String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
164 " expected: eastnorth(%s,%s),%n" +
165 " but got: eastnorth(%s,%s)!%n",
166 proj, data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
167 fail.append(error);
168 }
169 if (!equalsJava9(ll2, data.ll2)) {
170 String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
171 " expected: latlon(%s,%s),%n" +
172 " but got: latlon(%s,%s)!%n",
173 proj, data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
174 fail.append(error);
175 }
176 }
177
178 if (fail.length() > 0) {
179 System.err.println(fail);
180 fail(fail.toString());
181 }
182 }
183
184 private static boolean equalsDoubleMaxUlp(double d1, double d2) {
185 // Due to error accumulation in projection computation, the difference can reach hundreds of ULPs
186 // The worst error is 1168 ULP (followed by 816 ULP then 512 ULP) with:
187 // NAD83 / Colorado South (EPSG:26955): Projecting latlon(32.24604527892822,-125.93039495227096):
188 // expected: eastnorth(-1004398.8994415681,24167.8944844745),
189 // but got: eastnorth(-1004398.8994415683,24167.894484478747)!
190 // MacOS has higher errors, otherwise 1200 would be enough
191 return Math.abs(d1 - d2) <= 1700 * Math.ulp(d1);
192 }
193
194 private static boolean equalsJava9(EastNorth en1, EastNorth en2) {
195 return equalsDoubleMaxUlp(en1.east(), en2.east()) &&
196 equalsDoubleMaxUlp(en1.north(), en2.north());
197 }
198
199 private static boolean equalsJava9(LatLon ll1, LatLon ll2) {
200 return equalsDoubleMaxUlp(ll1.lat(), ll2.lat()) &&
201 equalsDoubleMaxUlp(ll1.lon(), ll2.lon());
202 }
203}
Note: See TracBrowser for help on using the repository browser.