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

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

remove import requiring Groovy >= 2.3

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