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

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

don't warn multiple times for unsupported projections

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