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

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

don't report zoom differences when the entry is a wms mirror

  • Property svn:eol-style set to native
File size: 34.5 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 if (et.size() > 0) et = et["en"]
465 if (jt.size() > 0) jt = jt["en"]
466 if (!et.equals(jt)) {
467 if (!jt) {
468 myprintln "+ SKIP - Missing JOSM description (${et}): ${getDescription(j)}"
469 } else if (et) {
470 myprintln "+ SKIP * Description differs (${et} != '${jt}'): ${getDescription(j)}"
471 } else if (!options.nomissingeli) {
472 myprintln "+ Missing ELI description ('${jt}'): ${getDescription(j)}"
473 }
474 }
475
476 et = getPermissionReferenceUrl(e)
477 jt = getPermissionReferenceUrl(j)
478 if (!jt) jt = getTermsOfUseUrl(j)
479 if (!et.equals(jt)) {
480 if (!jt) {
481 myprintln "+ SKIP - Missing JOSM license URL (${et}): ${getDescription(j)}"
482 } else if (et) {
483 myprintln "+ SKIP * License URL differs (${et} != '${jt}'): ${getDescription(j)}"
484 } else if (!options.nomissingeli) {
485 myprintln "+ Missing ELI license URL ('${jt}'): ${getDescription(j)}"
486 }
487 }
488
489 et = getAttributionUrl(e)
490 jt = getAttributionUrl(j)
491 if (!et.equals(jt)) {
492 if (!jt) {
493 myprintln "+ SKIP - Missing JOSM attribution URL (${et}): ${getDescription(j)}"
494 } else if (et) {
495 myprintln "+ SKIP * Attribution URL differs (${et} != '${jt}'): ${getDescription(j)}"
496 } else if (!options.nomissingeli) {
497 myprintln "+ Missing ELI attribution URL ('${jt}'): ${getDescription(j)}"
498 }
499 }
500
501 et = getAttributionText(e)
502 jt = getAttributionText(j)
503 if (!et.equals(jt)) {
504 if (!jt) {
505 myprintln "+ SKIP - Missing JOSM attribution text (${et}): ${getDescription(j)}"
506 } else if (et) {
507 myprintln "+ SKIP * Attribution text differs (${et} != '${jt}'): ${getDescription(j)}"
508 } else if (!options.nomissingeli) {
509 myprintln "+ Missing ELI attribution text ('${jt}'): ${getDescription(j)}"
510 }
511 }
512
513 et = getProjections(e)
514 jt = getProjections(j)
515 if (et) { et = new LinkedList(et); Collections.sort(et); et = String.join(" ", et) }
516 if (jt) { jt = new LinkedList(jt); Collections.sort(jt); jt = String.join(" ", jt) }
517 if (!et.equals(jt)) {
518 if (!jt) {
519 myprintln "+ SKIP - Missing JOSM projections (${et}): ${getDescription(j)}"
520 } else if (et) {
521 myprintln "+ SKIP * Projections differ (${et} != '${jt}'): ${getDescription(j)}"
522 } else if (!options.nomissingeli) {
523 myprintln "+ Missing ELI projections ('${jt}'): ${getDescription(j)}"
524 }
525 }
526 }
527 myprintln "*** Mismatching shapes: ***"
528 for (def url : josmUrls.keySet()) {
529 def j = josmUrls.get(url)
530 def num = 1
531 for (def shape : getShapes(j)) {
532 def p = shape.getPoints()
533 if(!p[0].equals(p[p.size()-1])) {
534 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
535 }
536 for (def nump = 1; nump < p.size(); ++nump) {
537 if (p[nump-1] == p[nump]) {
538 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
539 }
540 }
541 ++num
542 }
543 }
544 for (def url : eliUrls.keySet()) {
545 def e = eliUrls.get(url)
546 def num = 1
547 def s = getShapes(e)
548 for (def shape : s) {
549 def p = shape.getPoints()
550 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
551 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
552 }
553 for (def nump = 1; nump < p.size(); ++nump) {
554 if (p[nump-1] == p[nump]) {
555 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
556 }
557 }
558 ++num
559 }
560 if (!josmUrls.containsKey(url)) {
561 continue
562 }
563 def j = josmUrls.get(url)
564 def js = getShapes(j)
565 if(!s.size() && js.size()) {
566 if(!options.nomissingeli) {
567 myprintln "+ No ELI shape: ${getDescription(j)}"
568 }
569 } else if(!js.size() && s.size()) {
570 // don't report boundary like 5 point shapes as difference
571 if (s.size() != 1 || s[0].getPoints().size() != 5) {
572 myprintln "- No JOSM shape: ${getDescription(j)}"
573 }
574 } else if(s.size() != js.size()) {
575 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
576 } else {
577 for(def nums = 0; nums < s.size(); ++nums) {
578 def ep = s[nums].getPoints()
579 def jp = js[nums].getPoints()
580 if(ep.size() != jp.size()) {
581 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
582 } else {
583 for(def nump = 0; nump < ep.size(); ++nump) {
584 def ept = ep[nump]
585 def jpt = jp[nump]
586 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
587 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
588 nump = ep.size()
589 num = s.size()
590 }
591 }
592 }
593 }
594 }
595 }
596 myprintln "*** Mismatching icons: ***"
597 for (def url : eliUrls.keySet()) {
598 def e = eliUrls.get(url)
599 if (!josmUrls.containsKey(url)) {
600 continue
601 }
602 def j = josmUrls.get(url)
603 def ij = getIcon(j)
604 def ie = getIcon(e)
605 if(ij != null && ie == null) {
606 if(!options.nomissingeli) {
607 myprintln "+ No ELI icon: ${getDescription(j)}"
608 }
609 } else if(ij == null && ie != null) {
610 myprintln "- No JOSM icon: ${getDescription(j)}"
611 } else if(!ij.equals(ie)) {
612 myprintln "* Different icons: ${getDescription(j)}"
613 }
614 }
615 myprintln "*** Miscellaneous checks: ***"
616 def josmIds = new HashMap<String, ImageryInfo>()
617 for (def url : josmUrls.keySet()) {
618 def j = josmUrls.get(url)
619 def id = getId(j)
620 if(josmMirrors.containsKey(url)) {
621 continue
622 }
623 if(id == null) {
624 myprintln "* No JOSM-ID: ${getDescription(j)}"
625 } else if(josmIds.containsKey(id)) {
626 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
627 } else {
628 josmIds.put(id, j)
629 }
630 def d = getDate(j)
631 if(!d.isEmpty()) {
632 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
633 if(reg == null || reg.count != 1) {
634 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
635 } else {
636 try {
637 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
638 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
639 if(second.compareTo(first) < 0) {
640 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
641 }
642 }
643 catch (Exception e) {
644 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
645 }
646 }
647 }
648 def js = getShapes(j)
649 if(js.size()) {
650 def minlat = 1000
651 def minlon = 1000
652 def maxlat = -1000
653 def maxlon = -1000
654 for(def s: js) {
655 for(def p: s.getPoints()) {
656 def lat = p.getLat()
657 def lon = p.getLon()
658 if(lat > maxlat) maxlat = lat
659 if(lon > maxlon) maxlon = lon
660 if(lat < minlat) minlat = lat
661 if(lon < minlon) minlon = lon
662 }
663 }
664 def b = j.getBounds()
665 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
666 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)}"
667 }
668 }
669 }
670 }
671
672 /**
673 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
674 */
675 static String getUrl(Object e) {
676 if (e instanceof ImageryInfo) return e.url
677 return e.get("properties").getString("url")
678 }
679 static String getDate(Object e) {
680 if (e instanceof ImageryInfo) return e.date ? e.date : ""
681 def p = e.get("properties")
682 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
683 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
684 if(!start.isEmpty() && !end.isEmpty())
685 return start+";"+end
686 else if(!start.isEmpty())
687 return start+";-"
688 else if(!end.isEmpty())
689 return "-;"+end
690 return ""
691 }
692 static Date verifyDate(String year, String month, String day) {
693 def date
694 if(year == null) {
695 date = "3000-01-01"
696 } else {
697 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
698 }
699 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
700 df.setLenient(false)
701 return df.parse(date)
702 }
703 static String getId(Object e) {
704 if (e instanceof ImageryInfo) return e.getId()
705 return e.get("properties").getString("id")
706 }
707 static String getName(Object e) {
708 if (e instanceof ImageryInfo) return e.getOriginalName()
709 return e.get("properties").getString("name")
710 }
711 static List<Object> getMirrors(Object e) {
712 if (e instanceof ImageryInfo) return e.getMirrors()
713 return []
714 }
715 static List<Object> getProjections(Object e) {
716 def r
717 if (e instanceof ImageryInfo) {
718 r = e.getServerProjections()
719 } else {
720 def s = e.get("properties").get("available_projections")
721 if (s) {
722 r = []
723 for (def p : s)
724 r += p.getString()
725 }
726 }
727 return r ? r : []
728 }
729 static List<Shape> getShapes(Object e) {
730 if (e instanceof ImageryInfo) {
731 def bounds = e.getBounds()
732 if(bounds != null) {
733 return bounds.getShapes()
734 }
735 return []
736 }
737 if(!e.isNull("geometry")) {
738 def ex = e.get("geometry")
739 if(ex != null && !ex.isNull("coordinates")) {
740 def poly = ex.get("coordinates")
741 List<Shape> l = []
742 for(def shapes: poly) {
743 def s = new Shape()
744 for(def point: shapes) {
745 def lon = point[0].toString()
746 def lat = point[1].toString()
747 s.addPoint(lat, lon)
748 }
749 l.add(s)
750 }
751 return l
752 }
753 }
754 return []
755 }
756 static String getType(Object e) {
757 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
758 return e.get("properties").getString("type")
759 }
760 static Integer getMinZoom(Object e) {
761 if (e instanceof ImageryInfo) {
762 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
763 return null;
764 int mz = e.getMinZoom()
765 return mz == 0 ? null : mz
766 } else {
767 def num = e.get("properties").getJsonNumber("min_zoom")
768 if (num == null) return null
769 return num.intValue()
770 }
771 }
772 static Integer getMaxZoom(Object e) {
773 if (e instanceof ImageryInfo) {
774 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
775 return null;
776 int mz = e.getMaxZoom()
777 return mz == 0 ? null : mz
778 } else {
779 def num = e.get("properties").getJsonNumber("max_zoom")
780 if (num == null) return null
781 return num.intValue()
782 }
783 }
784 static String getCountryCode(Object e) {
785 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
786 return e.get("properties").getString("country_code", null)
787 }
788 static String getQuality(Object e) {
789 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
790 return (e.get("properties").containsKey("best")
791 && e.get("properties").getBoolean("best")) ? "eli-best" : null
792 }
793 static String getIcon(Object e) {
794 if (e instanceof ImageryInfo) return e.getIcon()
795 return e.get("properties").getString("icon", null)
796 }
797 static String getAttributionText(Object e) {
798 if (e instanceof ImageryInfo) return e.getAttributionText(0, null, null)
799 try {return e.get("properties").get("attribution").getString("text", null)} catch (NullPointerException ex) {return null}
800 }
801 static String getAttributionUrl(Object e) {
802 if (e instanceof ImageryInfo) return e.getAttributionLinkURL()
803 try {return e.get("properties").get("attribution").getString("url", null)} catch (NullPointerException ex) {return null}
804 }
805 static String getTermsOfUseText(Object e) {
806 if (e instanceof ImageryInfo) return e.getTermsOfUseText()
807 return null
808 }
809 static String getTermsOfUseUrl(Object e) {
810 if (e instanceof ImageryInfo) return e.getTermsOfUseURL()
811 return null
812 }
813 static String getPermissionReferenceUrl(Object e) {
814 if (e instanceof ImageryInfo) return e.getPermissionReferenceURL()
815 return e.get("properties").getString("license_url", null)
816 }
817 static Map<String,String> getDescriptions(Object e) {
818 Map<String,String> res = new HashMap<String, String>()
819 if (e instanceof ImageryInfo) {
820 String a = e.getDescription()
821 if (a) res.put("en", a)
822 } else {
823 String a = e.get("properties").getString("description", null)
824 if (a) res.put("en", a)
825 }
826 return res
827 }
828 static Boolean getValidGeoreference(Object e) {
829 if (e instanceof ImageryInfo) return e.isGeoreferenceValid()
830 return false
831 }
832 String getDescription(Object o) {
833 def url = getUrl(o)
834 def cc = getCountryCode(o)
835 if (cc == null) {
836 def j = josmUrls.get(url)
837 if (j != null) cc = getCountryCode(j)
838 if (cc == null) {
839 def e = eliUrls.get(url)
840 if (e != null) cc = getCountryCode(e)
841 }
842 }
843 if (cc == null) {
844 cc = ''
845 } else {
846 cc = "[$cc] "
847 }
848 def d = cc + getName(o) + " - " + getUrl(o)
849 if (options.shorten) {
850 def MAXLEN = 140
851 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
852 }
853 return d
854 }
855}
Note: See TracBrowser for help on using the repository browser.