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

Last change on this file since 12019 was 12008, checked in by stoecker, 8 years ago

ignore differing imagico text - allows us to strip the ugly header

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