1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.BufferedWriter;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileInputStream;
|
---|
8 | import java.io.FileNotFoundException;
|
---|
9 | import java.io.FileOutputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStreamReader;
|
---|
12 | import java.io.OutputStreamWriter;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.HashMap;
|
---|
16 | import java.util.HashSet;
|
---|
17 | import java.util.LinkedHashSet;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Map;
|
---|
20 | import java.util.Random;
|
---|
21 | import java.util.Set;
|
---|
22 |
|
---|
23 | import org.junit.BeforeClass;
|
---|
24 | import org.junit.Test;
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.data.Bounds;
|
---|
27 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
28 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
|
---|
30 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
31 | import org.openstreetmap.josm.tools.Pair;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
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 | */
|
---|
45 | public 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 (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
|
---|
61 | for (String code : pc.allCodes()) {
|
---|
62 | Collection<String> pref = pc.getPreferencesFromCode(code);
|
---|
63 | pc.setPreferences(pref);
|
---|
64 | Projection p = pc.getProjection();
|
---|
65 | supportedCodesMap.put(code, p);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | List<TestData> prevData = new ArrayList<>();
|
---|
70 | if (new File(PROJECTION_DATA_FILE).exists()) {
|
---|
71 | prevData = readData();
|
---|
72 | }
|
---|
73 | Map<String,TestData> prevCodesMap = new HashMap<>();
|
---|
74 | for (TestData data : prevData) {
|
---|
75 | prevCodesMap.put(data.code, data);
|
---|
76 | }
|
---|
77 |
|
---|
78 | Set<String> codesToWrite = new LinkedHashSet<>();
|
---|
79 | for (TestData data : prevData) {
|
---|
80 | if (supportedCodesMap.containsKey(data.code)) {
|
---|
81 | codesToWrite.add(data.code);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | for (String code : supportedCodesMap.keySet()) {
|
---|
85 | if (!codesToWrite.contains(code)) {
|
---|
86 | codesToWrite.add(code);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | Random rand = new Random();
|
---|
91 | try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(PROJECTION_DATA_FILE), Utils.UTF_8))) {
|
---|
92 | out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
|
---|
93 | out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
|
---|
94 | for (String code : codesToWrite) {
|
---|
95 | Projection proj = supportedCodesMap.get(code);
|
---|
96 | Bounds b = proj.getWorldBoundsLatLon();
|
---|
97 | double lat, lon;
|
---|
98 | TestData prev = prevCodesMap.get(proj.toCode());
|
---|
99 | if (prev != null) {
|
---|
100 | lat = prev.ll.lat();
|
---|
101 | lon = prev.ll.lon();
|
---|
102 | } else {
|
---|
103 | lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
|
---|
104 | lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
|
---|
105 | }
|
---|
106 | EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
|
---|
107 | LatLon ll2 = proj.eastNorth2latlon(en);
|
---|
108 | 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()));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | private static List<TestData> readData() throws IOException, FileNotFoundException {
|
---|
114 | try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(PROJECTION_DATA_FILE), Utils.UTF_8))) {
|
---|
115 | List<TestData> result = new ArrayList<>();
|
---|
116 | String line;
|
---|
117 | while ((line = in.readLine()) != null) {
|
---|
118 | if (line.startsWith("#")) {
|
---|
119 | continue;
|
---|
120 | }
|
---|
121 | TestData next = new TestData();
|
---|
122 |
|
---|
123 | Pair<Double,Double> ll = readLine("ll", in.readLine());
|
---|
124 | Pair<Double,Double> en = readLine("en", in.readLine());
|
---|
125 | Pair<Double,Double> ll2 = readLine("ll2", in.readLine());
|
---|
126 |
|
---|
127 | next.code = line;
|
---|
128 | next.ll = new LatLon(ll.a, ll.b);
|
---|
129 | next.en = new EastNorth(en.a, en.b);
|
---|
130 | next.ll2 = new LatLon(ll2.a, ll2.b);
|
---|
131 |
|
---|
132 | result.add(next);
|
---|
133 | }
|
---|
134 | return result;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static Pair<Double,Double> readLine(String expectedName, String input) {
|
---|
139 | String[] fields = input.trim().split("[ ]+");
|
---|
140 | if (fields.length != 3) throw new AssertionError();
|
---|
141 | if (!fields[0].equals(expectedName)) throw new AssertionError();
|
---|
142 | double a = Double.parseDouble(fields[1]);
|
---|
143 | double b = Double.parseDouble(fields[2]);
|
---|
144 | return Pair.create(a, b);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Setup test.
|
---|
149 | */
|
---|
150 | @BeforeClass
|
---|
151 | public static void setUp() {
|
---|
152 | Main.initApplicationPreferences();
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Test
|
---|
156 | public void regressionTest() throws IOException, FileNotFoundException {
|
---|
157 | List<TestData> allData = readData();
|
---|
158 | Set<String> dataCodes = new HashSet<>();
|
---|
159 | for (TestData data : allData) {
|
---|
160 | dataCodes.add(data.code);
|
---|
161 | }
|
---|
162 |
|
---|
163 | StringBuilder fail = new StringBuilder();
|
---|
164 |
|
---|
165 | for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
|
---|
166 | for (String code : pc.allCodes()) {
|
---|
167 | if (!dataCodes.contains(code)) {
|
---|
168 | fail.append("Did not find projection "+code+" in test data!\n");
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | for (TestData data : allData) {
|
---|
174 | Projection proj = Projections.getProjectionByCode(data.code);
|
---|
175 | if (proj == null) {
|
---|
176 | fail.append("Projection "+data.code+" from test data was not found!\n");
|
---|
177 | continue;
|
---|
178 | }
|
---|
179 | EastNorth en = proj.latlon2eastNorth(data.ll);
|
---|
180 | if (!en.equals(data.en)) {
|
---|
181 | String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
|
---|
182 | " expected: eastnorth(%s,%s),%n" +
|
---|
183 | " but got: eastnorth(%s,%s)!%n",
|
---|
184 | proj.toString(), data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
|
---|
185 | fail.append(error);
|
---|
186 | }
|
---|
187 | LatLon ll2 = proj.eastNorth2latlon(data.en);
|
---|
188 | if (!ll2.equals(data.ll2)) {
|
---|
189 | String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
|
---|
190 | " expected: latlon(%s,%s),%n" +
|
---|
191 | " but got: latlon(%s,%s)!%n",
|
---|
192 | proj.toString(), data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
|
---|
193 | fail.append(error);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (fail.length() > 0) {
|
---|
198 | System.err.println(fail.toString());
|
---|
199 | throw new AssertionError(fail.toString());
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|