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

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

update to Groovy 2.5.0 and Eclipse Photon (4.8.0)

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