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

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

checkstyle/PMD

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