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

Last change on this file since 15869 was 15851, checked in by Don-vip, 6 years ago

fix recent spotbugs/checkstyle/javadoc issues

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