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

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

see #14655 - implement nearly all features in ELI sync XML output

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