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

Last change on this file since 15769 was 15712, checked in by stoecker, 6 years ago

see #18172 - reactivate cdata, seems Groovy and Java handle RegExp differently

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