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

Last change on this file since 9628 was 9628, checked in by bastiK, 8 years ago

fix WMTS with EPSG:4326 broken in [9608] (see #12186)

  • Property svn:eol-style set to native
File size: 13.7 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.InputStream;
12import java.io.InputStreamReader;
13import java.io.OutputStream;
14import java.io.OutputStreamWriter;
15import java.nio.charset.StandardCharsets;
16import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.HashSet;
21import java.util.LinkedHashSet;
22import java.util.List;
23import java.util.Map;
24import java.util.Objects;
25import java.util.Random;
26import java.util.Set;
27import java.util.TreeMap;
28import java.util.TreeSet;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32import org.junit.Assert;
33import org.junit.Test;
34import org.openstreetmap.josm.data.Bounds;
35import org.openstreetmap.josm.data.coor.EastNorth;
36import org.openstreetmap.josm.data.coor.LatLon;
37import org.openstreetmap.josm.gui.preferences.projection.CodeProjectionChoice;
38import org.openstreetmap.josm.tools.Pair;
39import org.openstreetmap.josm.tools.Utils;
40
41/**
42 * Test projections using reference data from external program.
43 *
44 * To update the reference data file <code>data_nodist/projection/projection-reference-data</code>,
45 * run the main method of this class. For this, you need to have the cs2cs
46 * program from the proj.4 library in path (or use <code>CS2CS_EXE</code> to set
47 * the full path of the executable). Make sure the required *.gsb grid files
48 * can be accessed, i.e. copy them from <code>data/projection</code> to <code>/usr/share/proj</code> or
49 * wherever cs2cs expects them to be placed.
50 *
51 * The input parameter for the external library is <em>not</em> the projection code
52 * (e.g. "EPSG:25828"), but the entire definition, (e.g. "+proj=utm +zone=28 +ellps=GRS80 +nadgrids=null").
53 * This means the test does not verify our definitions, but the correctness
54 * of the algorithm, given a certain definition.
55 */
56public class ProjectionRefTest {
57
58 private static final String CS2CS_EXE = "cs2cs";
59
60 private static final String REFERENCE_DATA_FILE = "data_nodist/projection/projection-reference-data";
61
62 private static class RefEntry {
63 String code;
64 String def;
65 List<Pair<LatLon, EastNorth>> data;
66
67 RefEntry(String code, String def) {
68 this.code = code;
69 this.def = def;
70 this.data = new ArrayList<>();
71 }
72 }
73
74 static Random rand = new Random();
75
76 public static void main(String[] args) throws FileNotFoundException, IOException {
77 Collection<RefEntry> refs = readData();
78 refs = updateData(refs);
79 writeData(refs);
80 }
81
82 /**
83 * Reads data from the reference file.
84 * @return the data
85 * @throws IOException if any I/O error occurs
86 */
87 private static Collection<RefEntry> readData() throws IOException {
88 Collection<RefEntry> result = new ArrayList<>();
89 if (!new File(REFERENCE_DATA_FILE).exists()) {
90 System.err.println("Warning: refrence file does not exist.");
91 return result;
92 }
93 try (BufferedReader in = new BufferedReader(new InputStreamReader(
94 new FileInputStream(REFERENCE_DATA_FILE), StandardCharsets.UTF_8))) {
95 String line;
96 Pattern projPattern = Pattern.compile("<(.+?)>(.*)<>");
97 RefEntry curEntry = null;
98 while ((line = in.readLine()) != null) {
99 if (line.startsWith("#") || line.trim().isEmpty()) {
100 continue;
101 }
102 if (line.startsWith("<")) {
103 Matcher m = projPattern.matcher(line);
104 if (!m.matches()) {
105 Assert.fail("unable to parse line: " + line);
106 }
107 String code = m.group(1);
108 String def = m.group(2).trim();
109 curEntry = new RefEntry(code, def);
110 result.add(curEntry);
111 } else if (curEntry != null) {
112 String[] f = line.trim().split(",");
113 double lon = Double.parseDouble(f[0]);
114 double lat = Double.parseDouble(f[1]);
115 double east = Double.parseDouble(f[2]);
116 double north = Double.parseDouble(f[3]);
117 curEntry.data.add(Pair.create(new LatLon(lat, lon), new EastNorth(east, north)));
118 }
119 }
120 }
121 return result;
122 }
123
124 /**
125 * Generates new reference data by calling external program cs2cs.
126 *
127 * Old data is kept, as long as the projection definition is still the same.
128 *
129 * @param refs old data
130 * @return updated data
131 */
132 private static Collection<RefEntry> updateData(Collection<RefEntry> refs) {
133 Set<String> failed = new LinkedHashSet<>();
134 final int N_POINTS = 20;
135
136 Map<String, RefEntry> refsMap = new HashMap<>();
137 for (RefEntry ref : refs) {
138 refsMap.put(ref.code, ref);
139 }
140
141 List<RefEntry> refsNew = new ArrayList<>();
142
143 Set<String> codes = new TreeSet<>(new CodeProjectionChoice.CodeComparator());
144 codes.addAll(Projections.getAllProjectionCodes());
145 for (String code : codes) {
146 String def = Projections.getInit(code);
147
148 RefEntry ref = new RefEntry(code, def);
149 RefEntry oldRef = refsMap.get(code);
150
151 if (oldRef != null && Objects.equals(def, oldRef.def)) {
152 for (int i = 0; i < N_POINTS && i < oldRef.data.size(); i++) {
153 ref.data.add(oldRef.data.get(i));
154 }
155 }
156 if (ref.data.size() < N_POINTS) {
157 System.out.print(code);
158 System.out.flush();
159 Projection proj = Projections.getProjectionByCode(code);
160 Bounds b = proj.getWorldBoundsLatLon();
161 for (int i = ref.data.size(); i < N_POINTS; i++) {
162 System.out.print(".");
163 System.out.flush();
164 LatLon ll = getRandom(b);
165 EastNorth en = latlon2eastNorthProj4(def, ll);
166 if (en != null) {
167 ref.data.add(Pair.create(ll, en));
168 } else {
169 System.err.println("Warning: cannot convert "+code+" at "+ll);
170 failed.add(code);
171 }
172 }
173 System.out.println();
174 }
175 refsNew.add(ref);
176 }
177 if (!failed.isEmpty()) {
178 System.err.println("Error: the following " + failed.size() + " entries had errors: " + failed);
179 }
180 return refsNew;
181 }
182
183 /**
184 * Get random LatLon value within the bounds.
185 * @param b the bounds
186 * @return random LatLon value within the bounds
187 */
188 private static LatLon getRandom(Bounds b) {
189 double lat, lon;
190 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
191 double minlon = b.getMinLon();
192 double maxlon = b.getMaxLon();
193 if (b.crosses180thMeridian()) {
194 maxlon += 360;
195 }
196 lon = minlon + rand.nextDouble() * (maxlon - minlon);
197 lon = LatLon.toIntervalLon(lon);
198 return new LatLon(lat, lon);
199 }
200
201 /**
202 * Run external cs2cs command from the PROJ.4 library to convert lat/lon to
203 * east/north value.
204 * @param def the proj.4 projection definition string
205 * @param ll the LatLon
206 * @return projected EastNorth or null in case of error
207 */
208 private static EastNorth latlon2eastNorthProj4(String def, LatLon ll) {
209 List<String> args = new ArrayList<>();
210 args.add(CS2CS_EXE);
211 args.addAll(Arrays.asList("-f %.9f +proj=longlat +datum=WGS84 +to".split(" ")));
212 // proj.4 cannot read our ntf_r93_b.gsb file
213 // possibly because it is big endian. Use equivalent
214 // little endian file shipped with proj.4.
215 // see http://geodesie.ign.fr/contenu/fichiers/documentation/algorithmes/notice/NT111_V1_HARMEL_TransfoNTF-RGF93_FormatGrilleNTV2.pdf
216 def = def.replace("ntf_r93_b.gsb", "ntf_r93.gsb");
217 args.addAll(Arrays.asList(def.split(" ")));
218 ProcessBuilder pb = new ProcessBuilder(args);
219
220 String output;
221 try {
222 Process process = pb.start();
223 OutputStream stdin = process.getOutputStream();
224 final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
225 InputStream stdout = process.getInputStream();
226 final BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
227 String input = String.format("%.9f %.9f\n", ll.lon(), ll.lat());
228 writer.write(input);
229 writer.close();
230 output = reader.readLine();
231 reader.close();
232 } catch (IOException e) {
233 System.err.println("Error: Running external command failed: " + e + "\nCommand was: "+Utils.join(" ", args));
234 return null;
235 }
236 Pattern p = Pattern.compile("(\\S+)\\s+(\\S+)\\s.*");
237 Matcher m = p.matcher(output);
238 if (!m.matches()) {
239 System.err.println("Error: Cannot parse cs2cs output: '" + output + "'");
240 return null;
241 }
242 String es = m.group(1);
243 String ns = m.group(2);
244 if ("*".equals(es) || "*".equals(ns)) {
245 System.err.println("Error: cs2cs is unable to convert coordinates.");
246 return null;
247 }
248 try {
249 return new EastNorth(Double.parseDouble(es), Double.parseDouble(ns));
250 } catch (NumberFormatException nfe) {
251 System.err.println("Error: Cannot parse cs2cs output: '" + es + "', '" + ns + "'" + "\nCommand was: "+Utils.join(" ", args));
252 return null;
253 }
254 }
255
256 /**
257 * Writes data to file.
258 * @param refs the data
259 * @throws IOException if any I/O error occurs
260 */
261 private static void writeData(Collection<RefEntry> refs) throws IOException {
262 Map<String, RefEntry> refsMap = new TreeMap<>(new CodeProjectionChoice.CodeComparator());
263 for (RefEntry ref : refs) {
264 refsMap.put(ref.code, ref);
265 }
266 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
267 new FileOutputStream(REFERENCE_DATA_FILE), StandardCharsets.UTF_8))) {
268 for (Map.Entry<String, RefEntry> e : refsMap.entrySet()) {
269 RefEntry ref = e.getValue();
270 out.write("<" + ref.code + "> " + ref.def + " <>\n");
271 for (Pair<LatLon, EastNorth> p : ref.data) {
272 LatLon ll = p.a;
273 EastNorth en = p.b;
274 out.write(" " + ll.lon() + "," + ll.lat() + "," + en.east() + "," + en.north() + "\n");
275 }
276 }
277 }
278 }
279
280 @Test
281 public void test() throws IOException {
282 StringBuilder fail = new StringBuilder();
283 Set<String> allCodes = new HashSet<>(Projections.getAllProjectionCodes());
284 Collection<RefEntry> refs = readData();
285
286 for (RefEntry ref : refs) {
287 String def0 = Projections.getInit(ref.code);
288 if (def0 == null) {
289 Assert.fail("unkown code: "+ref.code);
290 }
291 if (!ref.def.equals(def0)) {
292 Assert.fail("definitions for " + ref.code + " do not match");
293 }
294 Projection proj = Projections.getProjectionByCode(ref.code);
295 double scale = ((CustomProjection) proj).getMetersPerUnitProj();
296 for (Pair<LatLon, EastNorth> p : ref.data) {
297 LatLon ll = p.a;
298 EastNorth enRef = p.b;
299 enRef = new EastNorth(enRef.east() * scale, enRef.north() * scale); // convert to meter
300
301 EastNorth en = proj.latlon2eastNorth(ll);
302 if (proj.switchXY()) {
303 en = new EastNorth(en.north(), en.east());
304 }
305 final double EPSILON_EN = 1e-2; // 1cm
306 if (!isEqual(enRef, en, EPSILON_EN, true)) {
307 String errorEN = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
308 " expected: eastnorth(%s,%s),%n" +
309 " but got: eastnorth(%s,%s)!%n",
310 proj.toString(), proj.toCode(), ll.lat(), ll.lon(), enRef.east(), enRef.north(), en.east(), en.north());
311 fail.append(errorEN);
312 }
313 }
314 allCodes.remove(ref.code);
315 }
316 if (!allCodes.isEmpty()) {
317 Assert.fail("no reference data for following projections: "+allCodes);
318 }
319 if (fail.length() > 0) {
320 System.err.println(fail.toString());
321 throw new AssertionError(fail.toString());
322 }
323 }
324
325 /**
326 * Check if two EastNorth objects are equal.
327 * @param en1 first value
328 * @param en2 second value
329 * @param epsilon allowed tolerance
330 * @param abs true if absolute value is compared; this is done as long as
331 * advanced axis configuration is not supported in JOSM
332 * @return true if both are considered equal
333 */
334 private static boolean isEqual(EastNorth en1, EastNorth en2, double epsilon, boolean abs) {
335 double east1 = en1.east();
336 double north1 = en1.north();
337 double east2 = en2.east();
338 double north2 = en2.north();
339 if (abs) {
340 east1 = Math.abs(east1);
341 north1 = Math.abs(north1);
342 east2 = Math.abs(east2);
343 north2 = Math.abs(north2);
344 }
345 return Math.abs(east1 - east2) < epsilon && Math.abs(north1 - north2) < epsilon;
346 }
347}
Note: See TracBrowser for help on using the repository browser.