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

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

writer no longer has a newLine() function

  • Property svn:eol-style set to native
File size: 34.8 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'
[12066]41 static FileOutputStream outputFile = null
42 static OutputStreamWriter 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) {
[12066]58 def file = new FileOutputStream(options.josmxml)
59 def stream = new OutputStreamWriter(file, "UTF-8")
[11964]60 script.printentries(script.josmEntries, stream)
[12066]61 stream.close();
62 file.close();
[11964]63 }
[11582]64 script.loadELIEntries()
[11964]65 if(options.elixml) {
[12066]66 def file = new FileOutputStream(options.elixml)
67 def stream = new OutputStreamWriter(file, "UTF-8")
[11964]68 script.printentries(script.eliEntries, stream)
[12066]69 stream.close();
70 file.close();
[11964]71 }
[7726]72 script.checkInOneButNotTheOther()
73 script.checkCommonEntries()
[9658]74 script.end()
[9505]75 if(outputStream != null) {
76 outputStream.close();
77 }
78 if(outputFile != null) {
79 outputFile.close();
80 }
[7726]81 }
[9653]82
[7726]83 /**
84 * Parse command line arguments.
85 */
86 static void parse_command_line_arguments(args) {
[9658]87 def cli = new CliBuilder(width: 160)
[9505]88 cli.o(longOpt:'output', args:1, argName: "output", "Output file, - prints to stdout (default: -)")
[11854]89 cli.e(longOpt:'eli_input', args:1, argName:"eli_input", "Input file for the editor layer index (geojson). Default is $eliInputFile (current directory).")
[9505]90 cli.j(longOpt:'josm_input', args:1, argName:"josm_input", "Input file for the JOSM imagery list (xml). Default is $josmInputFile (current directory).")
[11238]91 cli.i(longOpt:'ignore_input', args:1, argName:"ignore_input", "Input file for the ignore list. Default is $ignoreInputFile (current directory).")
[7726]92 cli.s(longOpt:'shorten', "shorten the output, so it is easier to read in a console window")
[9505]93 cli.n(longOpt:'noskip', argName:"noskip", "don't skip known entries")
[9658]94 cli.x(longOpt:'xhtmlbody', argName:"xhtmlbody", "create XHTML body for display in a web page")
95 cli.X(longOpt:'xhtml', argName:"xhtml", "create XHTML for display in a web page")
[11964]96 cli.p(longOpt:'elixml', args:1, argName:"elixml", "ELI entries for use in JOSM as XML file (incomplete)")
97 cli.q(longOpt:'josmxml', args:1, argName:"josmxml", "JOSM entries reoutput as XML file (incomplete)")
[11965]98 cli.m(longOpt:'noeli', argName:"noeli", "don't show output for ELI problems")
[7726]99 cli.h(longOpt:'help', "show this help")
100 options = cli.parse(args)
101
102 if (options.h) {
103 cli.usage()
104 System.exit(0)
105 }
[11582]106 if (options.eli_input) {
107 eliInputFile = options.eli_input
[7726]108 }
109 if (options.josm_input) {
110 josmInputFile = options.josm_input
111 }
[11238]112 if (options.ignore_input) {
113 ignoreInputFile = options.ignore_input
114 }
[9505]115 if (options.output && options.output != "-") {
[12066]116 outputFile = new FileOutputStream(options.output)
117 outputStream = new OutputStreamWriter(outputFile, "UTF-8")
[9505]118 }
[7726]119 }
120
[9505]121 void loadSkip() {
[12061]122 def fr = new InputStreamReader(new FileInputStream(ignoreInputFile), "UTF-8")
[11238]123 def line
124
125 while((line = fr.readLine()) != null) {
[11582]126 def res = (line =~ /^\|\| *(ELI|Ignore) *\|\| *\{\{\{(.+)\}\}\} *\|\|/)
[11238]127 if(res.count)
128 {
[11582]129 if(res[0][1].equals("Ignore")) {
130 skip[res[0][2]] = "green"
[11238]131 } else {
[11582]132 skip[res[0][2]] = "darkgoldenrod"
[11238]133 }
134 }
[11234]135 }
[11238]136 }
[9653]137
[9658]138 void myprintlnfinal(String s) {
139 if(outputStream != null) {
[12067]140 outputStream.write(s+System.getProperty("line.separator"))
[9658]141 } else {
[11964]142 println s
[9658]143 }
144 }
145
[9505]146 void myprintln(String s) {
[11582]147 if(skip.containsKey(s)) {
148 String color = skip.get(s)
149 skip.remove(s)
[9658]150 if(options.xhtmlbody || options.xhtml) {
[11582]151 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9658]152 }
[9662]153 if (!options.noskip) {
[11964]154 return
[9662]155 }
[9658]156 } else if(options.xhtmlbody || options.xhtml) {
[11582]157 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ ELI")) ? "blue" : "red")
[9658]158 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9505]159 }
[11965]160 if ((s.startsWith("+ ") || s.startsWith("+++ ELI")) && options.noeli) {
161 return
162 }
[9658]163 myprintlnfinal(s)
164 }
165
166 void start() {
167 if (options.xhtml) {
168 myprintlnfinal "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
[11582]169 myprintlnfinal "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/><title>JOSM - ELI differences</title></head><body>\n"
[9505]170 }
171 }
[9653]172
[9658]173 void end() {
[11582]174 for (def s: skip.keySet()) {
[9658]175 myprintln "+++ Obsolete skip entry: " + s
176 }
177 if (options.xhtml) {
178 myprintlnfinal "</body></html>\n"
179 }
180 }
181
[11582]182 void loadELIEntries() {
183 FileReader fr = new FileReader(eliInputFile)
[7726]184 JsonReader jr = Json.createReader(fr)
[11582]185 eliEntries = jr.readObject().get("features")
[7726]186 jr.close()
[9653]187
[11582]188 for (def e : eliEntries) {
[7726]189 def url = getUrl(e)
[9653]190 if (url.contains("{z}")) {
[11582]191 myprintln "+++ ELI-URL uses {z} instead of {zoom}: "+url
[9653]192 url = url.replace("{z}","{zoom}")
193 }
[11582]194 if (eliUrls.containsKey(url)) {
195 myprintln "+++ ELI-URL is not unique: "+url
[9505]196 } else {
[11582]197 eliUrls.put(url, e)
[9505]198 }
[7726]199 }
[11582]200 myprintln "*** Loaded ${eliEntries.size()} entries (ELI). ***"
[7726]201 }
[11975]202 String cdata(def s, boolean escape = false) {
203 if(escape) {
204 return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;")
205 } else if(s =~ /[<>&]/)
[11968]206 return "<![CDATA[$s]]>"
207 return s
208 }
[7726]209
[11967]210 String maininfo(def entry, String offset) {
[11975]211 String t = getType(entry)
212 String res = offset + "<type>$t</type>\n"
[11968]213 res += offset + "<url>${cdata(getUrl(entry))}</url>\n"
[11975]214 if(t == "tms") {
[11967]215 if(getMinZoom(entry) != null)
216 res += offset + "<min-zoom>${getMinZoom(entry)}</min-zoom>\n"
217 if(getMaxZoom(entry) != null)
218 res += offset + "<max-zoom>${getMaxZoom(entry)}</max-zoom>\n"
[11975]219 } else if (t == "wms") {
220 def p = getProjections(entry)
221 if (p) {
222 res += offset + "<projections>\n"
223 for (def c : p)
224 res += offset + " <code>$c</code>\n"
225 res += offset + "</projections>\n"
226 }
[11967]227 }
228 return res
229 }
[12061]230
[11964]231 void printentries(def entries, def stream) {
[11967]232 DecimalFormat df = new DecimalFormat("#.#######")
233 df.setRoundingMode(java.math.RoundingMode.CEILING)
[11964]234 stream.write "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
235 stream.write "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n"
236 for (def e : entries) {
237 def best = "eli-best".equals(getQuality(e))
238 stream.write " <entry"+(best ? " eli-best=\"true\"" : "" )+">\n"
[11975]239 stream.write " <name>${cdata(getName(e), true)}</name>\n"
[11964]240 stream.write " <id>${getId(e)}</id>\n"
[11968]241 def t
242 if((t = getDate(e)))
243 stream.write " <date>$t</date>\n"
244 if((t = getCountryCode(e)))
245 stream.write " <country-code>$t</country-code>\n"
[11975]246 stream.write maininfo(e, " ")
247 if((t = getAttributionText(e)))
248 stream.write " <attribution-text mandatory=\"true\">${cdata(t, true)}</attribution-text>\n"
249 if((t = getAttributionUrl(e)))
250 stream.write " <attribution-url>${cdata(t)}</attribution-url>\n"
251 if((t = getTermsOfUseText(e)))
252 stream.write " <terms-of-use-text>${cdata(t, true)}</terms-of-use-text>\n"
253 if((t = getTermsOfUseUrl(e)))
254 stream.write " <terms-of-use-url>${cdata(t)}</terms-of-use-url>\n"
255 if((t = getPermissionReferenceUrl(e)))
256 stream.write " <permission-ref>${cdata(t)}</permission-ref>\n"
257 if((getValidGeoreference(e)))
258 stream.write " <valid-georeference>true</valid-georeference>\n"
[11968]259 if((t = getIcon(e)))
260 stream.write " <icon>${cdata(t)}</icon>\n"
[11975]261 for (def d : getDescriptions(e)) {
262 stream.write " <description lang=\"${d.getKey()}\">${d.getValue()}</description>\n"
263 }
[11967]264 for (def m : getMirrors(e)) {
265 stream.write " <mirror>\n"+maininfo(m, " ")+" </mirror>\n"
266 }
[11964]267 def minlat = 1000
268 def minlon = 1000
269 def maxlat = -1000
270 def maxlon = -1000
271 def shapes = ""
272 def sep = "\n "
273 for(def s: getShapes(e)) {
274 shapes += " <shape>"
275 def i = 0
276 for(def p: s.getPoints()) {
277 def lat = p.getLat()
278 def lon = p.getLon()
279 if(lat > maxlat) maxlat = lat
280 if(lon > maxlon) maxlon = lon
281 if(lat < minlat) minlat = lat
282 if(lon < minlon) minlon = lon
283 if(!(i++%3)) {
284 shapes += sep + " "
285 }
[11967]286 shapes += "<point lat='${df.format(lat)}' lon='${df.format(lon)}'/>"
[11964]287 }
288 shapes += sep + "</shape>\n"
289 }
290 if(shapes) {
[11967]291 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]292 stream.write shapes + " </bounds>\n"
293 }
[11967]294 stream.write " </entry>\n"
[11964]295 }
296 stream.write "</imagery>\n"
297 stream.close()
298 }
299
[7726]300 void loadJosmEntries() {
301 def reader = new ImageryReader(josmInputFile)
302 josmEntries = reader.parse()
[9667]303
[7726]304 for (def e : josmEntries) {
305 def url = getUrl(e)
[9658]306 if (url.contains("{z}")) {
307 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
308 url = url.replace("{z}","{zoom}")
309 }
[9505]310 if (josmUrls.containsKey(url)) {
311 myprintln "+++ JOSM-URL is not unique: "+url
312 } else {
[11420]313 josmUrls.put(url, e)
[7726]314 }
[9658]315 for (def m : e.getMirrors()) {
316 url = getUrl(m)
[11574]317 m.origName = m.getOriginalName().replaceAll(" mirror server( \\d+)?","")
[9658]318 if (josmUrls.containsKey(url)) {
319 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
320 } else {
[11420]321 josmUrls.put(url, m)
322 josmMirrors.put(url, m)
[9658]323 }
324 }
[7726]325 }
[9505]326 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
[7726]327 }
328
329 List inOneButNotTheOther(Map m1, Map m2) {
330 def l = []
331 for (def url : m1.keySet()) {
332 if (!m2.containsKey(url)) {
333 def name = getName(m1.get(url))
334 l += " "+getDescription(m1.get(url))
335 }
336 }
337 l.sort()
338 }
[9667]339
[7726]340 void checkInOneButNotTheOther() {
[11582]341 def l1 = inOneButNotTheOther(eliUrls, josmUrls)
342 myprintln "*** URLs found in ELI but not in JOSM (${l1.size()}): ***"
[9658]343 if (!l1.isEmpty()) {
[11412]344 for (def l : l1) {
345 myprintln "-" + l
346 }
[7726]347 }
348
[11582]349 def l2 = inOneButNotTheOther(josmUrls, eliUrls)
350 myprintln "*** URLs found in JOSM but not in ELI (${l2.size()}): ***"
[9658]351 if (!l2.isEmpty()) {
[11412]352 for (def l : l2) {
[9658]353 myprintln "+" + l
[11412]354 }
[7726]355 }
356 }
[9667]357
[7726]358 void checkCommonEntries() {
[9505]359 myprintln "*** Same URL, but different name: ***"
[11582]360 for (def url : eliUrls.keySet()) {
361 def e = eliUrls.get(url)
[7726]362 if (!josmUrls.containsKey(url)) continue
363 def j = josmUrls.get(url)
[12061]364 def ename = getName(e).replace("'","\u2019")
365 def jname = getName(j).replace("'","\u2019")
[11951]366 if (!ename.equals(jname)) {
[11582]367 myprintln "* Name differs ('${getName(e)}' != '${getName(j)}'): $url"
[7726]368 }
369 }
[9667]370
[9505]371 myprintln "*** Same URL, but different type: ***"
[11582]372 for (def url : eliUrls.keySet()) {
373 def e = eliUrls.get(url)
[7726]374 if (!josmUrls.containsKey(url)) continue
375 def j = josmUrls.get(url)
376 if (!getType(e).equals(getType(j))) {
[11582]377 myprintln "* Type differs (${getType(e)} != ${getType(j)}): ${getName(j)} - $url"
[7726]378 }
379 }
[9667]380
[9505]381 myprintln "*** Same URL, but different zoom bounds: ***"
[11582]382 for (def url : eliUrls.keySet()) {
383 def e = eliUrls.get(url)
[7726]384 if (!josmUrls.containsKey(url)) continue
385 def j = josmUrls.get(url)
386
387 Integer eMinZoom = getMinZoom(e)
388 Integer jMinZoom = getMinZoom(j)
[9518]389 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
[11582]390 myprintln "* Minzoom differs (${eMinZoom} != ${jMinZoom}): ${getDescription(j)}"
[7726]391 }
392 Integer eMaxZoom = getMaxZoom(e)
393 Integer jMaxZoom = getMaxZoom(j)
394 if (eMaxZoom != jMaxZoom) {
[11582]395 myprintln "* Maxzoom differs (${eMaxZoom} != ${jMaxZoom}): ${getDescription(j)}"
[7726]396 }
397 }
[9667]398
[9505]399 myprintln "*** Same URL, but different country code: ***"
[11582]400 for (def url : eliUrls.keySet()) {
401 def e = eliUrls.get(url)
[7726]402 if (!josmUrls.containsKey(url)) continue
403 def j = josmUrls.get(url)
404 if (!getCountryCode(e).equals(getCountryCode(j))) {
[11582]405 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
[7726]406 }
407 }
[11599]408 myprintln "*** Same URL, but different quality: ***"
[11582]409 for (def url : eliUrls.keySet()) {
410 def e = eliUrls.get(url)
[9515]411 if (!josmUrls.containsKey(url)) {
412 def q = getQuality(e)
[11582]413 if("eli-best".equals(q)) {
414 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
[9515]415 }
416 continue
417 }
[9505]418 def j = josmUrls.get(url)
419 if (!getQuality(e).equals(getQuality(j))) {
[11582]420 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
[9505]421 }
[11599]422 }
[11665]423 myprintln "*** Same URL, but different dates: ***"
[11582]424 for (def url : eliUrls.keySet()) {
[11612]425 def ed = getDate(eliUrls.get(url))
[11573]426 if (!josmUrls.containsKey(url)) continue
427 def j = josmUrls.get(url)
[11612]428 def jd = getDate(j)
429 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
[11964]430 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
[11639]431 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
[11964]432 String ed2 = ed
[11639]433 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
434 if(reg != null && reg.count == 1) {
[11964]435 Calendar cal = Calendar.getInstance()
[11639]436 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)
437 cal.add(Calendar.DAY_OF_MONTH, -1)
[11667]438 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
[11639]439 if (reg[0][4] != null)
440 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
441 if (reg[0][6] != null)
442 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
443 }
[11964]444 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
[11639]445 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
[11964]446 String t = "'${ed}'"
[11612]447 if (!ed.equals(ef)) {
[11964]448 t += " or '${ef}'"
[11612]449 }
[11666]450 if (jd.isEmpty()) {
451 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
[11668]452 } else if (!ed.isEmpty()) {
453 myprintln "* Date differs (${t} != '${jd}'): ${getDescription(j)}"
454 } else if (!options.nomissingeli) {
[11666]455 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
456 }
[11573]457 }
[11665]458 }
[11981]459 myprintln "*** Same URL, but different information: ***"
460 for (def url : eliUrls.keySet()) {
461 if (!josmUrls.containsKey(url)) continue
462 def e = eliUrls.get(url)
463 def j = josmUrls.get(url)
464
465 def et = getDescriptions(e)
466 def jt = getDescriptions(j)
[12008]467 et = (et.size() > 0) ? et["en"] : ""
468 jt = (jt.size() > 0) ? jt["en"] : ""
469 def et2 = et.replaceAll("channels (\\d+) ", "\$1 channels ") // imagico entries
470 if (!et.equals(jt) && !(et && jt && (et.endsWith(jt) || et2.endsWith(jt)))) {
[11981]471 if (!jt) {
472 myprintln "+ SKIP - Missing JOSM description (${et}): ${getDescription(j)}"
473 } else if (et) {
474 myprintln "+ SKIP * Description differs (${et} != '${jt}'): ${getDescription(j)}"
475 } else if (!options.nomissingeli) {
476 myprintln "+ Missing ELI description ('${jt}'): ${getDescription(j)}"
477 }
478 }
479
480 et = getPermissionReferenceUrl(e)
481 jt = getPermissionReferenceUrl(j)
482 if (!jt) jt = getTermsOfUseUrl(j)
483 if (!et.equals(jt)) {
484 if (!jt) {
485 myprintln "+ SKIP - Missing JOSM license URL (${et}): ${getDescription(j)}"
486 } else if (et) {
487 myprintln "+ SKIP * License URL differs (${et} != '${jt}'): ${getDescription(j)}"
488 } else if (!options.nomissingeli) {
489 myprintln "+ Missing ELI license URL ('${jt}'): ${getDescription(j)}"
490 }
491 }
492
493 et = getAttributionUrl(e)
494 jt = getAttributionUrl(j)
495 if (!et.equals(jt)) {
496 if (!jt) {
497 myprintln "+ SKIP - Missing JOSM attribution URL (${et}): ${getDescription(j)}"
498 } else if (et) {
499 myprintln "+ SKIP * Attribution URL differs (${et} != '${jt}'): ${getDescription(j)}"
500 } else if (!options.nomissingeli) {
501 myprintln "+ Missing ELI attribution URL ('${jt}'): ${getDescription(j)}"
502 }
503 }
504
505 et = getAttributionText(e)
506 jt = getAttributionText(j)
507 if (!et.equals(jt)) {
508 if (!jt) {
509 myprintln "+ SKIP - Missing JOSM attribution text (${et}): ${getDescription(j)}"
510 } else if (et) {
511 myprintln "+ SKIP * Attribution text differs (${et} != '${jt}'): ${getDescription(j)}"
512 } else if (!options.nomissingeli) {
513 myprintln "+ Missing ELI attribution text ('${jt}'): ${getDescription(j)}"
514 }
515 }
516
517 et = getProjections(e)
518 jt = getProjections(j)
[11983]519 if (et) { et = new LinkedList(et); Collections.sort(et); et = String.join(" ", et) }
520 if (jt) { jt = new LinkedList(jt); Collections.sort(jt); jt = String.join(" ", jt) }
[11981]521 if (!et.equals(jt)) {
522 if (!jt) {
523 myprintln "+ SKIP - Missing JOSM projections (${et}): ${getDescription(j)}"
524 } else if (et) {
525 myprintln "+ SKIP * Projections differ (${et} != '${jt}'): ${getDescription(j)}"
526 } else if (!options.nomissingeli) {
527 myprintln "+ Missing ELI projections ('${jt}'): ${getDescription(j)}"
528 }
529 }
530 }
[11410]531 myprintln "*** Mismatching shapes: ***"
532 for (def url : josmUrls.keySet()) {
533 def j = josmUrls.get(url)
534 def num = 1
535 for (def shape : getShapes(j)) {
536 def p = shape.getPoints()
537 if(!p[0].equals(p[p.size()-1])) {
538 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
539 }
[11964]540 for (def nump = 1; nump < p.size(); ++nump) {
541 if (p[nump-1] == p[nump]) {
542 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
543 }
544 }
[11410]545 ++num
546 }
547 }
[11582]548 for (def url : eliUrls.keySet()) {
549 def e = eliUrls.get(url)
[11410]550 def num = 1
551 def s = getShapes(e)
552 for (def shape : s) {
553 def p = shape.getPoints()
[11582]554 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
555 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
[11410]556 }
[11964]557 for (def nump = 1; nump < p.size(); ++nump) {
558 if (p[nump-1] == p[nump]) {
559 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
560 }
561 }
[11410]562 ++num
563 }
564 if (!josmUrls.containsKey(url)) {
565 continue
566 }
567 def j = josmUrls.get(url)
[11411]568 def js = getShapes(j)
[11414]569 if(!s.size() && js.size()) {
[11582]570 if(!options.nomissingeli) {
571 myprintln "+ No ELI shape: ${getDescription(j)}"
[11414]572 }
[11413]573 } else if(!js.size() && s.size()) {
[11415]574 // don't report boundary like 5 point shapes as difference
575 if (s.size() != 1 || s[0].getPoints().size() != 5) {
576 myprintln "- No JOSM shape: ${getDescription(j)}"
577 }
[11414]578 } else if(s.size() != js.size()) {
579 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
[11413]580 } else {
[11414]581 for(def nums = 0; nums < s.size(); ++nums) {
582 def ep = s[nums].getPoints()
583 def jp = js[nums].getPoints()
584 if(ep.size() != jp.size()) {
585 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
586 } else {
587 for(def nump = 0; nump < ep.size(); ++nump) {
588 def ept = ep[nump]
589 def jpt = jp[nump]
590 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
591 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
592 nump = ep.size()
593 num = s.size()
[11413]594 }
595 }
596 }
[11411]597 }
[11410]598 }
599 }
[11420]600 myprintln "*** Mismatching icons: ***"
[11582]601 for (def url : eliUrls.keySet()) {
602 def e = eliUrls.get(url)
[11420]603 if (!josmUrls.containsKey(url)) {
604 continue
605 }
606 def j = josmUrls.get(url)
607 def ij = getIcon(j)
608 def ie = getIcon(e)
609 if(ij != null && ie == null) {
[11582]610 if(!options.nomissingeli) {
611 myprintln "+ No ELI icon: ${getDescription(j)}"
[11420]612 }
613 } else if(ij == null && ie != null) {
614 myprintln "- No JOSM icon: ${getDescription(j)}"
615 } else if(!ij.equals(ie)) {
616 myprintln "* Different icons: ${getDescription(j)}"
617 }
618 }
619 myprintln "*** Miscellaneous checks: ***"
620 def josmIds = new HashMap<String, ImageryInfo>()
621 for (def url : josmUrls.keySet()) {
622 def j = josmUrls.get(url)
623 def id = getId(j)
624 if(josmMirrors.containsKey(url)) {
[11964]625 continue
[11420]626 }
627 if(id == null) {
628 myprintln "* No JOSM-ID: ${getDescription(j)}"
629 } else if(josmIds.containsKey(id)) {
630 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
631 } else {
[11964]632 josmIds.put(id, j)
[11420]633 }
[11572]634 def d = getDate(j)
[11573]635 if(!d.isEmpty()) {
[11639]636 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
[11572]637 if(reg == null || reg.count != 1) {
638 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
639 } else {
640 try {
[11964]641 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
642 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
[11572]643 if(second.compareTo(first) < 0) {
644 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
645 }
646 }
647 catch (Exception e) {
648 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
649 }
650 }
[11603]651 }
[11422]652 def js = getShapes(j)
653 if(js.size()) {
[11964]654 def minlat = 1000
655 def minlon = 1000
656 def maxlat = -1000
657 def maxlon = -1000
[11422]658 for(def s: js) {
659 for(def p: s.getPoints()) {
[11964]660 def lat = p.getLat()
661 def lon = p.getLon()
662 if(lat > maxlat) maxlat = lat
663 if(lon > maxlon) maxlon = lon
664 if(lat < minlat) minlat = lat
665 if(lon < minlon) minlon = lon
[11422]666 }
667 }
[11964]668 def b = j.getBounds()
[11422]669 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
[11423]670 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]671 }
672 }
[11420]673 }
[7726]674 }
[9667]675
[7726]676 /**
677 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
678 */
679 static String getUrl(Object e) {
680 if (e instanceof ImageryInfo) return e.url
[11412]681 return e.get("properties").getString("url")
[7726]682 }
[11572]683 static String getDate(Object e) {
[11573]684 if (e instanceof ImageryInfo) return e.date ? e.date : ""
685 def p = e.get("properties")
686 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
687 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
688 if(!start.isEmpty() && !end.isEmpty())
[11572]689 return start+";"+end
[11573]690 else if(!start.isEmpty())
[11612]691 return start+";-"
692 else if(!end.isEmpty())
693 return "-;"+end
[11964]694 return ""
[11572]695 }
696 static Date verifyDate(String year, String month, String day) {
697 def date
[11854]698 if(year == null) {
[11572]699 date = "3000-01-01"
[11854]700 } else {
[11572]701 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
[11854]702 }
[11572]703 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
704 df.setLenient(false)
705 return df.parse(date)
706 }
[11420]707 static String getId(Object e) {
708 if (e instanceof ImageryInfo) return e.getId()
709 return e.get("properties").getString("id")
710 }
[7726]711 static String getName(Object e) {
[10517]712 if (e instanceof ImageryInfo) return e.getOriginalName()
[11412]713 return e.get("properties").getString("name")
[7726]714 }
[11967]715 static List<Object> getMirrors(Object e) {
716 if (e instanceof ImageryInfo) return e.getMirrors()
717 return []
718 }
[11975]719 static List<Object> getProjections(Object e) {
720 def r
721 if (e instanceof ImageryInfo) {
722 r = e.getServerProjections()
723 } else {
[11981]724 def s = e.get("properties").get("available_projections")
725 if (s) {
726 r = []
727 for (def p : s)
728 r += p.getString()
729 }
[11975]730 }
731 return r ? r : []
732 }
[11410]733 static List<Shape> getShapes(Object e) {
734 if (e instanceof ImageryInfo) {
[11964]735 def bounds = e.getBounds()
[11411]736 if(bounds != null) {
[11964]737 return bounds.getShapes()
[11411]738 }
739 return []
[11410]740 }
[11412]741 if(!e.isNull("geometry")) {
742 def ex = e.get("geometry")
743 if(ex != null && !ex.isNull("coordinates")) {
744 def poly = ex.get("coordinates")
[11410]745 List<Shape> l = []
746 for(def shapes: poly) {
747 def s = new Shape()
748 for(def point: shapes) {
749 def lon = point[0].toString()
750 def lat = point[1].toString()
751 s.addPoint(lat, lon)
752 }
753 l.add(s)
754 }
755 return l
756 }
757 }
758 return []
759 }
[7726]760 static String getType(Object e) {
761 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
[11412]762 return e.get("properties").getString("type")
[7726]763 }
764 static Integer getMinZoom(Object e) {
765 if (e instanceof ImageryInfo) {
[12007]766 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
767 return null;
[7726]768 int mz = e.getMinZoom()
769 return mz == 0 ? null : mz
770 } else {
[11412]771 def num = e.get("properties").getJsonNumber("min_zoom")
[7726]772 if (num == null) return null
773 return num.intValue()
774 }
775 }
776 static Integer getMaxZoom(Object e) {
777 if (e instanceof ImageryInfo) {
[12007]778 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
779 return null;
[7726]780 int mz = e.getMaxZoom()
781 return mz == 0 ? null : mz
782 } else {
[11412]783 def num = e.get("properties").getJsonNumber("max_zoom")
[7726]784 if (num == null) return null
785 return num.intValue()
786 }
787 }
788 static String getCountryCode(Object e) {
789 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
[11412]790 return e.get("properties").getString("country_code", null)
[7726]791 }
[9505]792 static String getQuality(Object e) {
[11575]793 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
[11603]794 return (e.get("properties").containsKey("best")
795 && e.get("properties").getBoolean("best")) ? "eli-best" : null
[9505]796 }
[11420]797 static String getIcon(Object e) {
798 if (e instanceof ImageryInfo) return e.getIcon()
799 return e.get("properties").getString("icon", null)
800 }
[11975]801 static String getAttributionText(Object e) {
802 if (e instanceof ImageryInfo) return e.getAttributionText(0, null, null)
803 try {return e.get("properties").get("attribution").getString("text", null)} catch (NullPointerException ex) {return null}
804 }
805 static String getAttributionUrl(Object e) {
806 if (e instanceof ImageryInfo) return e.getAttributionLinkURL()
807 try {return e.get("properties").get("attribution").getString("url", null)} catch (NullPointerException ex) {return null}
808 }
809 static String getTermsOfUseText(Object e) {
810 if (e instanceof ImageryInfo) return e.getTermsOfUseText()
811 return null
812 }
813 static String getTermsOfUseUrl(Object e) {
814 if (e instanceof ImageryInfo) return e.getTermsOfUseURL()
815 return null
816 }
817 static String getPermissionReferenceUrl(Object e) {
818 if (e instanceof ImageryInfo) return e.getPermissionReferenceURL()
819 return e.get("properties").getString("license_url", null)
820 }
821 static Map<String,String> getDescriptions(Object e) {
822 Map<String,String> res = new HashMap<String, String>()
823 if (e instanceof ImageryInfo) {
824 String a = e.getDescription()
825 if (a) res.put("en", a)
826 } else {
827 String a = e.get("properties").getString("description", null)
828 if (a) res.put("en", a)
829 }
830 return res
831 }
832 static Boolean getValidGeoreference(Object e) {
833 if (e instanceof ImageryInfo) return e.isGeoreferenceValid()
834 return false
835 }
[7726]836 String getDescription(Object o) {
837 def url = getUrl(o)
838 def cc = getCountryCode(o)
839 if (cc == null) {
840 def j = josmUrls.get(url)
841 if (j != null) cc = getCountryCode(j)
842 if (cc == null) {
[11582]843 def e = eliUrls.get(url)
[7726]844 if (e != null) cc = getCountryCode(e)
845 }
846 }
847 if (cc == null) {
848 cc = ''
849 } else {
850 cc = "[$cc] "
851 }
852 def d = cc + getName(o) + " - " + getUrl(o)
853 if (options.shorten) {
854 def MAXLEN = 140
855 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
856 }
857 return d
858 }
859}
Note: See TracBrowser for help on using the repository browser.