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

Last change on this file since 13537 was 13536, checked in by stoecker, 8 years ago

add possibility to change map ids (see #14655), add overlay flag for imagery

  • Property svn:eol-style set to native
File size: 42.0 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 myprintln "* Projections differ ('${et}' != '${jt}'): ${getDescription(j)}"
598 } else if (!options.nomissingeli && !getType(e).equals("tms")) {
599 myprintln "+ Missing ELI projections ('${jt}'): ${getDescription(j)}"
600 }
601 }
602
603 et = getDefault(e)
604 jt = getDefault(j)
605 if (!et.equals(jt)) {
606 if (!jt) {
607 myprintln "- Missing JOSM default: ${getDescription(j)}"
608 } else if (!options.nomissingeli) {
609 myprintln "+ Missing ELI default: ${getDescription(j)}"
610 }
611 }
612 et = getOverlay(e)
613 jt = getOverlay(j)
614 if (!et.equals(jt)) {
615 if (!jt) {
616 myprintln "! Missing JOSM overlay flag: ${getDescription(j)}"
617 } else if (!options.nomissingeli) {
618 myprintln "+ Missing ELI overlay flag: ${getDescription(j)}"
619 }
620 }
621 }
622 myprintln "*** Mismatching shapes: ***"
623 for (def url : josmUrls.keySet()) {
624 def j = josmUrls.get(url)
625 def num = 1
626 for (def shape : getShapes(j)) {
627 def p = shape.getPoints()
628 if(!p[0].equals(p[p.size()-1])) {
629 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
630 }
631 for (def nump = 1; nump < p.size(); ++nump) {
632 if (p[nump-1] == p[nump]) {
633 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
634 }
635 }
636 ++num
637 }
638 }
639 for (def url : eliUrls.keySet()) {
640 def e = eliUrls.get(url)
641 def num = 1
642 def s = getShapes(e)
643 for (def shape : s) {
644 def p = shape.getPoints()
645 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
646 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
647 }
648 for (def nump = 1; nump < p.size(); ++nump) {
649 if (p[nump-1] == p[nump]) {
650 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
651 }
652 }
653 ++num
654 }
655 if (!josmUrls.containsKey(url)) {
656 continue
657 }
658 def j = josmUrls.get(url)
659 def js = getShapes(j)
660 if(!s.size() && js.size()) {
661 if(!options.nomissingeli) {
662 myprintln "+ No ELI shape: ${getDescription(j)}"
663 }
664 } else if(!js.size() && s.size()) {
665 // don't report boundary like 5 point shapes as difference
666 if (s.size() != 1 || s[0].getPoints().size() != 5) {
667 myprintln "- No JOSM shape: ${getDescription(j)}"
668 }
669 } else if(s.size() != js.size()) {
670 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
671 } else {
672 for(def nums = 0; nums < s.size(); ++nums) {
673 def ep = s[nums].getPoints()
674 def jp = js[nums].getPoints()
675 if(ep.size() != jp.size()) {
676 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
677 } else {
678 for(def nump = 0; nump < ep.size(); ++nump) {
679 def ept = ep[nump]
680 def jpt = jp[nump]
681 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
682 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
683 nump = ep.size()
684 num = s.size()
685 }
686 }
687 }
688 }
689 }
690 }
691 myprintln "*** Mismatching icons: ***"
692 for (def url : eliUrls.keySet()) {
693 def e = eliUrls.get(url)
694 if (!josmUrls.containsKey(url)) {
695 continue
696 }
697 def j = josmUrls.get(url)
698 def ij = getIcon(j)
699 def ie = getIcon(e)
700 if(ij != null && ie == null) {
701 if(!options.nomissingeli) {
702 myprintln "+ No ELI icon: ${getDescription(j)}"
703 }
704 } else if(ij == null && ie != null) {
705 myprintln "- No JOSM icon: ${getDescription(j)}"
706 } else if(!ij.equals(ie)) {
707 myprintln "* Different icons: ${getDescription(j)}"
708 }
709 }
710 myprintln "*** Miscellaneous checks: ***"
711 def josmIds = new HashMap<String, ImageryInfo>()
712 def all = Projections.getAllProjectionCodes()
713 for (def url : josmUrls.keySet()) {
714 def j = josmUrls.get(url)
715 def id = getId(j)
716 if("wms".equals(getType(j))) {
717 if(!getProjections(j)) {
718 myprintln "* WMS without projections: ${getDescription(j)}"
719 } else {
720 def unsupported = new LinkedList<String>()
721 def old = new LinkedList<String>()
722 for (def p : getProjectionsUnstripped(j)) {
723 if("CRS:84".equals(p)) {
724 if(!(url =~ /(?i)version=1\.3/)) {
725 myprintln "* CRS:84 without WMS 1.3: ${getDescription(j)}"
726 }
727 } else if(oldproj.containsKey(p)) {
728 old.add(p)
729 } else if(!all.contains(p) && !ignoreproj.contains(p)) {
730 unsupported.add(p)
731 }
732 }
733 if (unsupported) {
734 def s = String.join(", ", unsupported)
735 myprintln "* Projections ${s} not supported by JOSM: ${getDescription(j)}"
736 }
737 for (def o : old) {
738 myprintln "* Projection ${o} is an old unsupported code and has been replaced by ${oldproj.get(o)}: ${getDescription(j)}"
739 }
740 }
741 if((url =~ /(?i)version=1\.3/) && !(url =~ /[Cc][Rr][Ss]=\{proj\}/)) {
742 myprintln "* WMS 1.3 with strange CRS specification: ${getDescription(j)}"
743 }
744 if((url =~ /(?i)version=1\.1/) && !(url =~ /[Ss][Rr][Ss]=\{proj\}/)) {
745 myprintln "* WMS 1.1 with strange SRS specification: ${getDescription(j)}"
746 }
747 }
748 if(!(url =~ /^https?:\/\//) && !"scanex".equals(getType(j))) {
749 myprintln "* Strange URL: ${getDescription(j)}"
750 }
751 if(josmMirrors.containsKey(url)) {
752 continue
753 }
754 if(id == null) {
755 myprintln "* No JOSM-ID: ${getDescription(j)}"
756 } else if(josmIds.containsKey(id)) {
757 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
758 } else {
759 josmIds.put(id, j)
760 }
761 def d = getDate(j)
762 if(!d.isEmpty()) {
763 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
764 if(reg == null || reg.count != 1) {
765 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
766 } else {
767 try {
768 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
769 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
770 if(second.compareTo(first) < 0) {
771 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
772 }
773 }
774 catch (Exception e) {
775 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
776 }
777 }
778 }
779 if(getAttributionUrl(j) && !getAttributionText(j)) {
780 myprintln "* Attribution link without text: ${getDescription(j)}"
781 }
782 if(getLogoUrl(j) && !getLogoImage(j)) {
783 myprintln "* Logo link without image: ${getDescription(j)}"
784 }
785 if(getTermsOfUseText(j) && !getTermsOfUseUrl(j)) {
786 myprintln "* Terms of Use text without link: ${getDescription(j)}"
787 }
788 def js = getShapes(j)
789 if(js.size()) {
790 def minlat = 1000
791 def minlon = 1000
792 def maxlat = -1000
793 def maxlon = -1000
794 for(def s: js) {
795 for(def p: s.getPoints()) {
796 def lat = p.getLat()
797 def lon = p.getLon()
798 if(lat > maxlat) maxlat = lat
799 if(lon > maxlon) maxlon = lon
800 if(lat < minlat) minlat = lat
801 if(lon < minlon) minlon = lon
802 }
803 }
804 def b = j.getBounds()
805 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
806 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)}"
807 }
808 }
809 }
810 }
811
812 /**
813 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
814 */
815 static String getUrl(Object e) {
816 if (e instanceof ImageryInfo) return e.url
817 return e.get("properties").getString("url")
818 }
819 static String getUrlStripped(Object e) {
820 return getUrl(e).replaceAll("\\?(apikey|access_token)=.*","")
821 }
822 static String getDate(Object e) {
823 if (e instanceof ImageryInfo) return e.date ? e.date : ""
824 def p = e.get("properties")
825 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
826 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
827 if(!start.isEmpty() && !end.isEmpty())
828 return start+";"+end
829 else if(!start.isEmpty())
830 return start+";-"
831 else if(!end.isEmpty())
832 return "-;"+end
833 return ""
834 }
835 static Date verifyDate(String year, String month, String day) {
836 def date
837 if(year == null) {
838 date = "3000-01-01"
839 } else {
840 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
841 }
842 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
843 df.setLenient(false)
844 return df.parse(date)
845 }
846 static String getId(Object e) {
847 if (e instanceof ImageryInfo) return e.getId()
848 return e.get("properties").getString("id")
849 }
850 static String getName(Object e) {
851 if (e instanceof ImageryInfo) return e.getOriginalName()
852 return e.get("properties").getString("name")
853 }
854 static List<Object> getMirrors(Object e) {
855 if (e instanceof ImageryInfo) return e.getMirrors()
856 return []
857 }
858 static List<Object> getProjections(Object e) {
859 def r = []
860 def u = getProjectionsUnstripped(e)
861 if(u) {
862 for (def p : u) {
863 if(!oldproj.containsKey(p) && !("CRS:84".equals(p) && !(getUrlStripped(e) =~ /(?i)version=1\.3/))) {
864 r += p
865 }
866 }
867 }
868 return r
869 }
870 static List<Object> getProjectionsUnstripped(Object e) {
871 def r
872 if (e instanceof ImageryInfo) {
873 r = e.getServerProjections()
874 } else {
875 def s = e.get("properties").get("available_projections")
876 if (s) {
877 r = []
878 for (def p : s) {
879 r += p.getString()
880 }
881 }
882 }
883 return r ? r : []
884 }
885 static List<Shape> getShapes(Object e) {
886 if (e instanceof ImageryInfo) {
887 def bounds = e.getBounds()
888 if(bounds != null) {
889 return bounds.getShapes()
890 }
891 return []
892 }
893 if(!e.isNull("geometry")) {
894 def ex = e.get("geometry")
895 if(ex != null && !ex.isNull("coordinates")) {
896 def poly = ex.get("coordinates")
897 List<Shape> l = []
898 for(def shapes: poly) {
899 def s = new Shape()
900 for(def point: shapes) {
901 def lon = point[0].toString()
902 def lat = point[1].toString()
903 s.addPoint(lat, lon)
904 }
905 l.add(s)
906 }
907 return l
908 }
909 }
910 return []
911 }
912 static String getType(Object e) {
913 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
914 return e.get("properties").getString("type")
915 }
916 static Integer getMinZoom(Object e) {
917 if (e instanceof ImageryInfo) {
918 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
919 return null;
920 int mz = e.getMinZoom()
921 return mz == 0 ? null : mz
922 } else {
923 def num = e.get("properties").getJsonNumber("min_zoom")
924 if (num == null) return null
925 return num.intValue()
926 }
927 }
928 static Integer getMaxZoom(Object e) {
929 if (e instanceof ImageryInfo) {
930 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
931 return null;
932 int mz = e.getMaxZoom()
933 return mz == 0 ? null : mz
934 } else {
935 def num = e.get("properties").getJsonNumber("max_zoom")
936 if (num == null) return null
937 return num.intValue()
938 }
939 }
940 static String getCountryCode(Object e) {
941 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
942 return e.get("properties").getString("country_code", null)
943 }
944 static String getQuality(Object e) {
945 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
946 return (e.get("properties").containsKey("best")
947 && e.get("properties").getBoolean("best")) ? "eli-best" : null
948 }
949 static Boolean getOverlay(Object e) {
950 if (e instanceof ImageryInfo) return e.isOverlay()
951 return (e.get("properties").containsKey("overlay")
952 && e.get("properties").getBoolean("overlay"))
953 }
954 static String getIcon(Object e) {
955 if (e instanceof ImageryInfo) return e.getIcon()
956 return e.get("properties").getString("icon", null)
957 }
958 static String getAttributionText(Object e) {
959 if (e instanceof ImageryInfo) return e.getAttributionText(0, null, null)
960 try {return e.get("properties").get("attribution").getString("text", null)} catch (NullPointerException ex) {return null}
961 }
962 static String getAttributionUrl(Object e) {
963 if (e instanceof ImageryInfo) return e.getAttributionLinkURL()
964 try {return e.get("properties").get("attribution").getString("url", null)} catch (NullPointerException ex) {return null}
965 }
966 static String getTermsOfUseText(Object e) {
967 if (e instanceof ImageryInfo) return e.getTermsOfUseText()
968 return null
969 }
970 static String getTermsOfUseUrl(Object e) {
971 if (e instanceof ImageryInfo) return e.getTermsOfUseURL()
972 return null
973 }
974 static String getLogoImage(Object e) {
975 if (e instanceof ImageryInfo) return e.getAttributionImageRaw()
976 return null
977 }
978 static String getLogoUrl(Object e) {
979 if (e instanceof ImageryInfo) return e.getAttributionImageURL()
980 return null
981 }
982 static String getPermissionReferenceUrl(Object e) {
983 if (e instanceof ImageryInfo) return e.getPermissionReferenceURL()
984 return e.get("properties").getString("license_url", null)
985 }
986 static Map<String,String> getDescriptions(Object e) {
987 Map<String,String> res = new HashMap<String, String>()
988 if (e instanceof ImageryInfo) {
989 String a = e.getDescription()
990 if (a) res.put("en", a)
991 } else {
992 String a = e.get("properties").getString("description", null)
993 if (a) res.put("en", a)
994 }
995 return res
996 }
997 static Boolean getValidGeoreference(Object e) {
998 if (e instanceof ImageryInfo) return e.isGeoreferenceValid()
999 return false
1000 }
1001 static Boolean getDefault(Object e) {
1002 if (e instanceof ImageryInfo) return e.isDefaultEntry()
1003 return e.get("properties").getBoolean("default", false)
1004 }
1005 String getDescription(Object o) {
1006 def url = getUrl(o)
1007 def cc = getCountryCode(o)
1008 if (cc == null) {
1009 def j = josmUrls.get(url)
1010 if (j != null) cc = getCountryCode(j)
1011 if (cc == null) {
1012 def e = eliUrls.get(url)
1013 if (e != null) cc = getCountryCode(e)
1014 }
1015 }
1016 if (cc == null) {
1017 cc = ''
1018 } else {
1019 cc = "[$cc] "
1020 }
1021 def d = cc + getName(o) + " - " + getUrl(o)
1022 if (options.shorten) {
1023 def MAXLEN = 140
1024 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
1025 }
1026 return d
1027 }
1028}
Note: See TracBrowser for help on using the repository browser.