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

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

more XML output in Sync script

  • Property svn:eol-style set to native
File size: 27.4 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
200 String maininfo(def entry, String offset) {
201 String res = offset + "<type>${getType(entry)}</type>\n"
202 res += offset + "<url><![CDATA[${getUrl(entry)}]]></url>\n"
203 if(getType(entry) == "tms") {
204 if(getMinZoom(entry) != null)
205 res += offset + "<min-zoom>${getMinZoom(entry)}</min-zoom>\n"
206 if(getMaxZoom(entry) != null)
207 res += offset + "<max-zoom>${getMaxZoom(entry)}</max-zoom>\n"
208 }
209 return res
210 }
211
212
213 void printentries(def entries, def stream) {
214 DecimalFormat df = new DecimalFormat("#.#######")
215 df.setRoundingMode(java.math.RoundingMode.CEILING)
216 stream.write "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
217 stream.write "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n"
218 for (def e : entries) {
219 def best = "eli-best".equals(getQuality(e))
220 stream.write " <entry"+(best ? " eli-best=\"true\"" : "" )+">\n"
221 stream.write " <name>${getName(e)}</name>\n"
222 stream.write " <id>${getId(e)}</id>\n"
223 if(getDate(e) != "")
224 stream.write " <date>${getDate(e)}</date>\n"
225 stream.write maininfo(e, " ")
226 for (def m : getMirrors(e)) {
227 stream.write " <mirror>\n"+maininfo(m, " ")+" </mirror>\n"
228 }
229 def minlat = 1000
230 def minlon = 1000
231 def maxlat = -1000
232 def maxlon = -1000
233 def shapes = ""
234 def sep = "\n "
235 for(def s: getShapes(e)) {
236 shapes += " <shape>"
237 def i = 0
238 for(def p: s.getPoints()) {
239 def lat = p.getLat()
240 def lon = p.getLon()
241 if(lat > maxlat) maxlat = lat
242 if(lon > maxlon) maxlon = lon
243 if(lat < minlat) minlat = lat
244 if(lon < minlon) minlon = lon
245 if(!(i++%3)) {
246 shapes += sep + " "
247 }
248 shapes += "<point lat='${df.format(lat)}' lon='${df.format(lon)}'/>"
249 }
250 shapes += sep + "</shape>\n"
251 }
252 if(shapes) {
253 stream.write " <bounds min-lat='${df.format(minlat)}' min-lon='${df.format(minlon)}' max-lat='${df.format(maxlat)}' max-lon='${df.format(maxlon)}'>\n"
254 stream.write shapes + " </bounds>\n"
255 }
256 stream.write " </entry>\n"
257 }
258 stream.write "</imagery>\n"
259 stream.close()
260 }
261
262 void loadJosmEntries() {
263 def reader = new ImageryReader(josmInputFile)
264 josmEntries = reader.parse()
265
266 for (def e : josmEntries) {
267 def url = getUrl(e)
268 if (url.contains("{z}")) {
269 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
270 url = url.replace("{z}","{zoom}")
271 }
272 if (josmUrls.containsKey(url)) {
273 myprintln "+++ JOSM-URL is not unique: "+url
274 } else {
275 josmUrls.put(url, e)
276 }
277 for (def m : e.getMirrors()) {
278 url = getUrl(m)
279 m.origName = m.getOriginalName().replaceAll(" mirror server( \\d+)?","")
280 if (josmUrls.containsKey(url)) {
281 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
282 } else {
283 josmUrls.put(url, m)
284 josmMirrors.put(url, m)
285 }
286 }
287 }
288 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
289 }
290
291 List inOneButNotTheOther(Map m1, Map m2) {
292 def l = []
293 for (def url : m1.keySet()) {
294 if (!m2.containsKey(url)) {
295 def name = getName(m1.get(url))
296 l += " "+getDescription(m1.get(url))
297 }
298 }
299 l.sort()
300 }
301
302 void checkInOneButNotTheOther() {
303 def l1 = inOneButNotTheOther(eliUrls, josmUrls)
304 myprintln "*** URLs found in ELI but not in JOSM (${l1.size()}): ***"
305 if (!l1.isEmpty()) {
306 for (def l : l1) {
307 myprintln "-" + l
308 }
309 }
310
311 def l2 = inOneButNotTheOther(josmUrls, eliUrls)
312 myprintln "*** URLs found in JOSM but not in ELI (${l2.size()}): ***"
313 if (!l2.isEmpty()) {
314 for (def l : l2) {
315 myprintln "+" + l
316 }
317 }
318 }
319
320 void checkCommonEntries() {
321 myprintln "*** Same URL, but different name: ***"
322 for (def url : eliUrls.keySet()) {
323 def e = eliUrls.get(url)
324 if (!josmUrls.containsKey(url)) continue
325 def j = josmUrls.get(url)
326 def ename = getName(e).replace("'","’")
327 def jname = getName(j).replace("'","’")
328 if (!ename.equals(jname)) {
329 myprintln "* Name differs ('${getName(e)}' != '${getName(j)}'): $url"
330 }
331 }
332
333 myprintln "*** Same URL, but different type: ***"
334 for (def url : eliUrls.keySet()) {
335 def e = eliUrls.get(url)
336 if (!josmUrls.containsKey(url)) continue
337 def j = josmUrls.get(url)
338 if (!getType(e).equals(getType(j))) {
339 myprintln "* Type differs (${getType(e)} != ${getType(j)}): ${getName(j)} - $url"
340 }
341 }
342
343 myprintln "*** Same URL, but different zoom bounds: ***"
344 for (def url : eliUrls.keySet()) {
345 def e = eliUrls.get(url)
346 if (!josmUrls.containsKey(url)) continue
347 def j = josmUrls.get(url)
348
349 Integer eMinZoom = getMinZoom(e)
350 Integer jMinZoom = getMinZoom(j)
351 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
352 myprintln "* Minzoom differs (${eMinZoom} != ${jMinZoom}): ${getDescription(j)}"
353 }
354 Integer eMaxZoom = getMaxZoom(e)
355 Integer jMaxZoom = getMaxZoom(j)
356 if (eMaxZoom != jMaxZoom) {
357 myprintln "* Maxzoom differs (${eMaxZoom} != ${jMaxZoom}): ${getDescription(j)}"
358 }
359 }
360
361 myprintln "*** Same URL, but different country code: ***"
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 if (!getCountryCode(e).equals(getCountryCode(j))) {
367 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
368 }
369 }
370 myprintln "*** Same URL, but different quality: ***"
371 for (def url : eliUrls.keySet()) {
372 def e = eliUrls.get(url)
373 if (!josmUrls.containsKey(url)) {
374 def q = getQuality(e)
375 if("eli-best".equals(q)) {
376 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
377 }
378 continue
379 }
380 def j = josmUrls.get(url)
381 if (!getQuality(e).equals(getQuality(j))) {
382 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
383 }
384 }
385 myprintln "*** Same URL, but different dates: ***"
386 for (def url : eliUrls.keySet()) {
387 def ed = getDate(eliUrls.get(url))
388 if (!josmUrls.containsKey(url)) continue
389 def j = josmUrls.get(url)
390 def jd = getDate(j)
391 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
392 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
393 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
394 String ed2 = ed
395 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
396 if(reg != null && reg.count == 1) {
397 Calendar cal = Calendar.getInstance()
398 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)
399 cal.add(Calendar.DAY_OF_MONTH, -1)
400 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
401 if (reg[0][4] != null)
402 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
403 if (reg[0][6] != null)
404 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
405 }
406 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
407 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
408 String t = "'${ed}'"
409 if (!ed.equals(ef)) {
410 t += " or '${ef}'"
411 }
412 if (jd.isEmpty()) {
413 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
414 } else if (!ed.isEmpty()) {
415 myprintln "* Date differs (${t} != '${jd}'): ${getDescription(j)}"
416 } else if (!options.nomissingeli) {
417 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
418 }
419 }
420 }
421 myprintln "*** Mismatching shapes: ***"
422 for (def url : josmUrls.keySet()) {
423 def j = josmUrls.get(url)
424 def num = 1
425 for (def shape : getShapes(j)) {
426 def p = shape.getPoints()
427 if(!p[0].equals(p[p.size()-1])) {
428 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
429 }
430 for (def nump = 1; nump < p.size(); ++nump) {
431 if (p[nump-1] == p[nump]) {
432 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
433 }
434 }
435 ++num
436 }
437 }
438 for (def url : eliUrls.keySet()) {
439 def e = eliUrls.get(url)
440 def num = 1
441 def s = getShapes(e)
442 for (def shape : s) {
443 def p = shape.getPoints()
444 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
445 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
446 }
447 for (def nump = 1; nump < p.size(); ++nump) {
448 if (p[nump-1] == p[nump]) {
449 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
450 }
451 }
452 ++num
453 }
454 if (!josmUrls.containsKey(url)) {
455 continue
456 }
457 def j = josmUrls.get(url)
458 def js = getShapes(j)
459 if(!s.size() && js.size()) {
460 if(!options.nomissingeli) {
461 myprintln "+ No ELI shape: ${getDescription(j)}"
462 }
463 } else if(!js.size() && s.size()) {
464 // don't report boundary like 5 point shapes as difference
465 if (s.size() != 1 || s[0].getPoints().size() != 5) {
466 myprintln "- No JOSM shape: ${getDescription(j)}"
467 }
468 } else if(s.size() != js.size()) {
469 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
470 } else {
471 for(def nums = 0; nums < s.size(); ++nums) {
472 def ep = s[nums].getPoints()
473 def jp = js[nums].getPoints()
474 if(ep.size() != jp.size()) {
475 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
476 } else {
477 for(def nump = 0; nump < ep.size(); ++nump) {
478 def ept = ep[nump]
479 def jpt = jp[nump]
480 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
481 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
482 nump = ep.size()
483 num = s.size()
484 }
485 }
486 }
487 }
488 }
489 }
490 myprintln "*** Mismatching icons: ***"
491 for (def url : eliUrls.keySet()) {
492 def e = eliUrls.get(url)
493 if (!josmUrls.containsKey(url)) {
494 continue
495 }
496 def j = josmUrls.get(url)
497 def ij = getIcon(j)
498 def ie = getIcon(e)
499 if(ij != null && ie == null) {
500 if(!options.nomissingeli) {
501 myprintln "+ No ELI icon: ${getDescription(j)}"
502 }
503 } else if(ij == null && ie != null) {
504 myprintln "- No JOSM icon: ${getDescription(j)}"
505 } else if(!ij.equals(ie)) {
506 myprintln "* Different icons: ${getDescription(j)}"
507 }
508 }
509 myprintln "*** Miscellaneous checks: ***"
510 def josmIds = new HashMap<String, ImageryInfo>()
511 for (def url : josmUrls.keySet()) {
512 def j = josmUrls.get(url)
513 def id = getId(j)
514 if(josmMirrors.containsKey(url)) {
515 continue
516 }
517 if(id == null) {
518 myprintln "* No JOSM-ID: ${getDescription(j)}"
519 } else if(josmIds.containsKey(id)) {
520 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
521 } else {
522 josmIds.put(id, j)
523 }
524 def d = getDate(j)
525 if(!d.isEmpty()) {
526 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
527 if(reg == null || reg.count != 1) {
528 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
529 } else {
530 try {
531 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
532 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
533 if(second.compareTo(first) < 0) {
534 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
535 }
536 }
537 catch (Exception e) {
538 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
539 }
540 }
541 }
542 def js = getShapes(j)
543 if(js.size()) {
544 def minlat = 1000
545 def minlon = 1000
546 def maxlat = -1000
547 def maxlon = -1000
548 for(def s: js) {
549 for(def p: s.getPoints()) {
550 def lat = p.getLat()
551 def lon = p.getLon()
552 if(lat > maxlat) maxlat = lat
553 if(lon > maxlon) maxlon = lon
554 if(lat < minlat) minlat = lat
555 if(lon < minlon) minlon = lon
556 }
557 }
558 def b = j.getBounds()
559 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
560 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)}"
561 }
562 }
563 }
564 }
565
566 /**
567 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
568 */
569 static String getUrl(Object e) {
570 if (e instanceof ImageryInfo) return e.url
571 return e.get("properties").getString("url")
572 }
573 static String getDate(Object e) {
574 if (e instanceof ImageryInfo) return e.date ? e.date : ""
575 def p = e.get("properties")
576 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
577 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
578 if(!start.isEmpty() && !end.isEmpty())
579 return start+";"+end
580 else if(!start.isEmpty())
581 return start+";-"
582 else if(!end.isEmpty())
583 return "-;"+end
584 return ""
585 }
586 static Date verifyDate(String year, String month, String day) {
587 def date
588 if(year == null) {
589 date = "3000-01-01"
590 } else {
591 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
592 }
593 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
594 df.setLenient(false)
595 return df.parse(date)
596 }
597 static String getId(Object e) {
598 if (e instanceof ImageryInfo) return e.getId()
599 return e.get("properties").getString("id")
600 }
601 static String getName(Object e) {
602 if (e instanceof ImageryInfo) return e.getOriginalName()
603 return e.get("properties").getString("name")
604 }
605 static List<Object> getMirrors(Object e) {
606 if (e instanceof ImageryInfo) return e.getMirrors()
607 return []
608 }
609 static List<Shape> getShapes(Object e) {
610 if (e instanceof ImageryInfo) {
611 def bounds = e.getBounds()
612 if(bounds != null) {
613 return bounds.getShapes()
614 }
615 return []
616 }
617 if(!e.isNull("geometry")) {
618 def ex = e.get("geometry")
619 if(ex != null && !ex.isNull("coordinates")) {
620 def poly = ex.get("coordinates")
621 List<Shape> l = []
622 for(def shapes: poly) {
623 def s = new Shape()
624 for(def point: shapes) {
625 def lon = point[0].toString()
626 def lat = point[1].toString()
627 s.addPoint(lat, lon)
628 }
629 l.add(s)
630 }
631 return l
632 }
633 }
634 return []
635 }
636 static String getType(Object e) {
637 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
638 return e.get("properties").getString("type")
639 }
640 static Integer getMinZoom(Object e) {
641 if (e instanceof ImageryInfo) {
642 int mz = e.getMinZoom()
643 return mz == 0 ? null : mz
644 } else {
645 def num = e.get("properties").getJsonNumber("min_zoom")
646 if (num == null) return null
647 return num.intValue()
648 }
649 }
650 static Integer getMaxZoom(Object e) {
651 if (e instanceof ImageryInfo) {
652 int mz = e.getMaxZoom()
653 return mz == 0 ? null : mz
654 } else {
655 def num = e.get("properties").getJsonNumber("max_zoom")
656 if (num == null) return null
657 return num.intValue()
658 }
659 }
660 static String getCountryCode(Object e) {
661 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
662 return e.get("properties").getString("country_code", null)
663 }
664 static String getQuality(Object e) {
665 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
666 return (e.get("properties").containsKey("best")
667 && e.get("properties").getBoolean("best")) ? "eli-best" : null
668 }
669 static String getIcon(Object e) {
670 if (e instanceof ImageryInfo) return e.getIcon()
671 return e.get("properties").getString("icon", null)
672 }
673 String getDescription(Object o) {
674 def url = getUrl(o)
675 def cc = getCountryCode(o)
676 if (cc == null) {
677 def j = josmUrls.get(url)
678 if (j != null) cc = getCountryCode(j)
679 if (cc == null) {
680 def e = eliUrls.get(url)
681 if (e != null) cc = getCountryCode(e)
682 }
683 }
684 if (cc == null) {
685 cc = ''
686 } else {
687 cc = "[$cc] "
688 }
689 def d = cc + getName(o) + " - " + getUrl(o)
690 if (options.shorten) {
691 def MAXLEN = 140
692 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
693 }
694 return d
695 }
696}
Note: See TracBrowser for help on using the repository browser.