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

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

see #14655 - add (commented) ID check

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