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

Last change on this file since 12066 was 12066, checked in by stoecker, 7 years ago

see #14655 - enforce UTF-8 for output in files

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