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

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

Hasta la vista Groovy

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