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

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

enable new checks

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