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

Last change on this file since 11241 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

  • Property svn:eol-style set to native
File size: 13.8 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 = 8;
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 InputStream stdout = process.getInputStream();
225 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin, StandardCharsets.UTF_8))) {
226 writer.write(String.format("%.9f %.9f%n", ll.lon(), ll.lat()));
227 }
228 try (BufferedReader reader = new BufferedReader(new InputStreamReader(stdout, StandardCharsets.UTF_8))) {
229 output = reader.readLine();
230 }
231 } catch (IOException e) {
232 System.err.println("Error: Running external command failed: " + e + "\nCommand was: "+Utils.join(" ", args));
233 return null;
234 }
235 Pattern p = Pattern.compile("(\\S+)\\s+(\\S+)\\s.*");
236 Matcher m = p.matcher(output);
237 if (!m.matches()) {
238 System.err.println("Error: Cannot parse cs2cs output: '" + output + "'");
239 return null;
240 }
241 String es = m.group(1);
242 String ns = m.group(2);
243 if ("*".equals(es) || "*".equals(ns)) {
244 System.err.println("Error: cs2cs is unable to convert coordinates.");
245 return null;
246 }
247 try {
248 return new EastNorth(Double.parseDouble(es), Double.parseDouble(ns));
249 } catch (NumberFormatException nfe) {
250 System.err.println("Error: Cannot parse cs2cs output: '" + es + "', '" + ns + "'" + "\nCommand was: "+Utils.join(" ", args));
251 return null;
252 }
253 }
254
255 /**
256 * Writes data to file.
257 * @param refs the data
258 * @throws IOException if any I/O error occurs
259 */
260 private static void writeData(Collection<RefEntry> refs) throws IOException {
261 Map<String, RefEntry> refsMap = new TreeMap<>(new CodeProjectionChoice.CodeComparator());
262 for (RefEntry ref : refs) {
263 refsMap.put(ref.code, ref);
264 }
265 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
266 new FileOutputStream(REFERENCE_DATA_FILE), StandardCharsets.UTF_8))) {
267 for (Map.Entry<String, RefEntry> e : refsMap.entrySet()) {
268 RefEntry ref = e.getValue();
269 out.write("<" + ref.code + "> " + ref.def + " <>\n");
270 for (Pair<LatLon, EastNorth> p : ref.data) {
271 LatLon ll = p.a;
272 EastNorth en = p.b;
273 out.write(" " + ll.lon() + "," + ll.lat() + "," + en.east() + "," + en.north() + "\n");
274 }
275 }
276 }
277 }
278
279 @Test
280 public void testProjections() throws IOException {
281 StringBuilder fail = new StringBuilder();
282 Set<String> allCodes = new HashSet<>(Projections.getAllProjectionCodes());
283 Collection<RefEntry> refs = readData();
284
285 for (RefEntry ref : refs) {
286 String def0 = Projections.getInit(ref.code);
287 if (def0 == null) {
288 Assert.fail("unkown code: "+ref.code);
289 }
290 if (!ref.def.equals(def0)) {
291 Assert.fail("definitions for " + ref.code + " do not match");
292 }
293 Projection proj = Projections.getProjectionByCode(ref.code);
294 double scale = ((CustomProjection) proj).getToMeter();
295 for (Pair<LatLon, EastNorth> p : ref.data) {
296 LatLon ll = p.a;
297 EastNorth enRef = p.b;
298 enRef = new EastNorth(enRef.east() * scale, enRef.north() * scale); // convert to meter
299
300 EastNorth en = proj.latlon2eastNorth(ll);
301 if (proj.switchXY()) {
302 en = new EastNorth(en.north(), en.east());
303 }
304 en = new EastNorth(en.east() * scale, en.north() * scale); // convert to meter
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.