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

Last change on this file since 7944 was 7944, checked in by bastiK, 9 years ago

add regression data for new projections

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import java.io.BufferedReader;
5import java.io.BufferedWriter;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.IOException;
11import java.io.InputStreamReader;
12import java.io.OutputStreamWriter;
13import java.nio.charset.StandardCharsets;
14import java.util.ArrayList;
15import java.util.Collection;
16import java.util.HashMap;
17import java.util.HashSet;
18import java.util.LinkedHashSet;
19import java.util.List;
20import java.util.Map;
21import java.util.Random;
22import java.util.Set;
23
24import org.junit.BeforeClass;
25import org.junit.Test;
26import org.openstreetmap.josm.JOSMFixture;
27import org.openstreetmap.josm.data.Bounds;
28import org.openstreetmap.josm.data.coor.EastNorth;
29import org.openstreetmap.josm.data.coor.LatLon;
30import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
31import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
32import org.openstreetmap.josm.tools.Pair;
33
34/**
35 * This test is used to monitor changes in projection code.
36 *
37 * It keeps a record of test data in the file data_nodist/projection-regression-test-data.csv.
38 * This record is generated from the current Projection classes available in JOSM. It needs to
39 * be updated, whenever a projection is added / removed or an algorithm is changed, such that
40 * the computed values are numerically different. There is no error threshold, every change is reported.
41 *
42 * So when this test fails, first check if the change is intended. Then update the regression
43 * test data, by running the main method of this class and commit the new data file.
44 */
45public class ProjectionRegressionTest {
46
47 public static final String PROJECTION_DATA_FILE = "data_nodist/projection-regression-test-data.csv";
48
49 private static class TestData {
50 public String code;
51 public LatLon ll;
52 public EastNorth en;
53 public LatLon ll2;
54 }
55
56 public static void main(String[] args) throws IOException, FileNotFoundException {
57 setUp();
58
59 Map<String, Projection> supportedCodesMap = new HashMap<>();
60 for (String code : Projections.getAllProjectionCodes()) {
61 supportedCodesMap.put(code, Projections.getProjectionByCode(code));
62 }
63
64 List<TestData> prevData = new ArrayList<>();
65 if (new File(PROJECTION_DATA_FILE).exists()) {
66 prevData = readData();
67 }
68 Map<String,TestData> prevCodesMap = new HashMap<>();
69 for (TestData data : prevData) {
70 prevCodesMap.put(data.code, data);
71 }
72
73 Set<String> codesToWrite = new LinkedHashSet<>();
74 for (TestData data : prevData) {
75 if (supportedCodesMap.containsKey(data.code)) {
76 codesToWrite.add(data.code);
77 }
78 }
79 for (String code : supportedCodesMap.keySet()) {
80 if (!codesToWrite.contains(code)) {
81 codesToWrite.add(code);
82 }
83 }
84
85 Random rand = new Random();
86 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(PROJECTION_DATA_FILE), StandardCharsets.UTF_8))) {
87 out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
88 out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
89 for (String code : codesToWrite) {
90 Projection proj = supportedCodesMap.get(code);
91 Bounds b = proj.getWorldBoundsLatLon();
92 double lat, lon;
93 TestData prev = prevCodesMap.get(proj.toCode());
94 if (prev != null) {
95 lat = prev.ll.lat();
96 lon = prev.ll.lon();
97 } else {
98 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
99 lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
100 }
101 EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
102 LatLon ll2 = proj.eastNorth2latlon(en);
103 out.write(String.format("%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()));
104 }
105 }
106 }
107
108 private static List<TestData> readData() throws IOException, FileNotFoundException {
109 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(PROJECTION_DATA_FILE), StandardCharsets.UTF_8))) {
110 List<TestData> result = new ArrayList<>();
111 String line;
112 while ((line = in.readLine()) != null) {
113 if (line.startsWith("#")) {
114 continue;
115 }
116 TestData next = new TestData();
117
118 Pair<Double,Double> ll = readLine("ll", in.readLine());
119 Pair<Double,Double> en = readLine("en", in.readLine());
120 Pair<Double,Double> ll2 = readLine("ll2", in.readLine());
121
122 next.code = line;
123 next.ll = new LatLon(ll.a, ll.b);
124 next.en = new EastNorth(en.a, en.b);
125 next.ll2 = new LatLon(ll2.a, ll2.b);
126
127 result.add(next);
128 }
129 return result;
130 }
131 }
132
133 private static Pair<Double,Double> readLine(String expectedName, String input) {
134 String[] fields = input.trim().split("[ ]+");
135 if (fields.length != 3) throw new AssertionError();
136 if (!fields[0].equals(expectedName)) throw new AssertionError();
137 double a = Double.parseDouble(fields[1]);
138 double b = Double.parseDouble(fields[2]);
139 return Pair.create(a, b);
140 }
141
142 /**
143 * Setup test.
144 */
145 @BeforeClass
146 public static void setUp() {
147 JOSMFixture.createUnitTestFixture().init();
148 }
149
150 @Test
151 public void regressionTest() throws IOException, FileNotFoundException {
152 List<TestData> allData = readData();
153 Set<String> dataCodes = new HashSet<>();
154 for (TestData data : allData) {
155 dataCodes.add(data.code);
156 }
157
158 StringBuilder fail = new StringBuilder();
159
160 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
161 for (String code : pc.allCodes()) {
162 if (!dataCodes.contains(code)) {
163 fail.append("Did not find projection "+code+" in test data!\n");
164 }
165 }
166 }
167
168 for (TestData data : allData) {
169 Projection proj = Projections.getProjectionByCode(data.code);
170 if (proj == null) {
171 fail.append("Projection "+data.code+" from test data was not found!\n");
172 continue;
173 }
174 EastNorth en = proj.latlon2eastNorth(data.ll);
175 if (!en.equals(data.en)) {
176 String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
177 " expected: eastnorth(%s,%s),%n" +
178 " but got: eastnorth(%s,%s)!%n",
179 proj.toString(), data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
180 fail.append(error);
181 }
182 LatLon ll2 = proj.eastNorth2latlon(data.en);
183 if (!ll2.equals(data.ll2)) {
184 String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
185 " expected: latlon(%s,%s),%n" +
186 " but got: latlon(%s,%s)!%n",
187 proj.toString(), data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
188 fail.append(error);
189 }
190 }
191
192 if (fail.length() > 0) {
193 System.err.println(fail.toString());
194 throw new AssertionError(fail.toString());
195 }
196 }
197}
Note: See TracBrowser for help on using the repository browser.