source: josm/trunk/scripts/SyncEditorLayerIndex.groovy@ 13997

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

see #16448 - dont skip zoomlevels for WMS mirrors totally

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