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

Last change on this file since 15134 was 15082, checked in by Don-vip, 7 years ago

see #17730 - compare no tile headers, refactor

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