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

Last change on this file since 12259 was 12247, checked in by stoecker, 8 years ago

try another color

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