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

Last change on this file since 16333 was 16127, checked in by Don-vip, 4 years ago

see #17285 - add privacy-policy-url

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