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

Last change on this file since 13549 was 13549, checked in by Klumbumbus, 6 years ago

see #14655 - enable overlay test

  • Property svn:eol-style set to native
File size: 42.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 stream.write(" <entry"
275 + ("eli-best".equals(getQuality(e)) ? " eli-best=\"true\"" : "" )
276 + (getOverlay(e) ? " overlay=\"true\"" : "" )
277 + ">\n")
278 stream.write " <name>${cdata(getName(e), true)}</name>\n"
279 stream.write " <id>${getId(e)}</id>\n"
280 def t
281 if((t = getDate(e)))
282 stream.write " <date>$t</date>\n"
283 if((t = getCountryCode(e)))
284 stream.write " <country-code>$t</country-code>\n"
285 if((getDefault(e)))
286 stream.write " <default>true</default>\n"
287 stream.write maininfo(e, " ")
288 if((t = getAttributionText(e)))
289 stream.write " <attribution-text mandatory=\"true\">${cdata(t, true)}</attribution-text>\n"
290 if((t = getAttributionUrl(e)))
291 stream.write " <attribution-url>${cdata(t)}</attribution-url>\n"
292 if((t = getLogoImage(e)))
293 stream.write " <logo-image>${cdata(t, true)}</logo-image>\n"
294 if((t = getLogoUrl(e)))
295 stream.write " <logo-url>${cdata(t)}</logo-url>\n"
296 if((t = getTermsOfUseText(e)))
297 stream.write " <terms-of-use-text>${cdata(t, true)}</terms-of-use-text>\n"
298 if((t = getTermsOfUseUrl(e)))
299 stream.write " <terms-of-use-url>${cdata(t)}</terms-of-use-url>\n"
300 if((t = getPermissionReferenceUrl(e)))
301 stream.write " <permission-ref>${cdata(t)}</permission-ref>\n"
302 if((getValidGeoreference(e)))
303 stream.write " <valid-georeference>true</valid-georeference>\n"
304 if((t = getIcon(e)))
305 stream.write " <icon>${cdata(t)}</icon>\n"
306 for (def d : getDescriptions(e)) {
307 stream.write " <description lang=\"${d.getKey()}\">${d.getValue()}</description>\n"
308 }
309 for (def m : getMirrors(e)) {
310 stream.write " <mirror>\n"+maininfo(m, " ")+" </mirror>\n"
311 }
312 def minlat = 1000
313 def minlon = 1000
314 def maxlat = -1000
315 def maxlon = -1000
316 def shapes = ""
317 def sep = "\n "
318 for(def s: getShapes(e)) {
319 shapes += " <shape>"
320 def i = 0
321 for(def p: s.getPoints()) {
322 def lat = p.getLat()
323 def lon = p.getLon()
324 if(lat > maxlat) maxlat = lat
325 if(lon > maxlon) maxlon = lon
326 if(lat < minlat) minlat = lat
327 if(lon < minlon) minlon = lon
328 if(!(i++%3)) {
329 shapes += sep + " "
330 }
331 shapes += "<point lat='${df.format(lat)}' lon='${df.format(lon)}'/>"
332 }
333 shapes += sep + "</shape>\n"
334 }
335 if(shapes) {
336 stream.write " <bounds min-lat='${df.format(minlat)}' min-lon='${df.format(minlon)}' max-lat='${df.format(maxlat)}' max-lon='${df.format(maxlon)}'>\n"
337 stream.write shapes + " </bounds>\n"
338 }
339 stream.write " </entry>\n"
340 }
341 stream.write "</imagery>\n"
342 stream.close()
343 }
344
345 void loadJosmEntries() {
346 def reader = new ImageryReader(josmInputFile)
347 josmEntries = reader.parse()
348
349 for (def e : josmEntries) {
350 def url = getUrlStripped(e)
351 if (url.contains("{z}")) {
352 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
353 url = url.replace("{z}","{zoom}")
354 }
355 if (josmUrls.containsKey(url)) {
356 myprintln "+++ JOSM-URL is not unique: "+url
357 } else {
358 josmUrls.put(url, e)
359 }
360 for (def m : e.getMirrors()) {
361 url = getUrlStripped(m)
362 m.origName = m.getOriginalName().replaceAll(" mirror server( \\d+)?","")
363 if (josmUrls.containsKey(url)) {
364 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
365 } else {
366 josmUrls.put(url, m)
367 josmMirrors.put(url, m)
368 }
369 }
370 }
371 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
372 }
373
374 List inOneButNotTheOther(Map m1, Map m2, String code, String https) {
375 def l = []
376 def k = new LinkedList<String>(m1.keySet())
377 for (def url : k) {
378 if (!m2.containsKey(url)) {
379 String urlhttps = url.replace("http:","https:")
380 if(!https || !m2.containsKey(urlhttps))
381 {
382 def name = getName(m1.get(url))
383 l += code+" "+getDescription(m1.get(url))
384 }
385 else
386 {
387 l += https+" Missing https: "+getDescription(m1.get(url))
388 m1.put(urlhttps, m1.get(url))
389 m1.remove(url)
390 }
391 }
392 }
393 l.sort()
394 }
395
396 void checkInOneButNotTheOther() {
397 def l1 = inOneButNotTheOther(eliUrls, josmUrls, "-", "+")
398 myprintln "*** URLs found in ELI but not in JOSM (${l1.size()}): ***"
399 if (!l1.isEmpty()) {
400 for (def l : l1) {
401 myprintln l
402 }
403 }
404
405 def l2 = inOneButNotTheOther(josmUrls, eliUrls, "+", "")
406 myprintln "*** URLs found in JOSM but not in ELI (${l2.size()}): ***"
407 if (!l2.isEmpty()) {
408 for (def l : l2) {
409 myprintln l
410 }
411 }
412 }
413
414 void checkCommonEntries() {
415 myprintln "*** Same URL, but different name: ***"
416 for (def url : eliUrls.keySet()) {
417 def e = eliUrls.get(url)
418 if (!josmUrls.containsKey(url)) continue
419 def j = josmUrls.get(url)
420 def ename = getName(e).replace("'","\u2019")
421 def jname = getName(j).replace("'","\u2019")
422 if (!ename.equals(jname)) {
423 myprintln "* Name differs ('${getName(e)}' != '${getName(j)}'): ${getUrl(j)}"
424 }
425 }
426
427 myprintln "*** Same URL, but different Id: ***"
428 for (def url : eliUrls.keySet()) {
429 def e = eliUrls.get(url)
430 if (!josmUrls.containsKey(url)) continue
431 def j = josmUrls.get(url)
432 def ename = getId(e)
433 def jname = getId(j)
434 if (!ename.equals(jname)) {
435 myprintln "# Id differs ('${getId(e)}' != '${getId(j)}'): ${getUrl(j)}"
436 }
437 }
438
439 myprintln "*** Same URL, but different type: ***"
440 for (def url : eliUrls.keySet()) {
441 def e = eliUrls.get(url)
442 if (!josmUrls.containsKey(url)) continue
443 def j = josmUrls.get(url)
444 if (!getType(e).equals(getType(j))) {
445 myprintln "* Type differs (${getType(e)} != ${getType(j)}): ${getName(j)} - ${getUrl(j)}"
446 }
447 }
448
449 myprintln "*** Same URL, but different zoom bounds: ***"
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
455 Integer eMinZoom = getMinZoom(e)
456 Integer jMinZoom = getMinZoom(j)
457 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
458 myprintln "* Minzoom differs (${eMinZoom} != ${jMinZoom}): ${getDescription(j)}"
459 }
460 Integer eMaxZoom = getMaxZoom(e)
461 Integer jMaxZoom = getMaxZoom(j)
462 if (eMaxZoom != jMaxZoom) {
463 myprintln "* Maxzoom differs (${eMaxZoom} != ${jMaxZoom}): ${getDescription(j)}"
464 }
465 }
466
467 myprintln "*** Same URL, but different country code: ***"
468 for (def url : eliUrls.keySet()) {
469 def e = eliUrls.get(url)
470 if (!josmUrls.containsKey(url)) continue
471 def j = josmUrls.get(url)
472 if (!getCountryCode(e).equals(getCountryCode(j))) {
473 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
474 }
475 }
476 myprintln "*** Same URL, but different quality: ***"
477 for (def url : eliUrls.keySet()) {
478 def e = eliUrls.get(url)
479 if (!josmUrls.containsKey(url)) {
480 def q = getQuality(e)
481 if("eli-best".equals(q)) {
482 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
483 }
484 continue
485 }
486 def j = josmUrls.get(url)
487 if (!getQuality(e).equals(getQuality(j))) {
488 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
489 }
490 }
491 myprintln "*** Same URL, but different dates: ***"
492 for (def url : eliUrls.keySet()) {
493 def ed = getDate(eliUrls.get(url))
494 if (!josmUrls.containsKey(url)) continue
495 def j = josmUrls.get(url)
496 def jd = getDate(j)
497 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
498 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
499 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
500 String ed2 = ed
501 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
502 if(reg != null && reg.count == 1) {
503 Calendar cal = Calendar.getInstance()
504 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)
505 cal.add(Calendar.DAY_OF_MONTH, -1)
506 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
507 if (reg[0][4] != null)
508 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
509 if (reg[0][6] != null)
510 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
511 }
512 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
513 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
514 String t = "'${ed}'"
515 if (!ed.equals(ef)) {
516 t += " or '${ef}'"
517 }
518 if (jd.isEmpty()) {
519 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
520 } else if (!ed.isEmpty()) {
521 myprintln "* Date differs ('${t}' != '${jd}'): ${getDescription(j)}"
522 } else if (!options.nomissingeli) {
523 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
524 }
525 }
526 }
527 myprintln "*** Same URL, but different information: ***"
528 for (def url : eliUrls.keySet()) {
529 if (!josmUrls.containsKey(url)) continue
530 def e = eliUrls.get(url)
531 def j = josmUrls.get(url)
532
533 def et = getDescriptions(e)
534 def jt = getDescriptions(j)
535 et = (et.size() > 0) ? et["en"] : ""
536 jt = (jt.size() > 0) ? jt["en"] : ""
537 if (!et.equals(jt)) {
538 if (!jt) {
539 myprintln "- Missing JOSM description (${et}): ${getDescription(j)}"
540 } else if (et) {
541 myprintln "* Description differs ('${et}' != '${jt}'): ${getDescription(j)}"
542 } else if (!options.nomissingeli) {
543 myprintln "+ Missing ELI description ('${jt}'): ${getDescription(j)}"
544 }
545 }
546
547 et = getPermissionReferenceUrl(e)
548 jt = getPermissionReferenceUrl(j)
549 if (!jt) jt = getTermsOfUseUrl(j)
550 if (!et.equals(jt)) {
551 if (!jt) {
552 myprintln "- Missing JOSM license URL (${et}): ${getDescription(j)}"
553 } else if (et) {
554 myprintln "* License URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
555 } else if (!options.nomissingeli) {
556 myprintln "+ Missing ELI license URL ('${jt}'): ${getDescription(j)}"
557 }
558 }
559
560 et = getAttributionUrl(e)
561 jt = getAttributionUrl(j)
562 if (!et.equals(jt)) {
563 if (!jt) {
564 myprintln "- Missing JOSM attribution URL (${et}): ${getDescription(j)}"
565 } else if (et) {
566 def ethttps = et.replace("http:","https:")
567 if(jt.equals(ethttps)) {
568 myprintln "+ Attribution URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
569 } else {
570 myprintln "* Attribution URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
571 }
572 } else if (!options.nomissingeli) {
573 myprintln "+ Missing ELI attribution URL ('${jt}'): ${getDescription(j)}"
574 }
575 }
576
577 et = getAttributionText(e)
578 jt = getAttributionText(j)
579 if (!et.equals(jt)) {
580 if (!jt) {
581 myprintln "- Missing JOSM attribution text (${et}): ${getDescription(j)}"
582 } else if (et) {
583 myprintln "* Attribution text differs ('${et}' != '${jt}'): ${getDescription(j)}"
584 } else if (!options.nomissingeli) {
585 myprintln "+ Missing ELI attribution text ('${jt}'): ${getDescription(j)}"
586 }
587 }
588
589 et = getProjections(e)
590 jt = getProjections(j)
591 if (et) { et = new LinkedList(et); Collections.sort(et); et = String.join(" ", et) }
592 if (jt) { jt = new LinkedList(jt); Collections.sort(jt); jt = String.join(" ", jt) }
593 if (!et.equals(jt)) {
594 if (!jt) {
595 myprintln "- Missing JOSM projections (${et}): ${getDescription(j)}"
596 } else if (et) {
597 if("EPSG:3857 EPSG:4326".equals(et) || "EPSG:3857".equals(et) || "EPSG:4326".equals(et)) {
598 myprintln "+ ELI has minimal projections ('${et}' != '${jt}'): ${getDescription(j)}"
599 } else {
600 myprintln "* Projections differ ('${et}' != '${jt}'): ${getDescription(j)}"
601 }
602 } else if (!options.nomissingeli && !getType(e).equals("tms")) {
603 myprintln "+ Missing ELI projections ('${jt}'): ${getDescription(j)}"
604 }
605 }
606
607 et = getDefault(e)
608 jt = getDefault(j)
609 if (!et.equals(jt)) {
610 if (!jt) {
611 myprintln "- Missing JOSM default: ${getDescription(j)}"
612 } else if (!options.nomissingeli) {
613 myprintln "+ Missing ELI default: ${getDescription(j)}"
614 }
615 }
616 et = getOverlay(e)
617 jt = getOverlay(j)
618 if (!et.equals(jt)) {
619 if (!jt) {
620 myprintln "- Missing JOSM overlay flag: ${getDescription(j)}"
621 } else if (!options.nomissingeli) {
622 myprintln "+ Missing ELI overlay flag: ${getDescription(j)}"
623 }
624 }
625 }
626 myprintln "*** Mismatching shapes: ***"
627 for (def url : josmUrls.keySet()) {
628 def j = josmUrls.get(url)
629 def num = 1
630 for (def shape : getShapes(j)) {
631 def p = shape.getPoints()
632 if(!p[0].equals(p[p.size()-1])) {
633 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
634 }
635 for (def nump = 1; nump < p.size(); ++nump) {
636 if (p[nump-1] == p[nump]) {
637 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
638 }
639 }
640 ++num
641 }
642 }
643 for (def url : eliUrls.keySet()) {
644 def e = eliUrls.get(url)
645 def num = 1
646 def s = getShapes(e)
647 for (def shape : s) {
648 def p = shape.getPoints()
649 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
650 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
651 }
652 for (def nump = 1; nump < p.size(); ++nump) {
653 if (p[nump-1] == p[nump]) {
654 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
655 }
656 }
657 ++num
658 }
659 if (!josmUrls.containsKey(url)) {
660 continue
661 }
662 def j = josmUrls.get(url)
663 def js = getShapes(j)
664 if(!s.size() && js.size()) {
665 if(!options.nomissingeli) {
666 myprintln "+ No ELI shape: ${getDescription(j)}"
667 }
668 } else if(!js.size() && s.size()) {
669 // don't report boundary like 5 point shapes as difference
670 if (s.size() != 1 || s[0].getPoints().size() != 5) {
671 myprintln "- No JOSM shape: ${getDescription(j)}"
672 }
673 } else if(s.size() != js.size()) {
674 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
675 } else {
676 for(def nums = 0; nums < s.size(); ++nums) {
677 def ep = s[nums].getPoints()
678 def jp = js[nums].getPoints()
679 if(ep.size() != jp.size()) {
680 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
681 } else {
682 for(def nump = 0; nump < ep.size(); ++nump) {
683 def ept = ep[nump]
684 def jpt = jp[nump]
685 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
686 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
687 nump = ep.size()
688 num = s.size()
689 }
690 }
691 }
692 }
693 }
694 }
695 myprintln "*** Mismatching icons: ***"
696 for (def url : eliUrls.keySet()) {
697 def e = eliUrls.get(url)
698 if (!josmUrls.containsKey(url)) {
699 continue
700 }
701 def j = josmUrls.get(url)
702 def ij = getIcon(j)
703 def ie = getIcon(e)
704 if(ij != null && ie == null) {
705 if(!options.nomissingeli) {
706 myprintln "+ No ELI icon: ${getDescription(j)}"
707 }
708 } else if(ij == null && ie != null) {
709 myprintln "- No JOSM icon: ${getDescription(j)}"
710 } else if(!ij.equals(ie)) {
711 myprintln "* Different icons: ${getDescription(j)}"
712 }
713 }
714 myprintln "*** Miscellaneous checks: ***"
715 def josmIds = new HashMap<String, ImageryInfo>()
716 def all = Projections.getAllProjectionCodes()
717 for (def url : josmUrls.keySet()) {
718 def j = josmUrls.get(url)
719 def id = getId(j)
720 if("wms".equals(getType(j))) {
721 if(!getProjections(j)) {
722 myprintln "* WMS without projections: ${getDescription(j)}"
723 } else {
724 def unsupported = new LinkedList<String>()
725 def old = new LinkedList<String>()
726 for (def p : getProjectionsUnstripped(j)) {
727 if("CRS:84".equals(p)) {
728 if(!(url =~ /(?i)version=1\.3/)) {
729 myprintln "* CRS:84 without WMS 1.3: ${getDescription(j)}"
730 }
731 } else if(oldproj.containsKey(p)) {
732 old.add(p)
733 } else if(!all.contains(p) && !ignoreproj.contains(p)) {
734 unsupported.add(p)
735 }
736 }
737 if (unsupported) {
738 def s = String.join(", ", unsupported)
739 myprintln "* Projections ${s} not supported by JOSM: ${getDescription(j)}"
740 }
741 for (def o : old) {
742 myprintln "* Projection ${o} is an old unsupported code and has been replaced by ${oldproj.get(o)}: ${getDescription(j)}"
743 }
744 }
745 if((url =~ /(?i)version=1\.3/) && !(url =~ /[Cc][Rr][Ss]=\{proj\}/)) {
746 myprintln "* WMS 1.3 with strange CRS specification: ${getDescription(j)}"
747 }
748 if((url =~ /(?i)version=1\.1/) && !(url =~ /[Ss][Rr][Ss]=\{proj\}/)) {
749 myprintln "* WMS 1.1 with strange SRS specification: ${getDescription(j)}"
750 }
751 }
752 if(!(url =~ /^https?:\/\//) && !"scanex".equals(getType(j))) {
753 myprintln "* Strange URL: ${getDescription(j)}"
754 }
755 if(josmMirrors.containsKey(url)) {
756 continue
757 }
758 if(id == null) {
759 myprintln "* No JOSM-ID: ${getDescription(j)}"
760 } else if(josmIds.containsKey(id)) {
761 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
762 } else {
763 josmIds.put(id, j)
764 }
765 def d = getDate(j)
766 if(!d.isEmpty()) {
767 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
768 if(reg == null || reg.count != 1) {
769 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
770 } else {
771 try {
772 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
773 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
774 if(second.compareTo(first) < 0) {
775 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
776 }
777 }
778 catch (Exception e) {
779 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
780 }
781 }
782 }
783 if(getAttributionUrl(j) && !getAttributionText(j)) {
784 myprintln "* Attribution link without text: ${getDescription(j)}"
785 }
786 if(getLogoUrl(j) && !getLogoImage(j)) {
787 myprintln "* Logo link without image: ${getDescription(j)}"
788 }
789 if(getTermsOfUseText(j) && !getTermsOfUseUrl(j)) {
790 myprintln "* Terms of Use text without link: ${getDescription(j)}"
791 }
792 def js = getShapes(j)
793 if(js.size()) {
794 def minlat = 1000
795 def minlon = 1000
796 def maxlat = -1000
797 def maxlon = -1000
798 for(def s: js) {
799 for(def p: s.getPoints()) {
800 def lat = p.getLat()
801 def lon = p.getLon()
802 if(lat > maxlat) maxlat = lat
803 if(lon > maxlon) maxlon = lon
804 if(lat < minlat) minlat = lat
805 if(lon < minlon) minlon = lon
806 }
807 }
808 def b = j.getBounds()
809 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
810 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)}"
811 }
812 }
813 }
814 }
815
816 /**
817 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
818 */
819 static String getUrl(Object e) {
820 if (e instanceof ImageryInfo) return e.url
821 return e.get("properties").getString("url")
822 }
823 static String getUrlStripped(Object e) {
824 return getUrl(e).replaceAll("\\?(apikey|access_token)=.*","")
825 }
826 static String getDate(Object e) {
827 if (e instanceof ImageryInfo) return e.date ? e.date : ""
828 def p = e.get("properties")
829 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
830 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
831 if(!start.isEmpty() && !end.isEmpty())
832 return start+";"+end
833 else if(!start.isEmpty())
834 return start+";-"
835 else if(!end.isEmpty())
836 return "-;"+end
837 return ""
838 }
839 static Date verifyDate(String year, String month, String day) {
840 def date
841 if(year == null) {
842 date = "3000-01-01"
843 } else {
844 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
845 }
846 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
847 df.setLenient(false)
848 return df.parse(date)
849 }
850 static String getId(Object e) {
851 if (e instanceof ImageryInfo) return e.getId()
852 return e.get("properties").getString("id")
853 }
854 static String getName(Object e) {
855 if (e instanceof ImageryInfo) return e.getOriginalName()
856 return e.get("properties").getString("name")
857 }
858 static List<Object> getMirrors(Object e) {
859 if (e instanceof ImageryInfo) return e.getMirrors()
860 return []
861 }
862 static List<Object> getProjections(Object e) {
863 def r = []
864 def u = getProjectionsUnstripped(e)
865 if(u) {
866 for (def p : u) {
867 if(!oldproj.containsKey(p) && !("CRS:84".equals(p) && !(getUrlStripped(e) =~ /(?i)version=1\.3/))) {
868 r += p
869 }
870 }
871 }
872 return r
873 }
874 static List<Object> getProjectionsUnstripped(Object e) {
875 def r
876 if (e instanceof ImageryInfo) {
877 r = e.getServerProjections()
878 } else {
879 def s = e.get("properties").get("available_projections")
880 if (s) {
881 r = []
882 for (def p : s) {
883 r += p.getString()
884 }
885 }
886 }
887 return r ? r : []
888 }
889 static List<Shape> getShapes(Object e) {
890 if (e instanceof ImageryInfo) {
891 def bounds = e.getBounds()
892 if(bounds != null) {
893 return bounds.getShapes()
894 }
895 return []
896 }
897 if(!e.isNull("geometry")) {
898 def ex = e.get("geometry")
899 if(ex != null && !ex.isNull("coordinates")) {
900 def poly = ex.get("coordinates")
901 List<Shape> l = []
902 for(def shapes: poly) {
903 def s = new Shape()
904 for(def point: shapes) {
905 def lon = point[0].toString()
906 def lat = point[1].toString()
907 s.addPoint(lat, lon)
908 }
909 l.add(s)
910 }
911 return l
912 }
913 }
914 return []
915 }
916 static String getType(Object e) {
917 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
918 return e.get("properties").getString("type")
919 }
920 static Integer getMinZoom(Object e) {
921 if (e instanceof ImageryInfo) {
922 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
923 return null;
924 int mz = e.getMinZoom()
925 return mz == 0 ? null : mz
926 } else {
927 def num = e.get("properties").getJsonNumber("min_zoom")
928 if (num == null) return null
929 return num.intValue()
930 }
931 }
932 static Integer getMaxZoom(Object e) {
933 if (e instanceof ImageryInfo) {
934 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
935 return null;
936 int mz = e.getMaxZoom()
937 return mz == 0 ? null : mz
938 } else {
939 def num = e.get("properties").getJsonNumber("max_zoom")
940 if (num == null) return null
941 return num.intValue()
942 }
943 }
944 static String getCountryCode(Object e) {
945 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
946 return e.get("properties").getString("country_code", null)
947 }
948 static String getQuality(Object e) {
949 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
950 return (e.get("properties").containsKey("best")
951 && e.get("properties").getBoolean("best")) ? "eli-best" : null
952 }
953 static Boolean getOverlay(Object e) {
954 if (e instanceof ImageryInfo) return e.isOverlay()
955 return (e.get("properties").containsKey("overlay")
956 && e.get("properties").getBoolean("overlay"))
957 }
958 static String getIcon(Object e) {
959 if (e instanceof ImageryInfo) return e.getIcon()
960 return e.get("properties").getString("icon", null)
961 }
962 static String getAttributionText(Object e) {
963 if (e instanceof ImageryInfo) return e.getAttributionText(0, null, null)
964 try {return e.get("properties").get("attribution").getString("text", null)} catch (NullPointerException ex) {return null}
965 }
966 static String getAttributionUrl(Object e) {
967 if (e instanceof ImageryInfo) return e.getAttributionLinkURL()
968 try {return e.get("properties").get("attribution").getString("url", null)} catch (NullPointerException ex) {return null}
969 }
970 static String getTermsOfUseText(Object e) {
971 if (e instanceof ImageryInfo) return e.getTermsOfUseText()
972 return null
973 }
974 static String getTermsOfUseUrl(Object e) {
975 if (e instanceof ImageryInfo) return e.getTermsOfUseURL()
976 return null
977 }
978 static String getLogoImage(Object e) {
979 if (e instanceof ImageryInfo) return e.getAttributionImageRaw()
980 return null
981 }
982 static String getLogoUrl(Object e) {
983 if (e instanceof ImageryInfo) return e.getAttributionImageURL()
984 return null
985 }
986 static String getPermissionReferenceUrl(Object e) {
987 if (e instanceof ImageryInfo) return e.getPermissionReferenceURL()
988 return e.get("properties").getString("license_url", null)
989 }
990 static Map<String,String> getDescriptions(Object e) {
991 Map<String,String> res = new HashMap<String, String>()
992 if (e instanceof ImageryInfo) {
993 String a = e.getDescription()
994 if (a) res.put("en", a)
995 } else {
996 String a = e.get("properties").getString("description", null)
997 if (a) res.put("en", a)
998 }
999 return res
1000 }
1001 static Boolean getValidGeoreference(Object e) {
1002 if (e instanceof ImageryInfo) return e.isGeoreferenceValid()
1003 return false
1004 }
1005 static Boolean getDefault(Object e) {
1006 if (e instanceof ImageryInfo) return e.isDefaultEntry()
1007 return e.get("properties").getBoolean("default", false)
1008 }
1009 String getDescription(Object o) {
1010 def url = getUrl(o)
1011 def cc = getCountryCode(o)
1012 if (cc == null) {
1013 def j = josmUrls.get(url)
1014 if (j != null) cc = getCountryCode(j)
1015 if (cc == null) {
1016 def e = eliUrls.get(url)
1017 if (e != null) cc = getCountryCode(e)
1018 }
1019 }
1020 if (cc == null) {
1021 cc = ''
1022 } else {
1023 cc = "[$cc] "
1024 }
1025 def d = cc + getName(o) + " - " + getUrl(o)
1026 if (options.shorten) {
1027 def MAXLEN = 140
1028 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
1029 }
1030 return d
1031 }
1032}
Note: See TracBrowser for help on using the repository browser.