source: josm/trunk/scripts/SyncEditorLayerIndex.java@ 19020

Last change on this file since 19020 was 18989, checked in by taylor.smock, 9 months ago

Fix #23485: JOSM crashes when opening Imagery Preferences

  • SyncEditorLayerIndex.java now checks to see if imagery entries are valid; if not, it prints the missing fields and the standard description.
  • ImageryInfo now has isValid and getMissingFields; the latter method should only be called in tests or SyncEditorLayerIndex.
  • ImageryLayerInfo removes invalid ImageryInfo objects after parsing the source
  • Property svn:eol-style set to native
File size: 69.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2
3import static java.nio.charset.StandardCharsets.UTF_8;
4import static org.apache.commons.lang3.StringUtils.isBlank;
5import static org.apache.commons.lang3.StringUtils.isNotBlank;
6
7import java.io.BufferedReader;
8import java.io.BufferedWriter;
9import java.io.IOException;
10import java.io.OutputStreamWriter;
11import java.io.Writer;
12import java.lang.reflect.Field;
13import java.net.MalformedURLException;
14import java.net.URL;
15import java.nio.charset.Charset;
16import java.nio.file.Files;
17import java.nio.file.Paths;
18import java.text.DecimalFormat;
19import java.text.ParseException;
20import java.text.SimpleDateFormat;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.Calendar;
24import java.util.Collection;
25import java.util.Collections;
26import java.util.Date;
27import java.util.HashMap;
28import java.util.LinkedList;
29import java.util.List;
30import java.util.Locale;
31import java.util.Map;
32import java.util.Map.Entry;
33import java.util.Objects;
34import java.util.Set;
35import java.util.function.BiConsumer;
36import java.util.function.Function;
37import java.util.regex.Matcher;
38import java.util.regex.Pattern;
39import java.util.stream.Collectors;
40
41import org.openstreetmap.gui.jmapviewer.Coordinate;
42import org.openstreetmap.josm.data.Preferences;
43import org.openstreetmap.josm.data.imagery.ImageryInfo;
44import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
45import org.openstreetmap.josm.data.imagery.Shape;
46import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
47import org.openstreetmap.josm.data.preferences.JosmUrls;
48import org.openstreetmap.josm.data.projection.Projections;
49import org.openstreetmap.josm.data.sources.SourceInfo;
50import org.openstreetmap.josm.data.validation.routines.DomainValidator;
51import org.openstreetmap.josm.io.imagery.ImageryReader;
52import org.openstreetmap.josm.spi.preferences.Config;
53import org.openstreetmap.josm.tools.ImageProvider;
54import org.openstreetmap.josm.tools.JosmRuntimeException;
55import org.openstreetmap.josm.tools.Logging;
56import org.openstreetmap.josm.tools.OptionParser;
57import org.openstreetmap.josm.tools.OptionParser.OptionCount;
58import org.openstreetmap.josm.tools.ReflectionUtils;
59import org.openstreetmap.josm.tools.Utils;
60import org.xml.sax.SAXException;
61
62import jakarta.json.Json;
63import jakarta.json.JsonArray;
64import jakarta.json.JsonNumber;
65import jakarta.json.JsonObject;
66import jakarta.json.JsonReader;
67import jakarta.json.JsonString;
68import jakarta.json.JsonValue;
69
70/**
71 * Compare and analyse the differences of the editor layer index and the JOSM imagery list.
72 * The goal is to keep both lists in sync.
73 * <p>
74 * The <a href="https://github.com/osmlab/editor-layer-index">editor layer index</a> project
75 * provides also a version in the JOSM format, but the GEOJSON is the original source format, so we read that.
76 * <p>
77 * For running, the main JOSM binary needs to be in classpath, e.g.
78 * <p>
79 * {@code $ java -cp ../dist/josm-custom.jar SyncEditorLayerIndex}
80 * <p>
81 * Add option {@code -h} to show the available command line flags.
82 */
83@SuppressWarnings("unchecked")
84public class SyncEditorLayerIndex {
85
86 private static final int MAXLEN = 140;
87
88 private List<ImageryInfo> josmEntries;
89 private JsonArray eliEntries;
90
91 private final Map<String, JsonObject> eliUrls = new HashMap<>();
92 private final Map<String, ImageryInfo> josmUrls = new HashMap<>();
93 private final Map<String, ImageryInfo> josmMirrors = new HashMap<>();
94 private static final Map<String, String> oldproj = new HashMap<>();
95 private static final List<String> ignoreproj = new LinkedList<>();
96
97 private static String eliInputFile = "imagery_eli.geojson";
98 private static String josmInputFile = "imagery_josm.imagery.xml";
99 private static String ignoreInputFile = "imagery_josm.ignores.txt";
100 private static Writer outputStream;
101 private static String optionOutput;
102 private static boolean optionShorten;
103 private static boolean optionNoSkip;
104 private static boolean optionXhtmlBody;
105 private static boolean optionXhtml;
106 private static String optionEliXml;
107 private static String optionJosmXml;
108 private static String optionEncoding;
109 private static boolean optionNoEli;
110 private Map<String, String> skip = new HashMap<>();
111 private Map<String, String> skipStart = new HashMap<>();
112
113 /**
114 * Main method.
115 * @param args program arguments
116 * @throws IOException if any I/O error occurs
117 * @throws ReflectiveOperationException if any reflective operation error occurs
118 * @throws SAXException if any SAX error occurs
119 */
120 public static void main(String[] args) throws IOException, SAXException, ReflectiveOperationException {
121 Locale.setDefault(Locale.ROOT);
122 parseCommandLineArguments(args);
123 Config.setUrlsProvider(JosmUrls.getInstance());
124 Preferences pref = new Preferences(JosmBaseDirectories.getInstance());
125 Config.setPreferencesInstance(pref);
126 pref.init(false);
127 SyncEditorLayerIndex script = new SyncEditorLayerIndex();
128 script.setupProj();
129 script.loadSkip();
130 script.start();
131 script.loadJosmEntries();
132 if (optionJosmXml != null) {
133 try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(optionJosmXml), UTF_8)) {
134 script.printentries(script.josmEntries, writer);
135 }
136 }
137 script.loadELIEntries();
138 if (optionEliXml != null) {
139 try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(optionEliXml), UTF_8)) {
140 script.printentries(script.eliEntries, writer);
141 }
142 }
143 script.checkInOneButNotTheOther();
144 script.checkCommonEntries();
145 script.end();
146 if (outputStream != null) {
147 outputStream.close();
148 }
149 }
150
151 /**
152 * Displays help on the console
153 */
154 private static void showHelp() {
155 System.out.println(getHelp());
156 System.exit(0);
157 }
158
159 static String getHelp() {
160 return "usage: java -cp build SyncEditorLayerIndex\n" +
161 "-c,--encoding <encoding> output encoding (defaults to UTF-8 or cp850 on Windows)\n" +
162 "-e,--eli_input <eli_input> Input file for the editor layer index (geojson). " +
163 "Default is imagery_eli.geojson (current directory).\n" +
164 "-h,--help show this help\n" +
165 "-i,--ignore_input <ignore_input> Input file for the ignore list. Default is imagery_josm.ignores.txt (current directory).\n" +
166 "-j,--josm_input <josm_input> Input file for the JOSM imagery list (xml). " +
167 "Default is imagery_josm.imagery.xml (current directory).\n" +
168 "-m,--noeli don't show output for ELI problems\n" +
169 "-n,--noskip don't skip known entries\n" +
170 "-o,--output <output> Output file, - prints to stdout (default: -)\n" +
171 "-p,--elixml <elixml> ELI entries for use in JOSM as XML file (incomplete)\n" +
172 "-q,--josmxml <josmxml> JOSM entries reoutput as XML file (incomplete)\n" +
173 "-s,--shorten shorten the output, so it is easier to read in a console window\n" +
174 "-x,--xhtmlbody create XHTML body for display in a web page\n" +
175 "-X,--xhtml create XHTML for display in a web page\n";
176 }
177
178 /**
179 * Parse command line arguments.
180 * @param args program arguments
181 * @throws IOException in case of I/O error
182 */
183 static void parseCommandLineArguments(String[] args) throws IOException {
184 new OptionParser("JOSM/ELI synchronization script")
185 .addFlagParameter("help", SyncEditorLayerIndex::showHelp)
186 .addShortAlias("help", "h")
187 .addArgumentParameter("output", OptionCount.OPTIONAL, x -> optionOutput = x)
188 .addShortAlias("output", "o")
189 .addArgumentParameter("eli_input", OptionCount.OPTIONAL, x -> eliInputFile = x)
190 .addShortAlias("eli_input", "e")
191 .addArgumentParameter("josm_input", OptionCount.OPTIONAL, x -> josmInputFile = x)
192 .addShortAlias("josm_input", "j")
193 .addArgumentParameter("ignore_input", OptionCount.OPTIONAL, x -> ignoreInputFile = x)
194 .addShortAlias("ignore_input", "i")
195 .addFlagParameter("shorten", () -> optionShorten = true)
196 .addShortAlias("shorten", "s")
197 .addFlagParameter("noskip", () -> optionNoSkip = true)
198 .addShortAlias("noskip", "n")
199 .addFlagParameter("xhtmlbody", () -> optionXhtmlBody = true)
200 .addShortAlias("xhtmlbody", "x")
201 .addFlagParameter("xhtml", () -> optionXhtml = true)
202 .addShortAlias("xhtml", "X")
203 .addArgumentParameter("elixml", OptionCount.OPTIONAL, x -> optionEliXml = x)
204 .addShortAlias("elixml", "p")
205 .addArgumentParameter("josmxml", OptionCount.OPTIONAL, x -> optionJosmXml = x)
206 .addShortAlias("josmxml", "q")
207 .addFlagParameter("noeli", () -> optionNoEli = true)
208 .addShortAlias("noeli", "m")
209 .addArgumentParameter("encoding", OptionCount.OPTIONAL, x -> optionEncoding = x)
210 .addShortAlias("encoding", "c")
211 .parseOptionsOrExit(Arrays.asList(args));
212
213 if (optionOutput != null && !"-".equals(optionOutput)) {
214 outputStream = Files.newBufferedWriter(Paths.get(optionOutput), optionEncoding != null ? Charset.forName(optionEncoding) : UTF_8);
215 } else if (optionEncoding != null) {
216 outputStream = new OutputStreamWriter(System.out, optionEncoding);
217 }
218 }
219
220 void setupProj() {
221 oldproj.put("EPSG:3359", "EPSG:3404");
222 oldproj.put("EPSG:3785", "EPSG:3857");
223 oldproj.put("EPSG:31297", "EPGS:31287");
224 oldproj.put("EPSG:31464", "EPSG:31468");
225 oldproj.put("EPSG:54004", "EPSG:3857");
226 oldproj.put("EPSG:102100", "EPSG:3857");
227 oldproj.put("EPSG:102113", "EPSG:3857");
228 oldproj.put("EPSG:900913", "EPGS:3857");
229 ignoreproj.add("EPSG:4267");
230 ignoreproj.add("EPSG:5221");
231 ignoreproj.add("EPSG:5514");
232 ignoreproj.add("EPSG:32019");
233 ignoreproj.add("EPSG:102066");
234 ignoreproj.add("EPSG:102067");
235 ignoreproj.add("EPSG:102685");
236 ignoreproj.add("EPSG:102711");
237 }
238
239 void loadSkip() throws IOException {
240 final Pattern pattern = Pattern.compile("^\\|\\| *(ELI|Ignore) *\\|\\| *\\{\\{\\{(.+)\\}\\}\\} *\\|\\|");
241 try (BufferedReader fr = Files.newBufferedReader(Paths.get(ignoreInputFile), UTF_8)) {
242 String line;
243
244 while ((line = fr.readLine()) != null) {
245 Matcher res = pattern.matcher(line);
246 if (res.matches()) {
247 String s = res.group(2);
248 if (s.endsWith("...")) {
249 s = s.substring(0, s.length() - 3);
250 if ("Ignore".equals(res.group(1))) {
251 skipStart.put(s, "green");
252 } else {
253 skipStart.put(s, "darkgoldenrod");
254 }
255 } else {
256 if ("Ignore".equals(res.group(1))) {
257 skip.put(s, "green");
258 } else {
259 skip.put(s, "darkgoldenrod");
260 }
261 }
262 }
263 }
264 }
265 }
266
267 void myprintlnfinal(String s) {
268 if (outputStream != null) {
269 try {
270 outputStream.write(s + System.getProperty("line.separator"));
271 } catch (IOException e) {
272 throw new JosmRuntimeException(e);
273 }
274 } else {
275 System.out.println(s);
276 }
277 }
278
279 String isSkipString(String s) {
280 if (skip.containsKey(s))
281 return skip.get(s);
282 for (Entry<String, String> str : skipStart.entrySet()) {
283 if (s.startsWith(str.getKey())) {
284 skipStart.remove(str.getKey());
285 return str.getValue();
286 }
287 }
288 return null;
289 }
290
291 void myprintln(String s) {
292 String color;
293 final String escaped = s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
294 if ((color = isSkipString(s)) != null) {
295 skip.remove(s);
296 if (optionXhtmlBody || optionXhtml) {
297 s = "<pre style=\"margin:3px;color:"+color+"\">"
298 + escaped +"</pre>";
299 }
300 if (!optionNoSkip) {
301 return;
302 }
303 } else if (optionXhtmlBody || optionXhtml) {
304 color =
305 s.startsWith("***") ? "black" :
306 ((s.startsWith("+ ") || s.startsWith("+++ ELI")) ? "blue" :
307 (s.startsWith("#") ? "indigo" :
308 (s.startsWith("!") ? "orange" :
309 (s.startsWith("~") ? "red" : "brown"))));
310 s = "<pre style=\"margin:3px;color:"+color+"\">"+ escaped +"</pre>";
311 }
312 if ((s.startsWith("+ ") || s.startsWith("+++ ELI") || s.startsWith("#")) && optionNoEli) {
313 return;
314 }
315 myprintlnfinal(s);
316 }
317
318 void start() {
319 if (optionXhtml) {
320 myprintlnfinal(
321 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
322 myprintlnfinal(
323 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"+
324 "<title>JOSM - ELI differences</title></head><body>\n");
325 }
326 }
327
328 void end() {
329 for (String s : skip.keySet()) {
330 myprintln("+++ Obsolete skip entry: " + s);
331 }
332 for (String s : skipStart.keySet()) {
333 myprintln("+++ Obsolete skip entry: " + s + "...");
334 }
335 if (optionXhtml) {
336 myprintlnfinal("</body></html>\n");
337 }
338 }
339
340 void loadELIEntries() throws IOException {
341 try (JsonReader jr = Json.createReader(Files.newBufferedReader(Paths.get(eliInputFile), UTF_8))) {
342 eliEntries = jr.readObject().getJsonArray("features");
343 }
344
345 for (JsonValue e : eliEntries) {
346 String url = getUrlStripped(e);
347 if (url.contains("{z}")) {
348 myprintln("+++ ELI-URL uses {z} instead of {zoom}: "+getDescription(e));
349 url = url.replace("{z}", "{zoom}");
350 }
351 if (eliUrls.containsKey(url)) {
352 myprintln("+++ ELI-URL is not unique: "+url);
353 } else {
354 eliUrls.put(url, e.asJsonObject());
355 }
356 JsonArray s = e.asJsonObject().get("properties").asJsonObject().getJsonArray("available_projections");
357 if (s != null) {
358 String urlLc = url.toLowerCase(Locale.ENGLISH);
359 List<String> old = new LinkedList<>();
360 for (JsonValue p : s) {
361 String proj = ((JsonString) p).getString();
362 if (oldproj.containsKey(proj) || ("CRS:84".equals(proj) && !urlLc.contains("version=1.3"))) {
363 old.add(proj);
364 }
365 }
366 if (!old.isEmpty()) {
367 myprintln("+ ELI Projections "+String.join(", ", old)+" not useful: "+getDescription(e));
368 }
369 }
370 }
371 myprintln("*** Loaded "+eliEntries.size()+" entries (ELI). ***");
372 }
373
374 String cdata(String s) {
375 return cdata(s, false);
376 }
377
378 String cdata(String s, boolean escape) {
379 if (escape) {
380 return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
381 } else if (s.matches(".*[<>&].*"))
382 return "<![CDATA["+s+"]]>";
383 return s;
384 }
385
386 String maininfo(Object entry, String offset) {
387 String t = getType(entry);
388 String res = offset + "<type>"+t+"</type>\n";
389 res += offset + "<url>"+cdata(getUrl(entry))+"</url>\n";
390 if (getMinZoom(entry) != null)
391 res += offset + "<min-zoom>"+getMinZoom(entry)+"</min-zoom>\n";
392 if (getMaxZoom(entry) != null)
393 res += offset + "<max-zoom>"+getMaxZoom(entry)+"</max-zoom>\n";
394 if ("wms".equals(t)) {
395 List<String> p = getProjections(entry);
396 if (p != null) {
397 res += offset + "<projections>\n";
398 for (String c : p) {
399 res += offset + " <code>"+c+"</code>\n";
400 }
401 res += offset + "</projections>\n";
402 }
403 }
404 return res;
405 }
406
407 void printentries(List<?> entries, Writer stream) throws IOException {
408 DecimalFormat df = new DecimalFormat("#.#######");
409 df.setRoundingMode(java.math.RoundingMode.CEILING);
410 stream.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
411 stream.write("<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n");
412 for (Object e : entries) {
413 stream.write(" <entry"
414 + ("eli-best".equals(getQuality(e)) ? " eli-best=\"true\"" : "")
415 + (getOverlay(e) ? " overlay=\"true\"" : "")
416 + ">\n");
417 String t;
418 if (isNotBlank(t = getName(e)))
419 stream.write(" <name>"+cdata(t, true)+"</name>\n");
420 if (isNotBlank(t = getId(e)))
421 stream.write(" <id>"+t+"</id>\n");
422 if (isNotBlank(t = getCategory(e)))
423 stream.write(" <category>"+t+"</category>\n");
424 if (isNotBlank(t = getDate(e)))
425 stream.write(" <date>"+t+"</date>\n");
426 if (isNotBlank(t = getCountryCode(e)))
427 stream.write(" <country-code>"+t+"</country-code>\n");
428 if ((getDefault(e)))
429 stream.write(" <default>true</default>\n");
430 stream.write(maininfo(e, " "));
431 if (isNotBlank(t = getAttributionText(e)))
432 stream.write(" <attribution-text mandatory=\"true\">"+cdata(t, true)+"</attribution-text>\n");
433 if (isNotBlank(t = getAttributionUrl(e)))
434 stream.write(" <attribution-url>"+cdata(t)+"</attribution-url>\n");
435 if (isNotBlank(t = getLogoImage(e)))
436 stream.write(" <logo-image>"+cdata(t, true)+"</logo-image>\n");
437 if (isNotBlank(t = getLogoUrl(e)))
438 stream.write(" <logo-url>"+cdata(t)+"</logo-url>\n");
439 if (isNotBlank(t = getTermsOfUseText(e)))
440 stream.write(" <terms-of-use-text>"+cdata(t, true)+"</terms-of-use-text>\n");
441 if (isNotBlank(t = getTermsOfUseUrl(e)))
442 stream.write(" <terms-of-use-url>"+cdata(t)+"</terms-of-use-url>\n");
443 if (isNotBlank(t = getPermissionReferenceUrl(e)))
444 stream.write(" <permission-ref>"+cdata(t)+"</permission-ref>\n");
445 if (isNotBlank(t = getPrivacyPolicyUrl(e)))
446 stream.write(" <privacy-policy-url>"+cdata(t)+"</privacy-policy-url>\n");
447 if ((getValidGeoreference(e)))
448 stream.write(" <valid-georeference>true</valid-georeference>\n");
449 if (isNotBlank(t = getIcon(e)))
450 stream.write(" <icon>"+cdata(t)+"</icon>\n");
451 for (Entry<String, String> d : getDescriptions(e).entrySet()) {
452 stream.write(" <description lang=\""+d.getKey()+"\">"+d.getValue()+"</description>\n");
453 }
454 for (ImageryInfo m : getMirrors(e)) {
455 stream.write(" <mirror>\n"+maininfo(m, " ")+" </mirror>\n");
456 }
457 double minlat = 1000;
458 double minlon = 1000;
459 double maxlat = -1000;
460 double maxlon = -1000;
461 String shapes = "";
462 String sep = "\n ";
463 try {
464 for (Shape s: getShapes(e)) {
465 shapes += " <shape>";
466 int i = 0;
467 for (Coordinate p: s.getPoints()) {
468 double lat = p.getLat();
469 double lon = p.getLon();
470 if (lat > maxlat) maxlat = lat;
471 if (lon > maxlon) maxlon = lon;
472 if (lat < minlat) minlat = lat;
473 if (lon < minlon) minlon = lon;
474 if ((i++ % 3) == 0) {
475 shapes += sep + " ";
476 }
477 shapes += "<point lat='"+df.format(lat)+"' lon='"+df.format(lon)+"'/>";
478 }
479 shapes += sep + "</shape>\n";
480 }
481 } catch (IllegalArgumentException ignored) {
482 Logging.trace(ignored);
483 }
484 if (!shapes.isEmpty()) {
485 stream.write(" <bounds min-lat='"+df.format(minlat)
486 +"' min-lon='"+df.format(minlon)
487 +"' max-lat='"+df.format(maxlat)
488 +"' max-lon='"+df.format(maxlon)+"'>\n");
489 stream.write(shapes + " </bounds>\n");
490 }
491 stream.write(" </entry>\n");
492 }
493 stream.write("</imagery>\n");
494 stream.close();
495 }
496
497 void loadJosmEntries() throws IOException, SAXException, ReflectiveOperationException {
498 try (ImageryReader reader = new ImageryReader(josmInputFile)) {
499 josmEntries = reader.parse();
500 }
501
502 for (ImageryInfo e : josmEntries) {
503 if (!e.isValid()) {
504 myprintln("~~~ JOSM-Entry missing fields (" + String.join(", ", e.getMissingFields()) + "): " + getDescription(e));
505 }
506 if (isBlank(getUrl(e))) {
507 myprintln("~~~ JOSM-Entry without URL: " + getDescription(e));
508 continue;
509 }
510 if (isBlank(e.getDate()) && e.getDate() != null) {
511 myprintln("~~~ JOSM-Entry with empty Date: " + getDescription(e));
512 continue;
513 }
514 if (isBlank(getName(e))) {
515 myprintln("~~~ JOSM-Entry without Name: " + getDescription(e));
516 continue;
517 }
518 String url = getUrlStripped(e);
519 if (url.contains("{z}")) {
520 myprintln("~~~ JOSM-URL uses {z} instead of {zoom}: "+getDescription(e));
521 url = url.replace("{z}", "{zoom}");
522 }
523 if (josmUrls.containsKey(url)) {
524 myprintln("~~~ JOSM-URL is not unique: "+url);
525 } else {
526 josmUrls.put(url, e);
527 }
528 for (ImageryInfo m : e.getMirrors()) {
529 url = getUrlStripped(m);
530 Field origNameField = SourceInfo.class.getDeclaredField("origName");
531 ReflectionUtils.setObjectsAccessible(origNameField);
532 origNameField.set(m, m.getOriginalName().replaceAll(" mirror server( \\d+)?", ""));
533 if (josmUrls.containsKey(url)) {
534 myprintln("~~~ JOSM-Mirror-URL is not unique: "+url);
535 } else {
536 josmUrls.put(url, m);
537 josmMirrors.put(url, m);
538 }
539 }
540 }
541 myprintln("*** Loaded "+josmEntries.size()+" entries (JOSM). ***");
542 }
543
544 // catch reordered arguments, make them uppercase, and switches to WMS version 1.3.0
545 String unifyWMS(String url) {
546 String[] x = url.replaceAll("(?i)VERSION=[0-9.]+", "VERSION=x")
547 .replaceAll("(?i)SRS=", "CRS=")
548 .replaceAll("(?i)BBOX=", "BBOX=")
549 .replaceAll("(?i)FORMAT=", "FORMAT=")
550 .replaceAll("(?i)LAYERS=", "LAYERS=")
551 .replaceAll("(?i)MAP=", "MAP=")
552 .replaceAll("(?i)REQUEST=", "REQUEST=")
553 .replaceAll("(?i)SERVICE=", "SERVICE=")
554 .replaceAll("(?i)STYLES=", "STYLES=")
555 .replaceAll("(?i)TRANSPARENT=FALSE", "TRANSPARENT=FALSE")
556 .replaceAll("(?i)TRANSPARENT=TRUE", "TRANSPARENT=TRUE")
557 .replaceAll("(?i)WIDTH=", "WIDTH=")
558 .replaceAll("(?i)HEIGHT=", "HEIGHT=")
559 .split("\\?");
560 return x[0] +"?" + Arrays.stream(x[1].split("&"))
561 .filter(s -> !s.endsWith("=")) // filter empty params
562 .sorted()
563 .collect(Collectors.joining("&"));
564 }
565
566 void checkInOneButNotTheOther() {
567 List<String> le = new LinkedList<>(eliUrls.keySet());
568 List<String> lj = new LinkedList<>(josmUrls.keySet());
569
570 for (String url : new LinkedList<>(le)) {
571 if (lj.contains(url)) {
572 le.remove(url);
573 lj.remove(url);
574 }
575 }
576
577 if (!le.isEmpty() && !lj.isEmpty()) {
578 List<String> ke = new LinkedList<>(le);
579 for (String urle : ke) {
580 JsonObject e = eliUrls.get(urle);
581 String ide = getId(e);
582 String urlhttps = urle.replace("http:", "https:");
583 if (lj.contains(urlhttps)) {
584 myprintln("+ Missing https: "+getDescription(e));
585 eliUrls.put(urlhttps, eliUrls.get(urle));
586 eliUrls.remove(urle);
587 le.remove(urle);
588 lj.remove(urlhttps);
589 } else if (isNotBlank(ide)) {
590 checkUrlsEquality(ide, e, urle, le, lj);
591 }
592 }
593 }
594
595 myprintln("*** URLs found in ELI but not in JOSM ("+le.size()+"): ***");
596 Collections.sort(le);
597 if (!le.isEmpty()) {
598 for (String l : le) {
599 myprintln("- " + getDescription(eliUrls.get(l)));
600 }
601 }
602 myprintln("*** URLs found in JOSM but not in ELI ("+lj.size()+"): ***");
603 Collections.sort(lj);
604 if (!lj.isEmpty()) {
605 for (String l : lj) {
606 myprintln("+ " + getDescription(josmUrls.get(l)));
607 }
608 }
609 }
610
611 void checkUrlsEquality(String ide, JsonObject e, String urle, List<String> le, List<String> lj) {
612 for (String urlj : new LinkedList<>(lj)) {
613 ImageryInfo j = josmUrls.get(urlj);
614 String idj = getId(j);
615
616 if (checkUrlEquality(ide, "id", idj, e, j, urle, urlj, le, lj)) {
617 return;
618 }
619 Collection<String> old = j.getOldIds();
620 if (old != null) {
621 for (String oidj : old) {
622 if (checkUrlEquality(ide, "oldid", oidj, e, j, urle, urlj, le, lj)) {
623 return;
624 }
625 }
626 }
627 }
628 }
629
630 boolean checkUrlEquality(
631 String ide, String idtype, String idj, JsonObject e, ImageryInfo j, String urle, String urlj, List<String> le, List<String> lj) {
632 if (ide.equals(idj) && Objects.equals(getType(j), getType(e))) {
633 if (getType(j).equals("wms") && unifyWMS(urle).equals(unifyWMS(urlj))) {
634 myprintln("# WMS-URL for "+idtype+" "+idj+" modified: "+getDescription(j));
635 } else {
636 myprintln("* URL for "+idtype+" "+idj+" differs ("+urle+"): "+getDescription(j));
637 }
638 le.remove(urle);
639 lj.remove(urlj);
640 // replace key for this entry with JOSM URL
641 eliUrls.remove(urle);
642 eliUrls.put(urlj, e);
643 return true;
644 }
645 return false;
646 }
647
648 void checkCommonEntries() {
649 doSameUrlButDifferentName();
650 doSameUrlButDifferentId();
651 doSameUrlButDifferentType();
652 doSameUrlButDifferentZoomBounds();
653 doSameUrlButDifferentCountryCode();
654 doSameUrlButDifferentQuality();
655 doSameUrlButDifferentDates();
656 doSameUrlButDifferentInformation();
657 doMismatchingShapes();
658 doMismatchingIcons();
659 doMismatchingCategories();
660 doMiscellaneousChecks();
661 }
662
663 void doSameUrlButDifferentName() {
664 myprintln("*** Same URL, but different name: ***");
665 for (String url : eliUrls.keySet()) {
666 JsonObject e = eliUrls.get(url);
667 if (!josmUrls.containsKey(url)) continue;
668 ImageryInfo j = josmUrls.get(url);
669 String ename = getName(e).replace("'", "\u2019");
670 String jname = getName(j).replace("'", "\u2019");
671 if (!ename.equals(jname)) {
672 myprintln("* Name differs ('"+getName(e)+"' != '"+getName(j)+"'): "+getUrl(j));
673 }
674 }
675 }
676
677 void doSameUrlButDifferentId() {
678 myprintln("*** Same URL, but different Id: ***");
679 for (String url : eliUrls.keySet()) {
680 JsonObject e = eliUrls.get(url);
681 if (!josmUrls.containsKey(url)) continue;
682 ImageryInfo j = josmUrls.get(url);
683 String ename = getId(e);
684 String jname = getId(j);
685 if (!Objects.equals(ename, jname)) {
686 myprintln("# Id differs ('"+getId(e)+"' != '"+getId(j)+"'): "+getUrl(j));
687 }
688 }
689 }
690
691 void doSameUrlButDifferentType() {
692 myprintln("*** Same URL, but different type: ***");
693 for (String url : eliUrls.keySet()) {
694 JsonObject e = eliUrls.get(url);
695 if (!josmUrls.containsKey(url)) continue;
696 ImageryInfo j = josmUrls.get(url);
697 if (!Objects.equals(getType(e), getType(j))) {
698 myprintln("* Type differs ("+getType(e)+" != "+getType(j)+"): "+getName(j)+" - "+getUrl(j));
699 }
700 }
701 }
702
703 void doSameUrlButDifferentZoomBounds() {
704 myprintln("*** Same URL, but different zoom bounds: ***");
705 for (String url : eliUrls.keySet()) {
706 JsonObject e = eliUrls.get(url);
707 if (!josmUrls.containsKey(url)) continue;
708 ImageryInfo j = josmUrls.get(url);
709
710 Integer eMinZoom = getMinZoom(e);
711 Integer jMinZoom = getMinZoom(j);
712 /* dont warn for entries copied from the base of the mirror */
713 if (eMinZoom == null && "wms".equals(getType(j)) && j.getName().contains(" mirror"))
714 jMinZoom = null;
715 if (!Objects.equals(eMinZoom, jMinZoom) && !(Objects.equals(eMinZoom, 0) && jMinZoom == null)) {
716 myprintln("* Minzoom differs ("+eMinZoom+" != "+jMinZoom+"): "+getDescription(j));
717 }
718 Integer eMaxZoom = getMaxZoom(e);
719 Integer jMaxZoom = getMaxZoom(j);
720 /* dont warn for entries copied from the base of the mirror */
721 if (eMaxZoom == null && "wms".equals(getType(j)) && j.getName().contains(" mirror"))
722 jMaxZoom = null;
723 if (!Objects.equals(eMaxZoom, jMaxZoom)) {
724 myprintln("* Maxzoom differs ("+eMaxZoom+" != "+jMaxZoom+"): "+getDescription(j));
725 }
726 }
727 }
728
729 void doSameUrlButDifferentCountryCode() {
730 myprintln("*** Same URL, but different country code: ***");
731 for (String url : eliUrls.keySet()) {
732 JsonObject e = eliUrls.get(url);
733 if (!josmUrls.containsKey(url)) continue;
734 ImageryInfo j = josmUrls.get(url);
735 String cce = getCountryCode(e);
736 if ("ZZ".equals(cce)) { /* special ELI country code */
737 cce = null;
738 }
739 if (cce != null && !cce.equals(getCountryCode(j))) {
740 myprintln("* Country code differs ("+getCountryCode(e)+" != "+getCountryCode(j)+"): "+getDescription(j));
741 }
742 }
743 }
744
745 void doSameUrlButDifferentQuality() {
746 myprintln("*** Same URL, but different quality: ***");
747 for (String url : eliUrls.keySet()) {
748 JsonObject e = eliUrls.get(url);
749 if (!josmUrls.containsKey(url)) {
750 String q = getQuality(e);
751 if ("eli-best".equals(q)) {
752 myprintln("- Quality best entry not in JOSM for "+getDescription(e));
753 }
754 continue;
755 }
756 ImageryInfo j = josmUrls.get(url);
757 if (!Objects.equals(getQuality(e), getQuality(j))) {
758 myprintln("* Quality differs ("+getQuality(e)+" != "+getQuality(j)+"): "+getDescription(j));
759 }
760 }
761 }
762
763 void doSameUrlButDifferentDates() {
764 myprintln("*** Same URL, but different dates: ***");
765 Pattern pattern = Pattern.compile("^(.*;)(\\d\\d\\d\\d)(-(\\d\\d)(-(\\d\\d))?)?$");
766 for (String url : eliUrls.keySet()) {
767 String ed = getDate(eliUrls.get(url));
768 if (!josmUrls.containsKey(url)) continue;
769 ImageryInfo j = josmUrls.get(url);
770 String jd = getDate(j);
771 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
772 String ef = ed.replaceAll("\\A-;", "").replaceAll(";-\\z", "").replaceAll("\\A([0-9-]+);\\1\\z", "$1");
773 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
774 String ed2 = ed;
775 Matcher m = pattern.matcher(ed);
776 if (m.matches()) {
777 Calendar cal = Calendar.getInstance();
778 cal.set(Integer.valueOf(m.group(2)),
779 m.group(4) == null ? 0 : Integer.valueOf(m.group(4))-1,
780 m.group(6) == null ? 1 : Integer.valueOf(m.group(6)));
781 cal.add(Calendar.DAY_OF_MONTH, -1);
782 ed2 = m.group(1) + cal.get(Calendar.YEAR);
783 if (m.group(4) != null)
784 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1);
785 if (m.group(6) != null)
786 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH));
787 }
788 String ef2 = ed2.replaceAll("\\A-;", "").replaceAll(";-\\z", "").replaceAll("\\A([0-9-]+);\\1\\z", "$1");
789 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
790 String t = "'"+ed+"'";
791 if (!ed.equals(ef)) {
792 t += " or '"+ef+"'";
793 }
794 if (jd.isEmpty()) {
795 myprintln("- Missing JOSM date ("+t+"): "+getDescription(j));
796 } else if (!ed.isEmpty()) {
797 myprintln("* Date differs ('"+t+"' != '"+jd+"'): "+getDescription(j));
798 } else if (!optionNoEli) {
799 myprintln("+ Missing ELI date ('"+jd+"'): "+getDescription(j));
800 }
801 }
802 }
803 }
804
805 void doSameUrlButDifferentInformation() {
806 myprintln("*** Same URL, but different information: ***");
807 for (String url : eliUrls.keySet()) {
808 if (!josmUrls.containsKey(url)) continue;
809 JsonObject e = eliUrls.get(url);
810 ImageryInfo j = josmUrls.get(url);
811
812 compareDescriptions(e, j);
813 comparePrivacyPolicyUrls(e, j);
814 comparePermissionReferenceUrls(e, j);
815 compareAttributionUrls(e, j);
816 compareAttributionTexts(e, j);
817 compareProjections(e, j);
818 compareDefaults(e, j);
819 compareOverlays(e, j);
820 compareNoTileHeaders(e, j);
821 }
822 }
823
824 void compareDescriptions(JsonObject e, ImageryInfo j) {
825 String et = getDescriptions(e).getOrDefault("en", "");
826 String jt = getDescriptions(j).getOrDefault("en", "");
827 if (!et.equals(jt)) {
828 if (jt.isEmpty()) {
829 myprintln("- Missing JOSM description ("+et+"): "+getDescription(j));
830 } else if (!et.isEmpty()) {
831 myprintln("* Description differs ('"+et+"' != '"+jt+"'): "+getDescription(j));
832 } else if (!optionNoEli) {
833 myprintln("+ Missing ELI description ('"+jt+"'): "+getDescription(j));
834 }
835 }
836 }
837
838 void comparePrivacyPolicyUrls(JsonObject e, ImageryInfo j) {
839 String et = getPrivacyPolicyUrl(e);
840 String jt = getPrivacyPolicyUrl(j);
841 if (!Objects.equals(et, jt)) {
842 if (isBlank(jt)) {
843 myprintln("- Missing JOSM privacy policy URL ("+et+"): "+getDescription(j));
844 } else if (isNotBlank(et)) {
845 myprintln("* Privacy policy URL differs ('"+et+"' != '"+jt+"'): "+getDescription(j));
846 } else if (!optionNoEli) {
847 myprintln("+ Missing ELI privacy policy URL ('"+jt+"'): "+getDescription(j));
848 }
849 }
850 }
851
852 void comparePermissionReferenceUrls(JsonObject e, ImageryInfo j) {
853 String et = getPermissionReferenceUrl(e);
854 String jt = getPermissionReferenceUrl(j);
855 String jt2 = getTermsOfUseUrl(j);
856 if (isBlank(jt)) jt = jt2;
857 if (!Objects.equals(et, jt)) {
858 if (isBlank(jt)) {
859 myprintln("- Missing JOSM license URL ("+et+"): "+getDescription(j));
860 } else if (isNotBlank(et)) {
861 String ethttps = et.replace("http:", "https:");
862 if (isBlank(jt2) || !(jt2.equals(ethttps) || jt2.equals(et+"/") || jt2.equals(ethttps+"/"))) {
863 if (jt.equals(ethttps) || jt.equals(et+"/") || jt.equals(ethttps+"/")) {
864 myprintln("+ License URL differs ('"+et+"' != '"+jt+"'): "+getDescription(j));
865 } else {
866 String ja = getAttributionUrl(j);
867 if (ja != null && (ja.equals(et) || ja.equals(ethttps) || ja.equals(et+"/") || ja.equals(ethttps+"/"))) {
868 myprintln("+ ELI License URL in JOSM Attribution: "+getDescription(j));
869 } else {
870 myprintln("* License URL differs ('"+et+"' != '"+jt+"'): "+getDescription(j));
871 }
872 }
873 }
874 } else if (!optionNoEli) {
875 myprintln("+ Missing ELI license URL ('"+jt+"'): "+getDescription(j));
876 }
877 }
878 }
879
880 void compareAttributionUrls(JsonObject e, ImageryInfo j) {
881 String et = getAttributionUrl(e);
882 String jt = getAttributionUrl(j);
883 if (!Objects.equals(et, jt)) {
884 if (isBlank(jt)) {
885 myprintln("- Missing JOSM attribution URL ("+et+"): "+getDescription(j));
886 } else if (isNotBlank(et)) {
887 String ethttps = et.replace("http:", "https:");
888 if (jt.equals(ethttps) || jt.equals(et+"/") || jt.equals(ethttps+"/")) {
889 myprintln("+ Attribution URL differs ('"+et+"' != '"+jt+"'): "+getDescription(j));
890 } else {
891 myprintln("* Attribution URL differs ('"+et+"' != '"+jt+"'): "+getDescription(j));
892 }
893 } else if (!optionNoEli) {
894 myprintln("+ Missing ELI attribution URL ('"+jt+"'): "+getDescription(j));
895 }
896 }
897 }
898
899 void compareAttributionTexts(JsonObject e, ImageryInfo j) {
900 String et = getAttributionText(e);
901 String jt = getAttributionText(j);
902 if (!Objects.equals(et, jt)) {
903 if (isBlank(jt)) {
904 myprintln("- Missing JOSM attribution text ("+et+"): "+getDescription(j));
905 } else if (isNotBlank(et)) {
906 myprintln("* Attribution text differs ('"+et+"' != '"+jt+"'): "+getDescription(j));
907 } else if (!optionNoEli) {
908 myprintln("+ Missing ELI attribution text ('"+jt+"'): "+getDescription(j));
909 }
910 }
911 }
912
913 void compareProjections(JsonObject e, ImageryInfo j) {
914 String et = getProjections(e).stream().sorted().collect(Collectors.joining(" "));
915 String jt = getProjections(j).stream().sorted().collect(Collectors.joining(" "));
916 if (!Objects.equals(et, jt)) {
917 if (isBlank(jt)) {
918 String t = getType(e);
919 if ("wms_endpoint".equals(t) || "tms".equals(t)) {
920 myprintln("+ ELI projections for type "+t+": "+getDescription(j));
921 } else {
922 myprintln("- Missing JOSM projections ("+et+"): "+getDescription(j));
923 }
924 } else if (isNotBlank(et)) {
925 if ("EPSG:3857 EPSG:4326".equals(et) || "EPSG:3857".equals(et) || "EPSG:4326".equals(et)) {
926 myprintln("+ ELI has minimal projections ('"+et+"' != '"+jt+"'): "+getDescription(j));
927 } else {
928 myprintln("* Projections differ ('"+et+"' != '"+jt+"'): "+getDescription(j));
929 }
930 } else if (!optionNoEli && !"tms".equals(getType(e))) {
931 myprintln("+ Missing ELI projections ('"+jt+"'): "+getDescription(j));
932 }
933 }
934 }
935
936 void compareDefaults(JsonObject e, ImageryInfo j) {
937 boolean ed = getDefault(e);
938 boolean jd = getDefault(j);
939 if (ed != jd) {
940 if (!jd) {
941 myprintln("- Missing JOSM default: "+getDescription(j));
942 } else if (!optionNoEli) {
943 myprintln("+ Missing ELI default: "+getDescription(j));
944 }
945 }
946 }
947
948 void compareOverlays(JsonObject e, ImageryInfo j) {
949 boolean eo = getOverlay(e);
950 boolean jo = getOverlay(j);
951 if (eo != jo) {
952 if (!jo) {
953 myprintln("- Missing JOSM overlay flag: "+getDescription(j));
954 } else if (!optionNoEli) {
955 myprintln("+ Missing ELI overlay flag: "+getDescription(j));
956 }
957 }
958 }
959
960 void compareNoTileHeaders(JsonObject e, ImageryInfo j) {
961 Map<String, Set<String>> eh = getNoTileHeader(e);
962 Map<String, Set<String>> jh = getNoTileHeader(j);
963 if (!Objects.equals(eh, jh)) {
964 if (Utils.isEmpty(jh)) {
965 myprintln("- Missing JOSM no tile headers ("+eh+"): "+getDescription(j));
966 } else if (!Utils.isEmpty(eh)) {
967 myprintln("* No tile headers differ ('"+eh+"' != '"+jh+"'): "+getDescription(j));
968 } else if (!optionNoEli) {
969 myprintln("+ Missing ELI no tile headers ('"+jh+"'): "+getDescription(j));
970 }
971 }
972 }
973
974 void doMismatchingShapes() {
975 myprintln("*** Mismatching shapes: ***");
976 for (String url : josmUrls.keySet()) {
977 ImageryInfo j = josmUrls.get(url);
978 int num = 1;
979 for (Shape shape : getShapes(j)) {
980 List<Coordinate> p = shape.getPoints();
981 if (!p.get(0).equals(p.get(p.size()-1))) {
982 myprintln("~~~ JOSM shape "+num+" unclosed: "+getDescription(j));
983 }
984 for (int nump = 1; nump < p.size(); ++nump) {
985 if (Objects.equals(p.get(nump-1), p.get(nump))) {
986 myprintln("~~~ JOSM shape "+num+" double point at "+(nump-1)+": "+getDescription(j));
987 }
988 }
989 ++num;
990 }
991 }
992 for (String url : eliUrls.keySet()) {
993 JsonObject e = eliUrls.get(url);
994 int num = 1;
995 List<Shape> s = null;
996 try {
997 s = getShapes(e);
998 for (Shape shape : s) {
999 List<Coordinate> p = shape.getPoints();
1000 if (!p.get(0).equals(p.get(p.size()-1)) && !optionNoEli) {
1001 myprintln("+++ ELI shape "+num+" unclosed: "+getDescription(e));
1002 }
1003 for (int nump = 1; nump < p.size(); ++nump) {
1004 if (Objects.equals(p.get(nump-1), p.get(nump))) {
1005 myprintln("+++ ELI shape "+num+" double point at "+(nump-1)+": "+getDescription(e));
1006 }
1007 }
1008 ++num;
1009 }
1010 } catch (IllegalArgumentException err) {
1011 String desc = getDescription(e);
1012 myprintln("+++ ELI shape contains invalid data for "+desc+": "+err.getMessage());
1013 }
1014 if (s == null || !josmUrls.containsKey(url)) {
1015 continue;
1016 }
1017 ImageryInfo j = josmUrls.get(url);
1018 List<Shape> js = getShapes(j);
1019 if (s.isEmpty() && !js.isEmpty()) {
1020 if (!optionNoEli) {
1021 myprintln("+ No ELI shape: "+getDescription(j));
1022 }
1023 } else if (js.isEmpty() && !s.isEmpty()) {
1024 // don't report boundary like 5 point shapes as difference
1025 if (s.size() != 1 || s.get(0).getPoints().size() != 5) {
1026 myprintln("- No JOSM shape: "+getDescription(j));
1027 }
1028 } else if (s.size() != js.size()) {
1029 myprintln("* Different number of shapes ("+s.size()+" != "+js.size()+"): "+getDescription(j));
1030 } else {
1031 boolean[] edone = new boolean[s.size()];
1032 boolean[] jdone = new boolean[js.size()];
1033 for (int enums = 0; enums < s.size(); ++enums) {
1034 List<Coordinate> ep = s.get(enums).getPoints();
1035 for (int jnums = 0; jnums < js.size() && !edone[enums]; ++jnums) {
1036 List<Coordinate> jp = js.get(jnums).getPoints();
1037 if (ep.size() == jp.size() && !jdone[jnums]) {
1038 boolean err = false;
1039 for (int nump = 0; nump < ep.size() && !err; ++nump) {
1040 Coordinate ept = ep.get(nump);
1041 Coordinate jpt = jp.get(nump);
1042 if (differentCoordinate(ept.getLat(), jpt.getLat()) || differentCoordinate(ept.getLon(), jpt.getLon()))
1043 err = true;
1044 }
1045 if (!err) {
1046 edone[enums] = true;
1047 jdone[jnums] = true;
1048 break;
1049 }
1050 }
1051 }
1052 }
1053 for (int enums = 0; enums < s.size(); ++enums) {
1054 List<Coordinate> ep = s.get(enums).getPoints();
1055 for (int jnums = 0; jnums < js.size() && !edone[enums]; ++jnums) {
1056 List<Coordinate> jp = js.get(jnums).getPoints();
1057 if (ep.size() == jp.size() && !jdone[jnums]) {
1058 boolean err = false;
1059 for (int nump = 0; nump < ep.size() && !err; ++nump) {
1060 Coordinate ept = ep.get(nump);
1061 Coordinate jpt = jp.get(nump);
1062 if (differentCoordinate(ept.getLat(), jpt.getLat()) || differentCoordinate(ept.getLon(), jpt.getLon())) {
1063 String numtxt = Integer.toString(enums+1);
1064 if (enums != jnums) {
1065 numtxt += '/' + Integer.toString(jnums+1);
1066 }
1067 myprintln("* Different coordinate for point "+(nump+1)+" of shape "+numtxt+": "+getDescription(j));
1068 break;
1069 }
1070 }
1071 edone[enums] = true;
1072 jdone[jnums] = true;
1073 break;
1074 }
1075 }
1076 }
1077 for (int enums = 0; enums < s.size(); ++enums) {
1078 List<Coordinate> ep = s.get(enums).getPoints();
1079 for (int jnums = 0; jnums < js.size() && !edone[enums]; ++jnums) {
1080 List<Coordinate> jp = js.get(jnums).getPoints();
1081 if (!jdone[jnums]) {
1082 String numtxt = Integer.toString(enums+1);
1083 if (enums != jnums) {
1084 numtxt += '/' + Integer.toString(jnums+1);
1085 }
1086 myprintln("* Different number of points for shape "+numtxt+" ("+ep.size()+" ! = "+jp.size()+"): "
1087 + getDescription(j));
1088 edone[enums] = true;
1089 jdone[jnums] = true;
1090 break;
1091 }
1092 }
1093 }
1094 }
1095 }
1096 }
1097
1098 private boolean differentCoordinate(double v1, double v2) {
1099 double epsilon = 0.00001;
1100 return Math.abs(v1 - v2) > epsilon;
1101 }
1102
1103 void doMismatchingIcons() {
1104 myprintln("*** Mismatching icons: ***");
1105 doMismatching(this::compareIcons);
1106 }
1107
1108 void doMismatchingCategories() {
1109 myprintln("*** Mismatching categories: ***");
1110 doMismatching(this::compareCategories);
1111 }
1112
1113 void doMismatching(BiConsumer<ImageryInfo, JsonObject> comparator) {
1114 for (String url : eliUrls.keySet()) {
1115 if (josmUrls.containsKey(url)) {
1116 comparator.accept(josmUrls.get(url), eliUrls.get(url));
1117 }
1118 }
1119 }
1120
1121 void compareIcons(ImageryInfo j, JsonObject e) {
1122 String ij = getIcon(j);
1123 String ie = getIcon(e);
1124 boolean ijok = isNotBlank(ij);
1125 boolean ieok = isNotBlank(ie);
1126 if (ijok && !ieok) {
1127 if (!optionNoEli) {
1128 myprintln("+ No ELI icon: "+getDescription(j));
1129 }
1130 } else if (!ijok && ieok) {
1131 myprintln("- No JOSM icon: "+getDescription(j));
1132 } else if (ijok && ieok && !Objects.equals(ij, ie) && !(
1133 (ie.startsWith("https://osmlab.github.io/editor-layer-index/")
1134 || ie.startsWith("https://raw.githubusercontent.com/osmlab/editor-layer-index/")) &&
1135 ij.startsWith("data:"))) {
1136 String iehttps = ie.replace("http:", "https:");
1137 if (ij.equals(iehttps)) {
1138 myprintln("+ Different icons: "+getDescription(j));
1139 } else {
1140 myprintln("* Different icons: "+getDescription(j));
1141 }
1142 }
1143 }
1144
1145 void compareCategories(ImageryInfo j, JsonObject e) {
1146 String cj = getCategory(j);
1147 String ce = getCategory(e);
1148 boolean cjok = isNotBlank(cj);
1149 boolean ceok = isNotBlank(ce);
1150 if (cjok && !ceok) {
1151 if (!optionNoEli) {
1152 myprintln("+ No ELI category: "+getDescription(j));
1153 }
1154 } else if (!cjok && ceok) {
1155 myprintln("- No JOSM category: "+getDescription(j));
1156 } else if (cjok && ceok && !Objects.equals(cj, ce)) {
1157 myprintln("* Different categories ('"+ce+"' != '"+cj+"'): "+getDescription(j));
1158 }
1159 }
1160
1161 void doMiscellaneousChecks() {
1162 myprintln("*** Miscellaneous checks: ***");
1163 Map<String, ImageryInfo> josmIds = new HashMap<>();
1164 Collection<String> all = Projections.getAllProjectionCodes();
1165 DomainValidator dv = DomainValidator.getInstance();
1166 for (String url : josmUrls.keySet()) {
1167 ImageryInfo j = josmUrls.get(url);
1168 String id = getId(j);
1169 if ("wms".equals(getType(j))) {
1170 String urlLc = url.toLowerCase(Locale.ENGLISH);
1171 if (getProjections(j).isEmpty()) {
1172 myprintln("~ WMS without projections: "+getDescription(j));
1173 } else {
1174 List<String> unsupported = new LinkedList<>();
1175 List<String> old = new LinkedList<>();
1176 for (String p : getProjectionsUnstripped(j)) {
1177 if ("CRS:84".equals(p)) {
1178 if (!urlLc.contains("version=1.3")) {
1179 myprintln("~ CRS:84 without WMS 1.3: "+getDescription(j));
1180 }
1181 } else if (oldproj.containsKey(p)) {
1182 old.add(p);
1183 } else if (!all.contains(p) && !ignoreproj.contains(p)) {
1184 unsupported.add(p);
1185 }
1186 }
1187 if (!unsupported.isEmpty()) {
1188 myprintln("~ Projections "+String.join(", ", unsupported)+" not supported by JOSM: "+getDescription(j));
1189 }
1190 for (String o : old) {
1191 myprintln("~ Projection "+o+" is an old unsupported code and has been replaced by "+oldproj.get(o)+": "
1192 + getDescription(j));
1193 }
1194 }
1195 if (urlLc.contains("version=1.3") && !urlLc.contains("crs={proj}")) {
1196 myprintln("~ WMS 1.3 with strange CRS specification: "+getDescription(j));
1197 } else if (urlLc.contains("version=1.1") && !urlLc.contains("srs={proj}")) {
1198 myprintln("~ WMS 1.1 with strange SRS specification: "+getDescription(j));
1199 }
1200 }
1201 List<String> urls = new LinkedList<>();
1202 if (!"scanex".equals(getType(j))) {
1203 urls.add(url);
1204 }
1205 String jt = getPermissionReferenceUrl(j);
1206 if (isNotBlank(jt) && !"Public Domain".equalsIgnoreCase(jt))
1207 urls.add(jt);
1208 jt = getTermsOfUseUrl(j);
1209 if (isNotBlank(jt))
1210 urls.add(jt);
1211 jt = getAttributionUrl(j);
1212 if (isNotBlank(jt))
1213 urls.add(jt);
1214 jt = getIcon(j);
1215 if (isNotBlank(jt)) {
1216 if (!jt.startsWith("data:image/"))
1217 urls.add(jt);
1218 else {
1219 try {
1220 new ImageProvider(jt).get();
1221 } catch (RuntimeException e) {
1222 myprintln("~ Strange Icon: "+getDescription(j));
1223 }
1224 }
1225 }
1226 Pattern patternU = Pattern.compile("^https?://([^/]+?)(:\\d+)?(/.*)?");
1227 for (String u : urls) {
1228 if (!patternU.matcher(u).matches() || u.matches(".*[ \t]+$")) {
1229 myprintln("~ Strange URL '"+u+"': "+getDescription(j));
1230 } else {
1231 try {
1232 URL jurl = new URL(u.replaceAll("\\{switch:[^\\}]*\\}", "x"));
1233 String domain = jurl.getHost();
1234 int port = jurl.getPort();
1235 if (!(domain.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$")) && !dv.isValid(domain))
1236 myprintln("~ Strange Domain '"+domain+"': "+getDescription(j));
1237 else if (80 == port || 443 == port) {
1238 myprintln("~ Useless port '"+port+"': "+getDescription(j));
1239 }
1240 } catch (MalformedURLException e) {
1241 myprintln("~ Malformed URL '"+u+"': "+getDescription(j)+" => "+e.getMessage());
1242 }
1243 }
1244 }
1245
1246 if (josmMirrors.containsKey(url)) {
1247 continue;
1248 }
1249 if (isBlank(id)) {
1250 myprintln("~ No JOSM-ID: "+getDescription(j));
1251 } else if (josmIds.containsKey(id)) {
1252 myprintln("~ JOSM-ID "+id+" not unique: "+getDescription(j));
1253 } else {
1254 josmIds.put(id, j);
1255 }
1256 String d = getDate(j);
1257 if (isNotBlank(d)) {
1258 Pattern patternD = Pattern.compile("^(-|(\\d\\d\\d\\d)(-(\\d\\d)(-(\\d\\d))?)?)(;(-|(\\d\\d\\d\\d)(-(\\d\\d)(-(\\d\\d))?)?))?$");
1259 Matcher m = patternD.matcher(d);
1260 if (!m.matches()) {
1261 myprintln("~ JOSM-Date '"+d+"' is strange: "+getDescription(j));
1262 } else {
1263 try {
1264 Date first = verifyDate(m.group(2), m.group(4), m.group(6));
1265 Date second = verifyDate(m.group(9), m.group(11), m.group(13));
1266 if (second.compareTo(first) < 0) {
1267 myprintln("~ JOSM-Date '"+d+"' is strange (second earlier than first): "+getDescription(j));
1268 }
1269 } catch (Exception e) {
1270 myprintln("~ JOSM-Date '"+d+"' is strange ("+e.getMessage()+"): "+getDescription(j));
1271 }
1272 }
1273 }
1274 if (isNotBlank(getAttributionUrl(j)) && isBlank(getAttributionText(j))) {
1275 myprintln("~ Attribution link without text: "+getDescription(j));
1276 }
1277 if (isNotBlank(getLogoUrl(j)) && isBlank(getLogoImage(j))) {
1278 myprintln("~ Logo link without image: "+getDescription(j));
1279 }
1280 if (isNotBlank(getTermsOfUseText(j)) && isBlank(getTermsOfUseUrl(j))) {
1281 myprintln("~ Terms of Use text without link: "+getDescription(j));
1282 }
1283 List<Shape> js = getShapes(j);
1284 if (!js.isEmpty()) {
1285 double minlat = 1000;
1286 double minlon = 1000;
1287 double maxlat = -1000;
1288 double maxlon = -1000;
1289 for (Shape s: js) {
1290 for (Coordinate p: s.getPoints()) {
1291 double lat = p.getLat();
1292 double lon = p.getLon();
1293 if (lat > maxlat) maxlat = lat;
1294 if (lon > maxlon) maxlon = lon;
1295 if (lat < minlat) minlat = lat;
1296 if (lon < minlon) minlon = lon;
1297 }
1298 }
1299 ImageryBounds b = j.getBounds();
1300 if (differentCoordinate(b.getMinLat(), minlat)
1301 || differentCoordinate(b.getMinLon(), minlon)
1302 || differentCoordinate(b.getMaxLat(), maxlat)
1303 || differentCoordinate(b.getMaxLon(), maxlon)) {
1304 myprintln("~ Bounds do not match shape (is "+b.getMinLat()+","+b.getMinLon()+","+b.getMaxLat()+","+b.getMaxLon()
1305 + ", calculated <bounds min-lat='"+minlat+"' min-lon='"+minlon+"' max-lat='"+maxlat+"' max-lon='"+maxlon+"'>): "
1306 + getDescription(j));
1307 }
1308 }
1309 List<String> knownCategories = Arrays.asList(
1310 "photo", "elevation", "map", "historicmap", "osmbasedmap", "historicphoto", "qa", "other");
1311 String cat = getCategory(j);
1312 if (isBlank(cat)) {
1313 myprintln("~ No category: "+getDescription(j));
1314 } else if (!knownCategories.contains(cat)) {
1315 myprintln("~ Strange category "+cat+": "+getDescription(j));
1316 }
1317 }
1318 }
1319
1320 /*
1321 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
1322 */
1323
1324 static String getUrl(Object e) {
1325 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getUrl();
1326 return ((Map<String, JsonObject>) e).get("properties").getString("url");
1327 }
1328
1329 static String getUrlStripped(Object e) {
1330 return getUrl(e).replaceAll("\\?(apikey|access_token)=.*", "");
1331 }
1332
1333 static String getDate(Object e) {
1334 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getDate() != null ? ((ImageryInfo) e).getDate() : "";
1335 JsonObject p = ((Map<String, JsonObject>) e).get("properties");
1336 String start = p.containsKey("start_date") ? p.getString("start_date") : "";
1337 String end = p.containsKey("end_date") ? p.getString("end_date") : "";
1338 if (!start.isEmpty() && !end.isEmpty())
1339 return start+";"+end;
1340 else if (!start.isEmpty())
1341 return start+";-";
1342 else if (!end.isEmpty())
1343 return "-;"+end;
1344 return "";
1345 }
1346
1347 static Date verifyDate(String year, String month, String day) throws ParseException {
1348 String date;
1349 if (year == null) {
1350 date = "3000-01-01";
1351 } else {
1352 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day);
1353 }
1354 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
1355 df.setLenient(false);
1356 return df.parse(date);
1357 }
1358
1359 static String getId(Object e) {
1360 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getId();
1361 return ((Map<String, JsonObject>) e).get("properties").getString("id");
1362 }
1363
1364 static String getName(Object e) {
1365 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getOriginalName();
1366 return ((Map<String, JsonObject>) e).get("properties").getString("name");
1367 }
1368
1369 static List<ImageryInfo> getMirrors(Object e) {
1370 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getMirrors();
1371 return Collections.emptyList();
1372 }
1373
1374 static List<String> getProjections(Object e) {
1375 List<String> r = new ArrayList<>();
1376 List<String> u = getProjectionsUnstripped(e);
1377 if (u != null) {
1378 for (String p : u) {
1379 if (!oldproj.containsKey(p) && !("CRS:84".equals(p) && !(getUrlStripped(e).matches("(?i)version=1\\.3")))) {
1380 r.add(p);
1381 }
1382 }
1383 }
1384 return r;
1385 }
1386
1387 static List<String> getProjectionsUnstripped(Object e) {
1388 List<String> r = null;
1389 if (e instanceof ImageryInfo) {
1390 r = ((ImageryInfo) e).getServerProjections();
1391 } else {
1392 JsonValue s = ((Map<String, JsonObject>) e).get("properties").get("available_projections");
1393 if (s != null) {
1394 r = new ArrayList<>();
1395 for (JsonValue p : s.asJsonArray()) {
1396 r.add(((JsonString) p).getString());
1397 }
1398 }
1399 }
1400 return r != null ? r : Collections.emptyList();
1401 }
1402
1403 static void addJsonShapes(List<Shape> l, JsonArray a) {
1404 if (a.get(0).asJsonArray().get(0) instanceof JsonArray) {
1405 for (JsonValue sub: a.asJsonArray()) {
1406 addJsonShapes(l, sub.asJsonArray());
1407 }
1408 } else {
1409 Shape s = new Shape();
1410 for (JsonValue point: a.asJsonArray()) {
1411 JsonArray ar = point.asJsonArray();
1412 String lon = ar.getJsonNumber(0).toString();
1413 String lat = ar.getJsonNumber(1).toString();
1414 s.addPoint(lat, lon);
1415 }
1416 l.add(s);
1417 }
1418 }
1419
1420 static List<Shape> getShapes(Object e) {
1421 if (e instanceof ImageryInfo) {
1422 ImageryBounds bounds = ((ImageryInfo) e).getBounds();
1423 if (bounds != null) {
1424 return bounds.getShapes();
1425 }
1426 return Collections.emptyList();
1427 }
1428 JsonValue ex = ((Map<String, JsonValue>) e).get("geometry");
1429 if (ex != null && !JsonValue.NULL.equals(ex) && !ex.asJsonObject().isNull("coordinates")) {
1430 JsonArray poly = ex.asJsonObject().getJsonArray("coordinates");
1431 List<Shape> l = new ArrayList<>();
1432 for (JsonValue shapes: poly) {
1433 addJsonShapes(l, shapes.asJsonArray());
1434 }
1435 return l;
1436 }
1437 return Collections.emptyList();
1438 }
1439
1440 static String getType(Object e) {
1441 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getImageryType().getTypeString();
1442 return ((Map<String, JsonObject>) e).get("properties").getString("type");
1443 }
1444
1445 static Integer getMinZoom(Object e) {
1446 if (e instanceof ImageryInfo) {
1447 int mz = ((ImageryInfo) e).getMinZoom();
1448 return mz == 0 ? null : mz;
1449 } else {
1450 JsonNumber num = ((Map<String, JsonObject>) e).get("properties").getJsonNumber("min_zoom");
1451 if (num == null) return null;
1452 return num.intValue();
1453 }
1454 }
1455
1456 static Integer getMaxZoom(Object e) {
1457 if (e instanceof ImageryInfo) {
1458 int mz = ((ImageryInfo) e).getMaxZoom();
1459 return mz == 0 ? null : mz;
1460 } else {
1461 JsonNumber num = ((Map<String, JsonObject>) e).get("properties").getJsonNumber("max_zoom");
1462 if (num == null) return null;
1463 return num.intValue();
1464 }
1465 }
1466
1467 static String getCountryCode(Object e) {
1468 if (e instanceof ImageryInfo) return "".equals(((ImageryInfo) e).getCountryCode()) ? null : ((ImageryInfo) e).getCountryCode();
1469 return ((Map<String, JsonObject>) e).get("properties").getString("country_code", null);
1470 }
1471
1472 static String getQuality(Object e) {
1473 if (e instanceof ImageryInfo) return ((ImageryInfo) e).isBestMarked() ? "eli-best" : null;
1474 return (((Map<String, JsonObject>) e).get("properties").containsKey("best")
1475 && ((Map<String, JsonObject>) e).get("properties").getBoolean("best")) ? "eli-best" : null;
1476 }
1477
1478 static boolean getOverlay(Object e) {
1479 if (e instanceof ImageryInfo) return ((ImageryInfo) e).isOverlay();
1480 return (((Map<String, JsonObject>) e).get("properties").containsKey("overlay")
1481 && ((Map<String, JsonObject>) e).get("properties").getBoolean("overlay"));
1482 }
1483
1484 static String getIcon(Object e) {
1485 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getIcon();
1486 return ((Map<String, JsonObject>) e).get("properties").getString("icon", null);
1487 }
1488
1489 static String getAttributionText(Object e) {
1490 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getAttributionText(0, null, null);
1491 try {
1492 return ((Map<String, JsonObject>) e).get("properties").getJsonObject("attribution").getString("text", null);
1493 } catch (NullPointerException ex) {
1494 return null;
1495 }
1496 }
1497
1498 static String getAttributionUrl(Object e) {
1499 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getAttributionLinkURL();
1500 try {
1501 return ((Map<String, JsonObject>) e).get("properties").getJsonObject("attribution").getString("url", null);
1502 } catch (NullPointerException ex) {
1503 return null;
1504 }
1505 }
1506
1507 static String getTermsOfUseText(Object e) {
1508 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getTermsOfUseText();
1509 return null;
1510 }
1511
1512 static String getTermsOfUseUrl(Object e) {
1513 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getTermsOfUseURL();
1514 return null;
1515 }
1516
1517 static String getCategory(Object e) {
1518 if (e instanceof ImageryInfo) {
1519 return ((ImageryInfo) e).getImageryCategoryOriginalString();
1520 }
1521 return ((Map<String, JsonObject>) e).get("properties").getString("category", null);
1522 }
1523
1524 static String getLogoImage(Object e) {
1525 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getAttributionImageRaw();
1526 return null;
1527 }
1528
1529 static String getLogoUrl(Object e) {
1530 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getAttributionImageURL();
1531 return null;
1532 }
1533
1534 static String getPermissionReferenceUrl(Object e) {
1535 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getPermissionReferenceURL();
1536 return ((Map<String, JsonObject>) e).get("properties").getString("license_url", null);
1537 }
1538
1539 static String getPrivacyPolicyUrl(Object e) {
1540 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getPrivacyPolicyURL();
1541 return ((Map<String, JsonObject>) e).get("properties").getString("privacy_policy_url", null);
1542 }
1543
1544 static Map<String, Set<String>> getNoTileHeader(Object e) {
1545 if (e instanceof ImageryInfo) return ((ImageryInfo) e).getNoTileHeaders();
1546 JsonObject nth = ((Map<String, JsonObject>) e).get("properties").getJsonObject("no_tile_header");
1547 return nth == null ? null : nth.keySet().stream().collect(Collectors.toMap(
1548 Function.identity(),
1549 k -> nth.getJsonArray(k).stream().map(x -> ((JsonString) x).getString()).collect(Collectors.toSet())));
1550 }
1551
1552 static Map<String, String> getDescriptions(Object e) {
1553 Map<String, String> res = new HashMap<>();
1554 if (e instanceof ImageryInfo) {
1555 String a = ((ImageryInfo) e).getDescription();
1556 if (a != null) res.put("en", a);
1557 } else {
1558 String a = ((Map<String, JsonObject>) e).get("properties").getString("description", null);
1559 if (a != null) res.put("en", a.replaceAll("''", "'"));
1560 }
1561 return res;
1562 }
1563
1564 static boolean getValidGeoreference(Object e) {
1565 if (e instanceof ImageryInfo) return ((ImageryInfo) e).isGeoreferenceValid();
1566 return false;
1567 }
1568
1569 static boolean getDefault(Object e) {
1570 if (e instanceof ImageryInfo) return ((ImageryInfo) e).isDefaultEntry();
1571 return ((Map<String, JsonObject>) e).get("properties").getBoolean("default", false);
1572 }
1573
1574 String getDescription(Object o) {
1575 String url = getUrl(o);
1576 String cc = getCountryCode(o);
1577 if (cc == null) {
1578 ImageryInfo j = josmUrls.get(url);
1579 if (j != null) cc = getCountryCode(j);
1580 if (cc == null) {
1581 JsonObject e = eliUrls.get(url);
1582 if (e != null) cc = getCountryCode(e);
1583 }
1584 }
1585 if (cc == null) {
1586 cc = "";
1587 } else {
1588 cc = "["+cc+"] ";
1589 }
1590 String name = getName(o);
1591 String id = getId(o);
1592 String d = cc;
1593 if (!Utils.isEmpty(name)) {
1594 d += name;
1595 if (!Utils.isEmpty(id))
1596 d += " ["+id+"]";
1597 } else if (!Utils.isEmpty(url))
1598 d += url;
1599 if (optionShorten) {
1600 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "...";
1601 }
1602 return d;
1603 }
1604}
Note: See TracBrowser for help on using the repository browser.