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

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

add country code and icon

  • Property svn:eol-style set to native
File size: 27.7 KB
RevLine 
[7726]1// License: GPL. For details, see LICENSE file.
2/**
[11854]3 * Compare and analyse the differences of the editor layer index and the JOSM imagery list.
[7726]4 * The goal is to keep both lists in sync.
5 *
[11854]6 * The editor layer index project (https://github.com/osmlab/editor-layer-index)
[11694]7 * provides also a version in the JOSM format, but the GEOJSON is the original source
[7726]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 *
[11854]15 * $ groovy -cp ../dist/josm-custom.jar SyncEditorLayerIndex.groovy
[9667]16 *
[7726]17 * Add option "-h" to show the available command line flags.
18 */
[11967]19import java.text.DecimalFormat
[7726]20import javax.json.Json
21import javax.json.JsonArray
22import javax.json.JsonObject
23import javax.json.JsonReader
24
[9953]25import org.openstreetmap.josm.data.imagery.ImageryInfo
[11410]26import org.openstreetmap.josm.data.imagery.Shape
[7726]27import org.openstreetmap.josm.io.imagery.ImageryReader
28
[11854]29class SyncEditorLayerIndex {
[7726]30
31 List<ImageryInfo> josmEntries;
[11582]32 JsonArray eliEntries;
[7726]33
[11582]34 def eliUrls = new HashMap<String, JsonObject>()
[7726]35 def josmUrls = new HashMap<String, ImageryInfo>()
[11420]36 def josmMirrors = new HashMap<String, ImageryInfo>()
[9667]37
[11965]38 static String eliInputFile = 'imagery_eli.geojson'
39 static String josmInputFile = 'imagery_josm.imagery.xml'
40 static String ignoreInputFile = 'imagery_josm.ignores.txt'
[9505]41 static FileWriter outputFile = null
42 static BufferedWriter outputStream = null
[11582]43 def skip = [:]
[9505]44
[7726]45 static def options
[9658]46
[7726]47 /**
48 * Main method.
49 */
50 static main(def args) {
[11967]51 Locale.setDefault(Locale.ROOT);
[7726]52 parse_command_line_arguments(args)
[11854]53 def script = new SyncEditorLayerIndex()
[9505]54 script.loadSkip()
[9658]55 script.start()
[7726]56 script.loadJosmEntries()
[11964]57 if(options.josmxml) {
58 def file = new FileWriter(options.josmxml)
59 def stream = new BufferedWriter(file)
60 script.printentries(script.josmEntries, stream)
61 }
[11582]62 script.loadELIEntries()
[11964]63 if(options.elixml) {
64 def file = new FileWriter(options.elixml)
65 def stream = new BufferedWriter(file)
66 script.printentries(script.eliEntries, stream)
67 }
[7726]68 script.checkInOneButNotTheOther()
69 script.checkCommonEntries()
[9658]70 script.end()
[9505]71 if(outputStream != null) {
72 outputStream.close();
73 }
74 if(outputFile != null) {
75 outputFile.close();
76 }
[7726]77 }
[9653]78
[7726]79 /**
80 * Parse command line arguments.
81 */
82 static void parse_command_line_arguments(args) {
[9658]83 def cli = new CliBuilder(width: 160)
[9505]84 cli.o(longOpt:'output', args:1, argName: "output", "Output file, - prints to stdout (default: -)")
[11854]85 cli.e(longOpt:'eli_input', args:1, argName:"eli_input", "Input file for the editor layer index (geojson). Default is $eliInputFile (current directory).")
[9505]86 cli.j(longOpt:'josm_input', args:1, argName:"josm_input", "Input file for the JOSM imagery list (xml). Default is $josmInputFile (current directory).")
[11238]87 cli.i(longOpt:'ignore_input', args:1, argName:"ignore_input", "Input file for the ignore list. Default is $ignoreInputFile (current directory).")
[7726]88 cli.s(longOpt:'shorten', "shorten the output, so it is easier to read in a console window")
[9505]89 cli.n(longOpt:'noskip', argName:"noskip", "don't skip known entries")
[9658]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")
[11964]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)")
[11965]94 cli.m(longOpt:'noeli', argName:"noeli", "don't show output for ELI problems")
[7726]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 }
[11582]102 if (options.eli_input) {
103 eliInputFile = options.eli_input
[7726]104 }
105 if (options.josm_input) {
106 josmInputFile = options.josm_input
107 }
[11238]108 if (options.ignore_input) {
109 ignoreInputFile = options.ignore_input
110 }
[9505]111 if (options.output && options.output != "-") {
112 outputFile = new FileWriter(options.output)
113 outputStream = new BufferedWriter(outputFile)
114 }
[7726]115 }
116
[9505]117 void loadSkip() {
[11238]118 FileReader fr = new FileReader(ignoreInputFile)
119 def line
120
121 while((line = fr.readLine()) != null) {
[11582]122 def res = (line =~ /^\|\| *(ELI|Ignore) *\|\| *\{\{\{(.+)\}\}\} *\|\|/)
[11238]123 if(res.count)
124 {
[11582]125 if(res[0][1].equals("Ignore")) {
126 skip[res[0][2]] = "green"
[11238]127 } else {
[11582]128 skip[res[0][2]] = "darkgoldenrod"
[11238]129 }
130 }
[11234]131 }
[11238]132 }
[9653]133
[9658]134 void myprintlnfinal(String s) {
135 if(outputStream != null) {
[11964]136 outputStream.write(s)
137 outputStream.newLine()
[9658]138 } else {
[11964]139 println s
[9658]140 }
141 }
142
[9505]143 void myprintln(String s) {
[11582]144 if(skip.containsKey(s)) {
145 String color = skip.get(s)
146 skip.remove(s)
[9658]147 if(options.xhtmlbody || options.xhtml) {
[11582]148 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9658]149 }
[9662]150 if (!options.noskip) {
[11964]151 return
[9662]152 }
[9658]153 } else if(options.xhtmlbody || options.xhtml) {
[11582]154 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ ELI")) ? "blue" : "red")
[9658]155 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9505]156 }
[11965]157 if ((s.startsWith("+ ") || s.startsWith("+++ ELI")) && options.noeli) {
158 return
159 }
[9658]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"
[11582]166 myprintlnfinal "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/><title>JOSM - ELI differences</title></head><body>\n"
[9505]167 }
168 }
[9653]169
[9658]170 void end() {
[11582]171 for (def s: skip.keySet()) {
[9658]172 myprintln "+++ Obsolete skip entry: " + s
173 }
174 if (options.xhtml) {
175 myprintlnfinal "</body></html>\n"
176 }
177 }
178
[11582]179 void loadELIEntries() {
180 FileReader fr = new FileReader(eliInputFile)
[7726]181 JsonReader jr = Json.createReader(fr)
[11582]182 eliEntries = jr.readObject().get("features")
[7726]183 jr.close()
[9653]184
[11582]185 for (def e : eliEntries) {
[7726]186 def url = getUrl(e)
[9653]187 if (url.contains("{z}")) {
[11582]188 myprintln "+++ ELI-URL uses {z} instead of {zoom}: "+url
[9653]189 url = url.replace("{z}","{zoom}")
190 }
[11582]191 if (eliUrls.containsKey(url)) {
192 myprintln "+++ ELI-URL is not unique: "+url
[9505]193 } else {
[11582]194 eliUrls.put(url, e)
[9505]195 }
[7726]196 }
[11582]197 myprintln "*** Loaded ${eliEntries.size()} entries (ELI). ***"
[7726]198 }
[11968]199 String cdata(def s) {
200 if(s =~ /[<>&]/)
201 return "<![CDATA[$s]]>"
202 return s
203 }
[7726]204
[11967]205 String maininfo(def entry, String offset) {
206 String res = offset + "<type>${getType(entry)}</type>\n"
[11968]207 res += offset + "<url>${cdata(getUrl(entry))}</url>\n"
[11967]208 if(getType(entry) == "tms") {
209 if(getMinZoom(entry) != null)
210 res += offset + "<min-zoom>${getMinZoom(entry)}</min-zoom>\n"
211 if(getMaxZoom(entry) != null)
212 res += offset + "<max-zoom>${getMaxZoom(entry)}</max-zoom>\n"
213 }
214 return res
215 }
216
[11964]217 void printentries(def entries, def stream) {
[11967]218 DecimalFormat df = new DecimalFormat("#.#######")
219 df.setRoundingMode(java.math.RoundingMode.CEILING)
[11964]220 stream.write "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
221 stream.write "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n"
222 for (def e : entries) {
223 def best = "eli-best".equals(getQuality(e))
224 stream.write " <entry"+(best ? " eli-best=\"true\"" : "" )+">\n"
225 stream.write " <name>${getName(e)}</name>\n"
226 stream.write " <id>${getId(e)}</id>\n"
[11968]227 def t
228 if((t = getDate(e)))
229 stream.write " <date>$t</date>\n"
230 if((t = getCountryCode(e)))
231 stream.write " <country-code>$t</country-code>\n"
232 if((t = getIcon(e)))
233 stream.write " <icon>${cdata(t)}</icon>\n"
[11967]234 stream.write maininfo(e, " ")
235 for (def m : getMirrors(e)) {
236 stream.write " <mirror>\n"+maininfo(m, " ")+" </mirror>\n"
237 }
[11964]238 def minlat = 1000
239 def minlon = 1000
240 def maxlat = -1000
241 def maxlon = -1000
242 def shapes = ""
243 def sep = "\n "
244 for(def s: getShapes(e)) {
245 shapes += " <shape>"
246 def i = 0
247 for(def p: s.getPoints()) {
248 def lat = p.getLat()
249 def lon = p.getLon()
250 if(lat > maxlat) maxlat = lat
251 if(lon > maxlon) maxlon = lon
252 if(lat < minlat) minlat = lat
253 if(lon < minlon) minlon = lon
254 if(!(i++%3)) {
255 shapes += sep + " "
256 }
[11967]257 shapes += "<point lat='${df.format(lat)}' lon='${df.format(lon)}'/>"
[11964]258 }
259 shapes += sep + "</shape>\n"
260 }
261 if(shapes) {
[11967]262 stream.write " <bounds min-lat='${df.format(minlat)}' min-lon='${df.format(minlon)}' max-lat='${df.format(maxlat)}' max-lon='${df.format(maxlon)}'>\n"
[11964]263 stream.write shapes + " </bounds>\n"
264 }
[11967]265 stream.write " </entry>\n"
[11964]266 }
267 stream.write "</imagery>\n"
268 stream.close()
269 }
270
[7726]271 void loadJosmEntries() {
272 def reader = new ImageryReader(josmInputFile)
273 josmEntries = reader.parse()
[9667]274
[7726]275 for (def e : josmEntries) {
276 def url = getUrl(e)
[9658]277 if (url.contains("{z}")) {
278 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
279 url = url.replace("{z}","{zoom}")
280 }
[9505]281 if (josmUrls.containsKey(url)) {
282 myprintln "+++ JOSM-URL is not unique: "+url
283 } else {
[11420]284 josmUrls.put(url, e)
[7726]285 }
[9658]286 for (def m : e.getMirrors()) {
287 url = getUrl(m)
[11574]288 m.origName = m.getOriginalName().replaceAll(" mirror server( \\d+)?","")
[9658]289 if (josmUrls.containsKey(url)) {
290 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
291 } else {
[11420]292 josmUrls.put(url, m)
293 josmMirrors.put(url, m)
[9658]294 }
295 }
[7726]296 }
[9505]297 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
[7726]298 }
299
300 List inOneButNotTheOther(Map m1, Map m2) {
301 def l = []
302 for (def url : m1.keySet()) {
303 if (!m2.containsKey(url)) {
304 def name = getName(m1.get(url))
305 l += " "+getDescription(m1.get(url))
306 }
307 }
308 l.sort()
309 }
[9667]310
[7726]311 void checkInOneButNotTheOther() {
[11582]312 def l1 = inOneButNotTheOther(eliUrls, josmUrls)
313 myprintln "*** URLs found in ELI but not in JOSM (${l1.size()}): ***"
[9658]314 if (!l1.isEmpty()) {
[11412]315 for (def l : l1) {
316 myprintln "-" + l
317 }
[7726]318 }
319
[11582]320 def l2 = inOneButNotTheOther(josmUrls, eliUrls)
321 myprintln "*** URLs found in JOSM but not in ELI (${l2.size()}): ***"
[9658]322 if (!l2.isEmpty()) {
[11412]323 for (def l : l2) {
[9658]324 myprintln "+" + l
[11412]325 }
[7726]326 }
327 }
[9667]328
[7726]329 void checkCommonEntries() {
[9505]330 myprintln "*** Same URL, but different name: ***"
[11582]331 for (def url : eliUrls.keySet()) {
332 def e = eliUrls.get(url)
[7726]333 if (!josmUrls.containsKey(url)) continue
334 def j = josmUrls.get(url)
[11951]335 def ename = getName(e).replace("'","’")
336 def jname = getName(j).replace("'","’")
337 if (!ename.equals(jname)) {
[11582]338 myprintln "* Name differs ('${getName(e)}' != '${getName(j)}'): $url"
[7726]339 }
340 }
[9667]341
[9505]342 myprintln "*** Same URL, but different type: ***"
[11582]343 for (def url : eliUrls.keySet()) {
344 def e = eliUrls.get(url)
[7726]345 if (!josmUrls.containsKey(url)) continue
346 def j = josmUrls.get(url)
347 if (!getType(e).equals(getType(j))) {
[11582]348 myprintln "* Type differs (${getType(e)} != ${getType(j)}): ${getName(j)} - $url"
[7726]349 }
350 }
[9667]351
[9505]352 myprintln "*** Same URL, but different zoom bounds: ***"
[11582]353 for (def url : eliUrls.keySet()) {
354 def e = eliUrls.get(url)
[7726]355 if (!josmUrls.containsKey(url)) continue
356 def j = josmUrls.get(url)
357
358 Integer eMinZoom = getMinZoom(e)
359 Integer jMinZoom = getMinZoom(j)
[9518]360 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
[11582]361 myprintln "* Minzoom differs (${eMinZoom} != ${jMinZoom}): ${getDescription(j)}"
[7726]362 }
363 Integer eMaxZoom = getMaxZoom(e)
364 Integer jMaxZoom = getMaxZoom(j)
365 if (eMaxZoom != jMaxZoom) {
[11582]366 myprintln "* Maxzoom differs (${eMaxZoom} != ${jMaxZoom}): ${getDescription(j)}"
[7726]367 }
368 }
[9667]369
[9505]370 myprintln "*** Same URL, but different country code: ***"
[11582]371 for (def url : eliUrls.keySet()) {
372 def e = eliUrls.get(url)
[7726]373 if (!josmUrls.containsKey(url)) continue
374 def j = josmUrls.get(url)
375 if (!getCountryCode(e).equals(getCountryCode(j))) {
[11582]376 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
[7726]377 }
378 }
[11599]379 myprintln "*** Same URL, but different quality: ***"
[11582]380 for (def url : eliUrls.keySet()) {
381 def e = eliUrls.get(url)
[9515]382 if (!josmUrls.containsKey(url)) {
383 def q = getQuality(e)
[11582]384 if("eli-best".equals(q)) {
385 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
[9515]386 }
387 continue
388 }
[9505]389 def j = josmUrls.get(url)
390 if (!getQuality(e).equals(getQuality(j))) {
[11582]391 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
[9505]392 }
[11599]393 }
[11665]394 myprintln "*** Same URL, but different dates: ***"
[11582]395 for (def url : eliUrls.keySet()) {
[11612]396 def ed = getDate(eliUrls.get(url))
[11573]397 if (!josmUrls.containsKey(url)) continue
398 def j = josmUrls.get(url)
[11612]399 def jd = getDate(j)
400 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
[11964]401 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
[11639]402 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
[11964]403 String ed2 = ed
[11639]404 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
405 if(reg != null && reg.count == 1) {
[11964]406 Calendar cal = Calendar.getInstance()
[11639]407 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)
408 cal.add(Calendar.DAY_OF_MONTH, -1)
[11667]409 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
[11639]410 if (reg[0][4] != null)
411 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
412 if (reg[0][6] != null)
413 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
414 }
[11964]415 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
[11639]416 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
[11964]417 String t = "'${ed}'"
[11612]418 if (!ed.equals(ef)) {
[11964]419 t += " or '${ef}'"
[11612]420 }
[11666]421 if (jd.isEmpty()) {
422 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
[11668]423 } else if (!ed.isEmpty()) {
424 myprintln "* Date differs (${t} != '${jd}'): ${getDescription(j)}"
425 } else if (!options.nomissingeli) {
[11666]426 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
427 }
[11573]428 }
[11665]429 }
[11410]430 myprintln "*** Mismatching shapes: ***"
431 for (def url : josmUrls.keySet()) {
432 def j = josmUrls.get(url)
433 def num = 1
434 for (def shape : getShapes(j)) {
435 def p = shape.getPoints()
436 if(!p[0].equals(p[p.size()-1])) {
437 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
438 }
[11964]439 for (def nump = 1; nump < p.size(); ++nump) {
440 if (p[nump-1] == p[nump]) {
441 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
442 }
443 }
[11410]444 ++num
445 }
446 }
[11582]447 for (def url : eliUrls.keySet()) {
448 def e = eliUrls.get(url)
[11410]449 def num = 1
450 def s = getShapes(e)
451 for (def shape : s) {
452 def p = shape.getPoints()
[11582]453 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
454 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
[11410]455 }
[11964]456 for (def nump = 1; nump < p.size(); ++nump) {
457 if (p[nump-1] == p[nump]) {
458 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
459 }
460 }
[11410]461 ++num
462 }
463 if (!josmUrls.containsKey(url)) {
464 continue
465 }
466 def j = josmUrls.get(url)
[11411]467 def js = getShapes(j)
[11414]468 if(!s.size() && js.size()) {
[11582]469 if(!options.nomissingeli) {
470 myprintln "+ No ELI shape: ${getDescription(j)}"
[11414]471 }
[11413]472 } else if(!js.size() && s.size()) {
[11415]473 // don't report boundary like 5 point shapes as difference
474 if (s.size() != 1 || s[0].getPoints().size() != 5) {
475 myprintln "- No JOSM shape: ${getDescription(j)}"
476 }
[11414]477 } else if(s.size() != js.size()) {
478 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
[11413]479 } else {
[11414]480 for(def nums = 0; nums < s.size(); ++nums) {
481 def ep = s[nums].getPoints()
482 def jp = js[nums].getPoints()
483 if(ep.size() != jp.size()) {
484 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
485 } else {
486 for(def nump = 0; nump < ep.size(); ++nump) {
487 def ept = ep[nump]
488 def jpt = jp[nump]
489 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
490 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
491 nump = ep.size()
492 num = s.size()
[11413]493 }
494 }
495 }
[11411]496 }
[11410]497 }
498 }
[11420]499 myprintln "*** Mismatching icons: ***"
[11582]500 for (def url : eliUrls.keySet()) {
501 def e = eliUrls.get(url)
[11420]502 if (!josmUrls.containsKey(url)) {
503 continue
504 }
505 def j = josmUrls.get(url)
506 def ij = getIcon(j)
507 def ie = getIcon(e)
508 if(ij != null && ie == null) {
[11582]509 if(!options.nomissingeli) {
510 myprintln "+ No ELI icon: ${getDescription(j)}"
[11420]511 }
512 } else if(ij == null && ie != null) {
513 myprintln "- No JOSM icon: ${getDescription(j)}"
514 } else if(!ij.equals(ie)) {
515 myprintln "* Different icons: ${getDescription(j)}"
516 }
517 }
518 myprintln "*** Miscellaneous checks: ***"
519 def josmIds = new HashMap<String, ImageryInfo>()
520 for (def url : josmUrls.keySet()) {
521 def j = josmUrls.get(url)
522 def id = getId(j)
523 if(josmMirrors.containsKey(url)) {
[11964]524 continue
[11420]525 }
526 if(id == null) {
527 myprintln "* No JOSM-ID: ${getDescription(j)}"
528 } else if(josmIds.containsKey(id)) {
529 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
530 } else {
[11964]531 josmIds.put(id, j)
[11420]532 }
[11572]533 def d = getDate(j)
[11573]534 if(!d.isEmpty()) {
[11639]535 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
[11572]536 if(reg == null || reg.count != 1) {
537 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
538 } else {
539 try {
[11964]540 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
541 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
[11572]542 if(second.compareTo(first) < 0) {
543 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
544 }
545 }
546 catch (Exception e) {
547 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
548 }
549 }
[11603]550 }
[11422]551 def js = getShapes(j)
552 if(js.size()) {
[11964]553 def minlat = 1000
554 def minlon = 1000
555 def maxlat = -1000
556 def maxlon = -1000
[11422]557 for(def s: js) {
558 for(def p: s.getPoints()) {
[11964]559 def lat = p.getLat()
560 def lon = p.getLon()
561 if(lat > maxlat) maxlat = lat
562 if(lon > maxlon) maxlon = lon
563 if(lat < minlat) minlat = lat
564 if(lon < minlon) minlon = lon
[11422]565 }
566 }
[11964]567 def b = j.getBounds()
[11422]568 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
[11423]569 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)}"
[11422]570 }
571 }
[11420]572 }
[7726]573 }
[9667]574
[7726]575 /**
576 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
577 */
578 static String getUrl(Object e) {
579 if (e instanceof ImageryInfo) return e.url
[11412]580 return e.get("properties").getString("url")
[7726]581 }
[11572]582 static String getDate(Object e) {
[11573]583 if (e instanceof ImageryInfo) return e.date ? e.date : ""
584 def p = e.get("properties")
585 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
586 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
587 if(!start.isEmpty() && !end.isEmpty())
[11572]588 return start+";"+end
[11573]589 else if(!start.isEmpty())
[11612]590 return start+";-"
591 else if(!end.isEmpty())
592 return "-;"+end
[11964]593 return ""
[11572]594 }
595 static Date verifyDate(String year, String month, String day) {
596 def date
[11854]597 if(year == null) {
[11572]598 date = "3000-01-01"
[11854]599 } else {
[11572]600 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
[11854]601 }
[11572]602 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
603 df.setLenient(false)
604 return df.parse(date)
605 }
[11420]606 static String getId(Object e) {
607 if (e instanceof ImageryInfo) return e.getId()
608 return e.get("properties").getString("id")
609 }
[7726]610 static String getName(Object e) {
[10517]611 if (e instanceof ImageryInfo) return e.getOriginalName()
[11412]612 return e.get("properties").getString("name")
[7726]613 }
[11967]614 static List<Object> getMirrors(Object e) {
615 if (e instanceof ImageryInfo) return e.getMirrors()
616 return []
617 }
[11410]618 static List<Shape> getShapes(Object e) {
619 if (e instanceof ImageryInfo) {
[11964]620 def bounds = e.getBounds()
[11411]621 if(bounds != null) {
[11964]622 return bounds.getShapes()
[11411]623 }
624 return []
[11410]625 }
[11412]626 if(!e.isNull("geometry")) {
627 def ex = e.get("geometry")
628 if(ex != null && !ex.isNull("coordinates")) {
629 def poly = ex.get("coordinates")
[11410]630 List<Shape> l = []
631 for(def shapes: poly) {
632 def s = new Shape()
633 for(def point: shapes) {
634 def lon = point[0].toString()
635 def lat = point[1].toString()
636 s.addPoint(lat, lon)
637 }
638 l.add(s)
639 }
640 return l
641 }
642 }
643 return []
644 }
[7726]645 static String getType(Object e) {
646 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
[11412]647 return e.get("properties").getString("type")
[7726]648 }
649 static Integer getMinZoom(Object e) {
650 if (e instanceof ImageryInfo) {
651 int mz = e.getMinZoom()
652 return mz == 0 ? null : mz
653 } else {
[11412]654 def num = e.get("properties").getJsonNumber("min_zoom")
[7726]655 if (num == null) return null
656 return num.intValue()
657 }
658 }
659 static Integer getMaxZoom(Object e) {
660 if (e instanceof ImageryInfo) {
661 int mz = e.getMaxZoom()
662 return mz == 0 ? null : mz
663 } else {
[11412]664 def num = e.get("properties").getJsonNumber("max_zoom")
[7726]665 if (num == null) return null
666 return num.intValue()
667 }
668 }
669 static String getCountryCode(Object e) {
670 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
[11412]671 return e.get("properties").getString("country_code", null)
[7726]672 }
[9505]673 static String getQuality(Object e) {
[11575]674 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
[11603]675 return (e.get("properties").containsKey("best")
676 && e.get("properties").getBoolean("best")) ? "eli-best" : null
[9505]677 }
[11420]678 static String getIcon(Object e) {
679 if (e instanceof ImageryInfo) return e.getIcon()
680 return e.get("properties").getString("icon", null)
681 }
[7726]682 String getDescription(Object o) {
683 def url = getUrl(o)
684 def cc = getCountryCode(o)
685 if (cc == null) {
686 def j = josmUrls.get(url)
687 if (j != null) cc = getCountryCode(j)
688 if (cc == null) {
[11582]689 def e = eliUrls.get(url)
[7726]690 if (e != null) cc = getCountryCode(e)
691 }
692 }
693 if (cc == null) {
694 cc = ''
695 } else {
696 cc = "[$cc] "
697 }
698 def d = cc + getName(o) + " - " + getUrl(o)
699 if (options.shorten) {
700 def MAXLEN = 140
701 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
702 }
703 return d
704 }
705}
Note: See TracBrowser for help on using the repository browser.