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

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

add id compare when URL mismatchs

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