1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.BufferedWriter;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.io.InputStreamReader;
|
---|
12 | import java.io.OutputStream;
|
---|
13 | import java.io.OutputStreamWriter;
|
---|
14 | import java.lang.reflect.Constructor;
|
---|
15 | import java.lang.reflect.Method;
|
---|
16 | import java.nio.charset.StandardCharsets;
|
---|
17 | import java.nio.file.Files;
|
---|
18 | import java.nio.file.Paths;
|
---|
19 | import java.security.SecureRandom;
|
---|
20 | import java.util.ArrayList;
|
---|
21 | import java.util.Arrays;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.Collections;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.HashSet;
|
---|
26 | import java.util.LinkedHashSet;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.Map;
|
---|
29 | import java.util.Objects;
|
---|
30 | import java.util.Random;
|
---|
31 | import java.util.Set;
|
---|
32 | import java.util.TreeMap;
|
---|
33 | import java.util.TreeSet;
|
---|
34 | import java.util.concurrent.ConcurrentHashMap;
|
---|
35 | import java.util.regex.Matcher;
|
---|
36 | import java.util.regex.Pattern;
|
---|
37 |
|
---|
38 | import org.junit.jupiter.api.Test;
|
---|
39 | import org.junit.jupiter.api.Timeout;
|
---|
40 | import org.openstreetmap.josm.data.Bounds;
|
---|
41 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
42 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
43 | import org.openstreetmap.josm.gui.preferences.projection.CodeProjectionChoice;
|
---|
44 | import org.openstreetmap.josm.testutils.annotations.ProjectionNadGrids;
|
---|
45 | import org.openstreetmap.josm.tools.Pair;
|
---|
46 | import org.openstreetmap.josm.tools.PlatformManager;
|
---|
47 |
|
---|
48 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Test projections using reference data from external program.
|
---|
52 | *
|
---|
53 | * To update the reference data file <code>nodist/data/projection/projection-reference-data</code>,
|
---|
54 | * run the main method of this class. For this, you need to have the cs2cs
|
---|
55 | * program from the proj.4 library in path (or use <code>CS2CS_EXE</code> to set
|
---|
56 | * the full path of the executable). Make sure the required *.gsb grid files
|
---|
57 | * can be accessed, i.e. copy them from <code>nodist/data/projection</code> to <code>/usr/share/proj</code> or
|
---|
58 | * wherever cs2cs expects them to be placed.
|
---|
59 | * <p>
|
---|
60 | * The input parameter for the external library is <em>not</em> the projection code
|
---|
61 | * (e.g. "EPSG:25828"), but the entire definition, (e.g. "+proj=utm +zone=28 +ellps=GRS80 +nadgrids=null").
|
---|
62 | * This means the test does not verify our definitions, but the correctness
|
---|
63 | * of the algorithm, given a certain definition.
|
---|
64 | */
|
---|
65 | @ProjectionNadGrids
|
---|
66 | @Timeout(90)
|
---|
67 | class ProjectionRefTest {
|
---|
68 |
|
---|
69 | private static final String CS2CS_EXE = "cs2cs";
|
---|
70 |
|
---|
71 | private static final String REFERENCE_DATA_FILE = "nodist/data/projection/projection-reference-data";
|
---|
72 | private static final String PROJ_LIB_DIR = "nodist/data/projection";
|
---|
73 |
|
---|
74 | private static class RefEntry {
|
---|
75 | String code;
|
---|
76 | String def;
|
---|
77 | List<Pair<LatLon, EastNorth>> data;
|
---|
78 |
|
---|
79 | RefEntry(String code, String def) {
|
---|
80 | this.code = code;
|
---|
81 | this.def = def;
|
---|
82 | this.data = new ArrayList<>();
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | static Random rand = new SecureRandom();
|
---|
87 |
|
---|
88 | static boolean debug;
|
---|
89 | static List<String> forcedCodes;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Program entry point.
|
---|
93 | * @param args optional comma-separated list of projections to update. If set, only these projections will be updated
|
---|
94 | * @throws IOException in case of I/O error
|
---|
95 | */
|
---|
96 | public static void main(String[] args) throws IOException {
|
---|
97 | if (args.length > 0) {
|
---|
98 | debug = "debug".equals(args[0]);
|
---|
99 | if (args[args.length - 1].startsWith("EPSG:")) {
|
---|
100 | forcedCodes = Arrays.asList(args[args.length - 1].split(",", -1));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | Collection<RefEntry> refs = readData();
|
---|
104 | refs = updateData(refs);
|
---|
105 | writeData(refs);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Reads data from the reference file.
|
---|
110 | * @return the data
|
---|
111 | * @throws IOException if any I/O error occurs
|
---|
112 | */
|
---|
113 | private static Collection<RefEntry> readData() throws IOException {
|
---|
114 | Collection<RefEntry> result = new ArrayList<>();
|
---|
115 | if (!new File(REFERENCE_DATA_FILE).exists()) {
|
---|
116 | System.err.println("Warning: reference file does not exist.");
|
---|
117 | return result;
|
---|
118 | }
|
---|
119 | try (BufferedReader in = new BufferedReader(new InputStreamReader(
|
---|
120 | Files.newInputStream(Paths.get(REFERENCE_DATA_FILE)), StandardCharsets.UTF_8))) {
|
---|
121 | String line;
|
---|
122 | Pattern projPattern = Pattern.compile("<(.+?)>(.*)<>");
|
---|
123 | RefEntry curEntry = null;
|
---|
124 | while ((line = in.readLine()) != null) {
|
---|
125 | if (line.startsWith("#") || line.trim().isEmpty()) {
|
---|
126 | continue;
|
---|
127 | }
|
---|
128 | if (line.startsWith("<")) {
|
---|
129 | Matcher m = projPattern.matcher(line);
|
---|
130 | if (!m.matches()) {
|
---|
131 | fail("unable to parse line: " + line);
|
---|
132 | }
|
---|
133 | String code = m.group(1);
|
---|
134 | String def = m.group(2).trim();
|
---|
135 | curEntry = new RefEntry(code, def);
|
---|
136 | result.add(curEntry);
|
---|
137 | } else if (curEntry != null) {
|
---|
138 | String[] f = line.trim().split(",", -1);
|
---|
139 | double lon = Double.parseDouble(f[0]);
|
---|
140 | double lat = Double.parseDouble(f[1]);
|
---|
141 | double east = Double.parseDouble(f[2]);
|
---|
142 | double north = Double.parseDouble(f[3]);
|
---|
143 | curEntry.data.add(Pair.create(new LatLon(lat, lon), new EastNorth(east, north)));
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return result;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Generates new reference data by calling external program cs2cs.
|
---|
152 | *
|
---|
153 | * Old data is kept, as long as the projection definition is still the same.
|
---|
154 | *
|
---|
155 | * @param refs old data
|
---|
156 | * @return updated data
|
---|
157 | */
|
---|
158 | private static Collection<RefEntry> updateData(Collection<RefEntry> refs) {
|
---|
159 | Set<String> failed = new LinkedHashSet<>();
|
---|
160 | final int N_POINTS = 8;
|
---|
161 |
|
---|
162 | Map<String, RefEntry> refsMap = new HashMap<>();
|
---|
163 | for (RefEntry ref : refs) {
|
---|
164 | refsMap.put(ref.code, ref);
|
---|
165 | }
|
---|
166 |
|
---|
167 | List<RefEntry> refsNew = new ArrayList<>();
|
---|
168 |
|
---|
169 | Set<String> codes = new TreeSet<>(new CodeProjectionChoice.CodeComparator());
|
---|
170 | codes.addAll(Projections.getAllProjectionCodes());
|
---|
171 | for (String code : codes) {
|
---|
172 | String def = Projections.getInit(code);
|
---|
173 |
|
---|
174 | RefEntry ref = new RefEntry(code, def);
|
---|
175 | RefEntry oldRef = refsMap.get(code);
|
---|
176 |
|
---|
177 | if (oldRef != null && Objects.equals(def, oldRef.def)) {
|
---|
178 | for (int i = 0; i < N_POINTS && i < oldRef.data.size(); i++) {
|
---|
179 | ref.data.add(oldRef.data.get(i));
|
---|
180 | }
|
---|
181 | }
|
---|
182 | boolean forced = forcedCodes != null && forcedCodes.contains(code) && !ref.data.isEmpty();
|
---|
183 | if (forced || ref.data.size() < N_POINTS) {
|
---|
184 | System.out.print(code);
|
---|
185 | System.out.flush();
|
---|
186 | Projection proj = Projections.getProjectionByCode(code);
|
---|
187 | Bounds b = proj.getWorldBoundsLatLon();
|
---|
188 | for (int i = forced ? 0 : ref.data.size(); i < N_POINTS; i++) {
|
---|
189 | System.out.print(".");
|
---|
190 | System.out.flush();
|
---|
191 | if (debug) {
|
---|
192 | System.out.println();
|
---|
193 | }
|
---|
194 | LatLon ll = forced ? ref.data.get(i).a : getRandom(b);
|
---|
195 | EastNorth en = latlon2eastNorthProj4(def, ll);
|
---|
196 | if (en != null) {
|
---|
197 | if (forced) {
|
---|
198 | ref.data.get(i).b = en;
|
---|
199 | } else {
|
---|
200 | ref.data.add(Pair.create(ll, en));
|
---|
201 | }
|
---|
202 | } else {
|
---|
203 | System.err.println("Warning: cannot convert "+code+" at "+ll);
|
---|
204 | failed.add(code);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | System.out.println();
|
---|
208 | }
|
---|
209 | refsNew.add(ref);
|
---|
210 | }
|
---|
211 | if (!failed.isEmpty()) {
|
---|
212 | System.err.println("Error: the following " + failed.size() + " entries had errors: " + failed);
|
---|
213 | }
|
---|
214 | return refsNew;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Get random LatLon value within the bounds.
|
---|
219 | * @param b the bounds
|
---|
220 | * @return random LatLon value within the bounds
|
---|
221 | */
|
---|
222 | private static LatLon getRandom(Bounds b) {
|
---|
223 | double lat, lon;
|
---|
224 | lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
|
---|
225 | double minlon = b.getMinLon();
|
---|
226 | double maxlon = b.getMaxLon();
|
---|
227 | if (b.crosses180thMeridian()) {
|
---|
228 | maxlon += 360;
|
---|
229 | }
|
---|
230 | lon = minlon + rand.nextDouble() * (maxlon - minlon);
|
---|
231 | lon = LatLon.toIntervalLon(lon);
|
---|
232 | return new LatLon(lat, lon);
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Run external PROJ.4 library to convert lat/lon to east/north value.
|
---|
237 | * @param def the proj.4 projection definition string
|
---|
238 | * @param ll the LatLon
|
---|
239 | * @return projected EastNorth or null in case of error
|
---|
240 | */
|
---|
241 | private static EastNorth latlon2eastNorthProj4(String def, LatLon ll) {
|
---|
242 | try {
|
---|
243 | Class<?> projClass = Class.forName("org.proj4.PJ");
|
---|
244 | Constructor<?> constructor = projClass.getConstructor(String.class);
|
---|
245 | Method transform = projClass.getMethod("transform", projClass, int.class, double[].class, int.class, int.class);
|
---|
246 | Object sourcePJ = constructor.newInstance("+proj=longlat +datum=WGS84");
|
---|
247 | Object targetPJ = constructor.newInstance(def);
|
---|
248 | double[] coordinates = {ll.lon(), ll.lat()};
|
---|
249 | if (debug) {
|
---|
250 | System.out.println(def);
|
---|
251 | System.out.print(Arrays.toString(coordinates));
|
---|
252 | }
|
---|
253 | transform.invoke(sourcePJ, targetPJ, 2, coordinates, 0, 1);
|
---|
254 | if (debug) {
|
---|
255 | System.out.println(" -> " + Arrays.toString(coordinates));
|
---|
256 | }
|
---|
257 | return new EastNorth(coordinates[0], coordinates[1]);
|
---|
258 | } catch (ReflectiveOperationException | LinkageError | SecurityException e) {
|
---|
259 | if (debug) {
|
---|
260 | System.err.println("Error for " + def);
|
---|
261 | e.printStackTrace();
|
---|
262 | }
|
---|
263 | // PROJ JNI bindings not available, fallback to cs2cs
|
---|
264 | return latlon2eastNorthProj4cs2cs(def, ll);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Run external cs2cs command from the PROJ.4 library to convert lat/lon to east/north value.
|
---|
270 | * @param def the proj.4 projection definition string
|
---|
271 | * @param ll the LatLon
|
---|
272 | * @return projected EastNorth or null in case of error
|
---|
273 | */
|
---|
274 | @SuppressFBWarnings(value = "COMMAND_INJECTION")
|
---|
275 | private static EastNorth latlon2eastNorthProj4cs2cs(String def, LatLon ll) {
|
---|
276 | List<String> args = new ArrayList<>();
|
---|
277 | args.add(CS2CS_EXE);
|
---|
278 | args.addAll(Arrays.asList("-f %.9f +proj=longlat +datum=WGS84 +to".split(" ", -1)));
|
---|
279 | // proj.4 cannot read our ntf_r93_b.gsb file
|
---|
280 | // possibly because it is big endian. Use equivalent
|
---|
281 | // little endian file shipped with proj.4.
|
---|
282 | // see http://geodesie.ign.fr/contenu/fichiers/documentation/algorithmes/notice/NT111_V1_HARMEL_TransfoNTF-RGF93_FormatGrilleNTV2.pdf
|
---|
283 | def = def.replace("ntf_r93_b.gsb", "ntf_r93.gsb");
|
---|
284 | if (PlatformManager.isPlatformWindows()) {
|
---|
285 | def = def.replace("'", "\\'").replace("\"", "\\\"");
|
---|
286 | }
|
---|
287 | args.addAll(Arrays.asList(def.split(" ", -1)));
|
---|
288 | ProcessBuilder pb = new ProcessBuilder(args);
|
---|
289 | pb.environment().put("PROJ_LIB", new File(PROJ_LIB_DIR).getAbsolutePath());
|
---|
290 |
|
---|
291 | String output = "";
|
---|
292 | try {
|
---|
293 | Process process = pb.start();
|
---|
294 | OutputStream stdin = process.getOutputStream();
|
---|
295 | InputStream stdout = process.getInputStream();
|
---|
296 | InputStream stderr = process.getErrorStream();
|
---|
297 | try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin, StandardCharsets.UTF_8))) {
|
---|
298 | String s = String.format("%s %s%n",
|
---|
299 | LatLon.cDdHighPrecisionFormatter.format(ll.lon()),
|
---|
300 | LatLon.cDdHighPrecisionFormatter.format(ll.lat()));
|
---|
301 | if (debug) {
|
---|
302 | System.out.println("\n" + String.join(" ", args) + "\n" + s);
|
---|
303 | }
|
---|
304 | writer.write(s);
|
---|
305 | }
|
---|
306 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(stdout, StandardCharsets.UTF_8))) {
|
---|
307 | String line;
|
---|
308 | while (null != (line = reader.readLine())) {
|
---|
309 | if (debug) {
|
---|
310 | System.out.println("> " + line);
|
---|
311 | }
|
---|
312 | output = line;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(stderr, StandardCharsets.UTF_8))) {
|
---|
316 | String line;
|
---|
317 | while (null != (line = reader.readLine())) {
|
---|
318 | System.err.println("! " + line);
|
---|
319 | }
|
---|
320 | }
|
---|
321 | } catch (IOException e) {
|
---|
322 | System.err.println("Error: Running external command failed: " + e + "\nCommand was: " + String.join(" ", args));
|
---|
323 | return null;
|
---|
324 | }
|
---|
325 | Pattern p = Pattern.compile("(\\S+)\\s+(\\S+)\\s.*");
|
---|
326 | Matcher m = p.matcher(output);
|
---|
327 | if (!m.matches()) {
|
---|
328 | System.err.println("Error: Cannot parse cs2cs output: '" + output + "'");
|
---|
329 | return null;
|
---|
330 | }
|
---|
331 | String es = m.group(1);
|
---|
332 | String ns = m.group(2);
|
---|
333 | if ("*".equals(es) || "*".equals(ns)) {
|
---|
334 | System.err.println("Error: cs2cs is unable to convert coordinates.");
|
---|
335 | return null;
|
---|
336 | }
|
---|
337 | try {
|
---|
338 | return new EastNorth(Double.parseDouble(es), Double.parseDouble(ns));
|
---|
339 | } catch (NumberFormatException nfe) {
|
---|
340 | System.err.println("Error: Cannot parse cs2cs output: '" + es + "', '" + ns + "'" + "\nCommand was: " + String.join(" ", args));
|
---|
341 | return null;
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Writes data to file.
|
---|
347 | * @param refs the data
|
---|
348 | * @throws IOException if any I/O error occurs
|
---|
349 | */
|
---|
350 | private static void writeData(Collection<RefEntry> refs) throws IOException {
|
---|
351 | Map<String, RefEntry> refsMap = new TreeMap<>(new CodeProjectionChoice.CodeComparator());
|
---|
352 | for (RefEntry ref : refs) {
|
---|
353 | refsMap.put(ref.code, ref);
|
---|
354 | }
|
---|
355 | try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
---|
356 | Files.newOutputStream(Paths.get(REFERENCE_DATA_FILE)), StandardCharsets.UTF_8))) {
|
---|
357 | for (Map.Entry<String, RefEntry> e : refsMap.entrySet()) {
|
---|
358 | RefEntry ref = e.getValue();
|
---|
359 | out.write("<" + ref.code + "> " + ref.def + " <>\n");
|
---|
360 | for (Pair<LatLon, EastNorth> p : ref.data) {
|
---|
361 | LatLon ll = p.a;
|
---|
362 | EastNorth en = p.b;
|
---|
363 | out.write(" " + ll.lon() + "," + ll.lat() + "," + en.east() + "," + en.north() + "\n");
|
---|
364 | }
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Test projections.
|
---|
371 | * @throws IOException if any I/O error occurs
|
---|
372 | */
|
---|
373 | @Test
|
---|
374 | void testProjections() throws IOException {
|
---|
375 | Set<String> failures = Collections.synchronizedSet(new TreeSet<>());
|
---|
376 | Map<String, Set<String>> failingProjs = new ConcurrentHashMap<>();
|
---|
377 | Set<String> allCodes = new HashSet<>(Projections.getAllProjectionCodes());
|
---|
378 | Collection<RefEntry> refs = readData();
|
---|
379 | refs.stream().map(ref -> ref.code).forEach(allCodes::remove);
|
---|
380 | if (!allCodes.isEmpty()) {
|
---|
381 | fail("no reference data for following projections: "+allCodes);
|
---|
382 | }
|
---|
383 |
|
---|
384 | refs.parallelStream().forEach(ref -> {
|
---|
385 | String def0 = Projections.getInit(ref.code);
|
---|
386 | if (def0 == null) {
|
---|
387 | fail("unknown code: "+ref.code);
|
---|
388 | }
|
---|
389 | if (!ref.def.equals(def0)) {
|
---|
390 | failures.add("definitions for ".concat(ref.code).concat(" do not match\n"));
|
---|
391 | } else {
|
---|
392 | CustomProjection proj = (CustomProjection) Projections.getProjectionByCode(ref.code);
|
---|
393 | double scale = proj.getToMeter();
|
---|
394 | for (Pair<LatLon, EastNorth> p : ref.data) {
|
---|
395 | LatLon ll = p.a;
|
---|
396 | EastNorth enRef = p.b;
|
---|
397 | enRef = new EastNorth(enRef.east() * scale, enRef.north() * scale); // convert to meter
|
---|
398 |
|
---|
399 | EastNorth en = proj.latlon2eastNorth(ll);
|
---|
400 | if (proj.switchXY()) {
|
---|
401 | en = new EastNorth(en.north(), en.east());
|
---|
402 | }
|
---|
403 | en = new EastNorth(en.east() * scale, en.north() * scale); // convert to meter
|
---|
404 | final double EPSILON_EN = 1e-2; // 1cm
|
---|
405 | if (!isEqual(enRef, en, EPSILON_EN, true)) {
|
---|
406 | String errorEN = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
|
---|
407 | " expected: eastnorth(%s,%s),%n" +
|
---|
408 | " but got: eastnorth(%s,%s)!%n",
|
---|
409 | proj, proj.toCode(), ll.lat(), ll.lon(), enRef.east(), enRef.north(), en.east(), en.north());
|
---|
410 | failures.add(errorEN);
|
---|
411 | failingProjs.computeIfAbsent(proj.proj.getProj4Id(), x -> new TreeSet<>()).add(ref.code);
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | });
|
---|
416 | if (!failures.isEmpty()) {
|
---|
417 | System.err.println(failures);
|
---|
418 | throw new AssertionError("Failing:\n" +
|
---|
419 | failingProjs.keySet().size() + " projections: " + failingProjs.keySet() + "\n" +
|
---|
420 | failingProjs.values().stream().mapToInt(Set::size).sum() + " definitions: " + failingProjs);
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Check if two EastNorth objects are equal.
|
---|
426 | * @param en1 first value
|
---|
427 | * @param en2 second value
|
---|
428 | * @param epsilon allowed tolerance
|
---|
429 | * @param abs true if absolute value is compared; this is done as long as
|
---|
430 | * advanced axis configuration is not supported in JOSM
|
---|
431 | * @return true if both are considered equal
|
---|
432 | */
|
---|
433 | private static boolean isEqual(EastNorth en1, EastNorth en2, double epsilon, boolean abs) {
|
---|
434 | double east1 = en1.east();
|
---|
435 | double north1 = en1.north();
|
---|
436 | double east2 = en2.east();
|
---|
437 | double north2 = en2.north();
|
---|
438 | if (abs) {
|
---|
439 | east1 = Math.abs(east1);
|
---|
440 | north1 = Math.abs(north1);
|
---|
441 | east2 = Math.abs(east2);
|
---|
442 | north2 = Math.abs(north2);
|
---|
443 | }
|
---|
444 | return Math.abs(east1 - east2) < epsilon && Math.abs(north1 - north2) < epsilon;
|
---|
445 | }
|
---|
446 | }
|
---|