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

Last change on this file since 18870 was 18801, checked in by taylor.smock, 19 months ago

Fix #22832: Code cleanup and some simplification, documentation fixes (patch by gaben)

There should not be any functional changes in this patch; it is intended to do
the following:

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