source: josm/trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionRefTest.java@ 17442

Last change on this file since 17442 was 17333, checked in by Don-vip, 3 years ago

see #20129 - Fix typos and misspellings in the code (patch by gaben)

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