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

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

see #14655 - fix bug in sync tool

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