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

Last change on this file since 13476 was 13476, checked in by Klumbumbus, 6 years ago

fix #16021 - fix imagery sync script

  • Property svn:eol-style set to native
File size: 36.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) {
[12247]157 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ ELI")) ? "blue" : (s.startsWith("#") ? "indigo" : "red"))
[9658]158 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9505]159 }
[12246]160 if ((s.startsWith("+ ") || s.startsWith("+++ ELI") || s.startsWith("#")) && options.noeli) {
[11965]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) {
[12242]189 def url = getUrlStripped(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"
[13474]214 if(getMinZoom(entry) != null)
215 res += offset + "<min-zoom>${getMinZoom(entry)}</min-zoom>\n"
216 if(getMaxZoom(entry) != null)
217 res += offset + "<max-zoom>${getMaxZoom(entry)}</max-zoom>\n"
[13476]218 if (t == "wms") {
[11975]219 def p = getProjections(entry)
220 if (p) {
221 res += offset + "<projections>\n"
222 for (def c : p)
223 res += offset + " <code>$c</code>\n"
224 res += offset + "</projections>\n"
225 }
[11967]226 }
227 return res
228 }
[12061]229
[11964]230 void printentries(def entries, def stream) {
[11967]231 DecimalFormat df = new DecimalFormat("#.#######")
232 df.setRoundingMode(java.math.RoundingMode.CEILING)
[11964]233 stream.write "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
234 stream.write "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n"
235 for (def e : entries) {
236 def best = "eli-best".equals(getQuality(e))
237 stream.write " <entry"+(best ? " eli-best=\"true\"" : "" )+">\n"
[11975]238 stream.write " <name>${cdata(getName(e), true)}</name>\n"
[11964]239 stream.write " <id>${getId(e)}</id>\n"
[11968]240 def t
241 if((t = getDate(e)))
242 stream.write " <date>$t</date>\n"
243 if((t = getCountryCode(e)))
244 stream.write " <country-code>$t</country-code>\n"
[12226]245 if((getDefault(e)))
246 stream.write " <default>true</default>\n"
[11975]247 stream.write maininfo(e, " ")
248 if((t = getAttributionText(e)))
249 stream.write " <attribution-text mandatory=\"true\">${cdata(t, true)}</attribution-text>\n"
250 if((t = getAttributionUrl(e)))
251 stream.write " <attribution-url>${cdata(t)}</attribution-url>\n"
[12261]252 if((t = getLogoImage(e)))
253 stream.write " <logo-image>${cdata(t, true)}</logo-image>\n"
254 if((t = getLogoUrl(e)))
255 stream.write " <logo-url>${cdata(t)}</logo-url>\n"
[11975]256 if((t = getTermsOfUseText(e)))
257 stream.write " <terms-of-use-text>${cdata(t, true)}</terms-of-use-text>\n"
258 if((t = getTermsOfUseUrl(e)))
259 stream.write " <terms-of-use-url>${cdata(t)}</terms-of-use-url>\n"
260 if((t = getPermissionReferenceUrl(e)))
261 stream.write " <permission-ref>${cdata(t)}</permission-ref>\n"
262 if((getValidGeoreference(e)))
263 stream.write " <valid-georeference>true</valid-georeference>\n"
[11968]264 if((t = getIcon(e)))
265 stream.write " <icon>${cdata(t)}</icon>\n"
[11975]266 for (def d : getDescriptions(e)) {
267 stream.write " <description lang=\"${d.getKey()}\">${d.getValue()}</description>\n"
268 }
[11967]269 for (def m : getMirrors(e)) {
270 stream.write " <mirror>\n"+maininfo(m, " ")+" </mirror>\n"
271 }
[11964]272 def minlat = 1000
273 def minlon = 1000
274 def maxlat = -1000
275 def maxlon = -1000
276 def shapes = ""
277 def sep = "\n "
278 for(def s: getShapes(e)) {
279 shapes += " <shape>"
280 def i = 0
281 for(def p: s.getPoints()) {
282 def lat = p.getLat()
283 def lon = p.getLon()
284 if(lat > maxlat) maxlat = lat
285 if(lon > maxlon) maxlon = lon
286 if(lat < minlat) minlat = lat
287 if(lon < minlon) minlon = lon
288 if(!(i++%3)) {
289 shapes += sep + " "
290 }
[11967]291 shapes += "<point lat='${df.format(lat)}' lon='${df.format(lon)}'/>"
[11964]292 }
293 shapes += sep + "</shape>\n"
294 }
295 if(shapes) {
[11967]296 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]297 stream.write shapes + " </bounds>\n"
298 }
[11967]299 stream.write " </entry>\n"
[11964]300 }
301 stream.write "</imagery>\n"
302 stream.close()
303 }
304
[7726]305 void loadJosmEntries() {
306 def reader = new ImageryReader(josmInputFile)
307 josmEntries = reader.parse()
[9667]308
[7726]309 for (def e : josmEntries) {
[12242]310 def url = getUrlStripped(e)
[9658]311 if (url.contains("{z}")) {
312 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
313 url = url.replace("{z}","{zoom}")
314 }
[9505]315 if (josmUrls.containsKey(url)) {
316 myprintln "+++ JOSM-URL is not unique: "+url
317 } else {
[11420]318 josmUrls.put(url, e)
[7726]319 }
[9658]320 for (def m : e.getMirrors()) {
[12242]321 url = getUrlStripped(m)
[11574]322 m.origName = m.getOriginalName().replaceAll(" mirror server( \\d+)?","")
[9658]323 if (josmUrls.containsKey(url)) {
324 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
325 } else {
[11420]326 josmUrls.put(url, m)
327 josmMirrors.put(url, m)
[9658]328 }
329 }
[7726]330 }
[9505]331 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
[7726]332 }
333
334 List inOneButNotTheOther(Map m1, Map m2) {
335 def l = []
336 for (def url : m1.keySet()) {
337 if (!m2.containsKey(url)) {
338 def name = getName(m1.get(url))
339 l += " "+getDescription(m1.get(url))
340 }
341 }
342 l.sort()
343 }
[9667]344
[7726]345 void checkInOneButNotTheOther() {
[11582]346 def l1 = inOneButNotTheOther(eliUrls, josmUrls)
347 myprintln "*** URLs found in ELI but not in JOSM (${l1.size()}): ***"
[9658]348 if (!l1.isEmpty()) {
[11412]349 for (def l : l1) {
350 myprintln "-" + l
351 }
[7726]352 }
353
[11582]354 def l2 = inOneButNotTheOther(josmUrls, eliUrls)
355 myprintln "*** URLs found in JOSM but not in ELI (${l2.size()}): ***"
[9658]356 if (!l2.isEmpty()) {
[11412]357 for (def l : l2) {
[9658]358 myprintln "+" + l
[11412]359 }
[7726]360 }
361 }
[9667]362
[7726]363 void checkCommonEntries() {
[9505]364 myprintln "*** Same URL, but different name: ***"
[11582]365 for (def url : eliUrls.keySet()) {
366 def e = eliUrls.get(url)
[7726]367 if (!josmUrls.containsKey(url)) continue
368 def j = josmUrls.get(url)
[12061]369 def ename = getName(e).replace("'","\u2019")
370 def jname = getName(j).replace("'","\u2019")
[11951]371 if (!ename.equals(jname)) {
[12242]372 myprintln "* Name differs ('${getName(e)}' != '${getName(j)}'): ${getUrl(j)}"
[7726]373 }
374 }
[9667]375
[12226]376 myprintln "*** Same URL, but different Id: ***"
[12126]377 for (def url : eliUrls.keySet()) {
378 def e = eliUrls.get(url)
379 if (!josmUrls.containsKey(url)) continue
380 def j = josmUrls.get(url)
381 def ename = getId(e)
382 def jname = getId(j)
383 if (!ename.equals(jname)) {
[12245]384 myprintln "# Id differs ('${getId(e)}' != '${getId(j)}'): ${getUrl(j)}"
[12126]385 }
[12226]386 }
[12126]387
[9505]388 myprintln "*** Same URL, but different type: ***"
[11582]389 for (def url : eliUrls.keySet()) {
390 def e = eliUrls.get(url)
[7726]391 if (!josmUrls.containsKey(url)) continue
392 def j = josmUrls.get(url)
393 if (!getType(e).equals(getType(j))) {
[12242]394 myprintln "* Type differs (${getType(e)} != ${getType(j)}): ${getName(j)} - ${getUrl(j)}"
[7726]395 }
396 }
[9667]397
[9505]398 myprintln "*** Same URL, but different zoom bounds: ***"
[11582]399 for (def url : eliUrls.keySet()) {
400 def e = eliUrls.get(url)
[7726]401 if (!josmUrls.containsKey(url)) continue
402 def j = josmUrls.get(url)
403
404 Integer eMinZoom = getMinZoom(e)
405 Integer jMinZoom = getMinZoom(j)
[9518]406 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
[11582]407 myprintln "* Minzoom differs (${eMinZoom} != ${jMinZoom}): ${getDescription(j)}"
[7726]408 }
409 Integer eMaxZoom = getMaxZoom(e)
410 Integer jMaxZoom = getMaxZoom(j)
411 if (eMaxZoom != jMaxZoom) {
[11582]412 myprintln "* Maxzoom differs (${eMaxZoom} != ${jMaxZoom}): ${getDescription(j)}"
[7726]413 }
414 }
[9667]415
[9505]416 myprintln "*** Same URL, but different country code: ***"
[11582]417 for (def url : eliUrls.keySet()) {
418 def e = eliUrls.get(url)
[7726]419 if (!josmUrls.containsKey(url)) continue
420 def j = josmUrls.get(url)
421 if (!getCountryCode(e).equals(getCountryCode(j))) {
[11582]422 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
[7726]423 }
424 }
[11599]425 myprintln "*** Same URL, but different quality: ***"
[11582]426 for (def url : eliUrls.keySet()) {
427 def e = eliUrls.get(url)
[9515]428 if (!josmUrls.containsKey(url)) {
429 def q = getQuality(e)
[11582]430 if("eli-best".equals(q)) {
431 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
[9515]432 }
433 continue
434 }
[9505]435 def j = josmUrls.get(url)
436 if (!getQuality(e).equals(getQuality(j))) {
[11582]437 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
[9505]438 }
[11599]439 }
[11665]440 myprintln "*** Same URL, but different dates: ***"
[11582]441 for (def url : eliUrls.keySet()) {
[11612]442 def ed = getDate(eliUrls.get(url))
[11573]443 if (!josmUrls.containsKey(url)) continue
444 def j = josmUrls.get(url)
[11612]445 def jd = getDate(j)
446 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
[11964]447 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
[11639]448 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
[11964]449 String ed2 = ed
[11639]450 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
451 if(reg != null && reg.count == 1) {
[11964]452 Calendar cal = Calendar.getInstance()
[11639]453 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)
454 cal.add(Calendar.DAY_OF_MONTH, -1)
[11667]455 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
[11639]456 if (reg[0][4] != null)
457 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
458 if (reg[0][6] != null)
459 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
460 }
[11964]461 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
[11639]462 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
[11964]463 String t = "'${ed}'"
[11612]464 if (!ed.equals(ef)) {
[11964]465 t += " or '${ef}'"
[11612]466 }
[11666]467 if (jd.isEmpty()) {
468 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
[11668]469 } else if (!ed.isEmpty()) {
[12145]470 myprintln "* Date differs ('${t}' != '${jd}'): ${getDescription(j)}"
[11668]471 } else if (!options.nomissingeli) {
[11666]472 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
473 }
[11573]474 }
[11665]475 }
[11981]476 myprintln "*** Same URL, but different information: ***"
477 for (def url : eliUrls.keySet()) {
478 if (!josmUrls.containsKey(url)) continue
479 def e = eliUrls.get(url)
480 def j = josmUrls.get(url)
481
482 def et = getDescriptions(e)
483 def jt = getDescriptions(j)
[12008]484 et = (et.size() > 0) ? et["en"] : ""
485 jt = (jt.size() > 0) ? jt["en"] : ""
[12086]486 if (!et.equals(jt)) {
[11981]487 if (!jt) {
[12179]488 myprintln "- Missing JOSM description (${et}): ${getDescription(j)}"
[11981]489 } else if (et) {
[12145]490 myprintln "* Description differs ('${et}' != '${jt}'): ${getDescription(j)}"
[11981]491 } else if (!options.nomissingeli) {
492 myprintln "+ Missing ELI description ('${jt}'): ${getDescription(j)}"
493 }
494 }
495
496 et = getPermissionReferenceUrl(e)
497 jt = getPermissionReferenceUrl(j)
498 if (!jt) jt = getTermsOfUseUrl(j)
499 if (!et.equals(jt)) {
500 if (!jt) {
[12266]501 myprintln "- Missing JOSM license URL (${et}): ${getDescription(j)}"
[11981]502 } else if (et) {
[12266]503 myprintln "* License URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
[11981]504 } else if (!options.nomissingeli) {
505 myprintln "+ Missing ELI license URL ('${jt}'): ${getDescription(j)}"
506 }
507 }
508
509 et = getAttributionUrl(e)
510 jt = getAttributionUrl(j)
511 if (!et.equals(jt)) {
512 if (!jt) {
[12143]513 myprintln "- Missing JOSM attribution URL (${et}): ${getDescription(j)}"
[11981]514 } else if (et) {
[12266]515 myprintln "* Attribution URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
[11981]516 } else if (!options.nomissingeli) {
517 myprintln "+ Missing ELI attribution URL ('${jt}'): ${getDescription(j)}"
518 }
519 }
520
521 et = getAttributionText(e)
522 jt = getAttributionText(j)
523 if (!et.equals(jt)) {
524 if (!jt) {
[12143]525 myprintln "- Missing JOSM attribution text (${et}): ${getDescription(j)}"
[11981]526 } else if (et) {
[12266]527 myprintln "* Attribution text differs ('${et}' != '${jt}'): ${getDescription(j)}"
[11981]528 } else if (!options.nomissingeli) {
529 myprintln "+ Missing ELI attribution text ('${jt}'): ${getDescription(j)}"
530 }
531 }
532
533 et = getProjections(e)
534 jt = getProjections(j)
[11983]535 if (et) { et = new LinkedList(et); Collections.sort(et); et = String.join(" ", et) }
536 if (jt) { jt = new LinkedList(jt); Collections.sort(jt); jt = String.join(" ", jt) }
[11981]537 if (!et.equals(jt)) {
538 if (!jt) {
[12143]539 myprintln "- Missing JOSM projections (${et}): ${getDescription(j)}"
[11981]540 } else if (et) {
[12179]541 myprintln "* Projections differ ('${et}' != '${jt}'): ${getDescription(j)}"
[12144]542 } else if (!options.nomissingeli && !getType(e).equals("tms")) {
[11981]543 myprintln "+ Missing ELI projections ('${jt}'): ${getDescription(j)}"
544 }
545 }
[12226]546
547 et = getDefault(e)
548 jt = getDefault(j)
549 if (!et.equals(jt)) {
550 if (!jt) {
[12227]551 myprintln "- Missing JOSM default: ${getDescription(j)}"
[12226]552 } else if (!options.nomissingeli) {
553 myprintln "+ Missing ELI default: ${getDescription(j)}"
554 }
555 }
[11981]556 }
[11410]557 myprintln "*** Mismatching shapes: ***"
558 for (def url : josmUrls.keySet()) {
559 def j = josmUrls.get(url)
560 def num = 1
561 for (def shape : getShapes(j)) {
562 def p = shape.getPoints()
563 if(!p[0].equals(p[p.size()-1])) {
564 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
565 }
[11964]566 for (def nump = 1; nump < p.size(); ++nump) {
567 if (p[nump-1] == p[nump]) {
568 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
569 }
570 }
[11410]571 ++num
572 }
573 }
[11582]574 for (def url : eliUrls.keySet()) {
575 def e = eliUrls.get(url)
[11410]576 def num = 1
577 def s = getShapes(e)
578 for (def shape : s) {
579 def p = shape.getPoints()
[11582]580 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
581 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
[11410]582 }
[11964]583 for (def nump = 1; nump < p.size(); ++nump) {
584 if (p[nump-1] == p[nump]) {
585 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
586 }
587 }
[11410]588 ++num
589 }
590 if (!josmUrls.containsKey(url)) {
591 continue
592 }
593 def j = josmUrls.get(url)
[11411]594 def js = getShapes(j)
[11414]595 if(!s.size() && js.size()) {
[11582]596 if(!options.nomissingeli) {
597 myprintln "+ No ELI shape: ${getDescription(j)}"
[11414]598 }
[11413]599 } else if(!js.size() && s.size()) {
[11415]600 // don't report boundary like 5 point shapes as difference
601 if (s.size() != 1 || s[0].getPoints().size() != 5) {
602 myprintln "- No JOSM shape: ${getDescription(j)}"
603 }
[11414]604 } else if(s.size() != js.size()) {
605 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
[11413]606 } else {
[11414]607 for(def nums = 0; nums < s.size(); ++nums) {
608 def ep = s[nums].getPoints()
609 def jp = js[nums].getPoints()
610 if(ep.size() != jp.size()) {
611 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
612 } else {
613 for(def nump = 0; nump < ep.size(); ++nump) {
614 def ept = ep[nump]
615 def jpt = jp[nump]
616 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
617 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
618 nump = ep.size()
619 num = s.size()
[11413]620 }
621 }
622 }
[11411]623 }
[11410]624 }
625 }
[11420]626 myprintln "*** Mismatching icons: ***"
[11582]627 for (def url : eliUrls.keySet()) {
628 def e = eliUrls.get(url)
[11420]629 if (!josmUrls.containsKey(url)) {
630 continue
631 }
632 def j = josmUrls.get(url)
633 def ij = getIcon(j)
634 def ie = getIcon(e)
635 if(ij != null && ie == null) {
[11582]636 if(!options.nomissingeli) {
637 myprintln "+ No ELI icon: ${getDescription(j)}"
[11420]638 }
639 } else if(ij == null && ie != null) {
640 myprintln "- No JOSM icon: ${getDescription(j)}"
641 } else if(!ij.equals(ie)) {
642 myprintln "* Different icons: ${getDescription(j)}"
643 }
644 }
645 myprintln "*** Miscellaneous checks: ***"
646 def josmIds = new HashMap<String, ImageryInfo>()
647 for (def url : josmUrls.keySet()) {
648 def j = josmUrls.get(url)
649 def id = getId(j)
650 if(josmMirrors.containsKey(url)) {
[11964]651 continue
[11420]652 }
653 if(id == null) {
654 myprintln "* No JOSM-ID: ${getDescription(j)}"
655 } else if(josmIds.containsKey(id)) {
656 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
657 } else {
[11964]658 josmIds.put(id, j)
[11420]659 }
[11572]660 def d = getDate(j)
[11573]661 if(!d.isEmpty()) {
[11639]662 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
[11572]663 if(reg == null || reg.count != 1) {
664 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
665 } else {
666 try {
[11964]667 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
668 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
[11572]669 if(second.compareTo(first) < 0) {
670 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
671 }
672 }
673 catch (Exception e) {
674 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
675 }
676 }
[11603]677 }
[12261]678 if(getAttributionUrl(j) && !getAttributionText(j)) {
679 myprintln "* Attribution link without text: ${getDescription(j)}"
680 }
681 if(getLogoUrl(j) && !getLogoImage(j)) {
682 myprintln "* Logo link without image: ${getDescription(j)}"
683 }
684 if(getTermsOfUseText(j) && !getTermsOfUseUrl(j)) {
685 myprintln "* Terms of Use text without link: ${getDescription(j)}"
686 }
[11422]687 def js = getShapes(j)
688 if(js.size()) {
[11964]689 def minlat = 1000
690 def minlon = 1000
691 def maxlat = -1000
692 def maxlon = -1000
[11422]693 for(def s: js) {
694 for(def p: s.getPoints()) {
[11964]695 def lat = p.getLat()
696 def lon = p.getLon()
697 if(lat > maxlat) maxlat = lat
698 if(lon > maxlon) maxlon = lon
699 if(lat < minlat) minlat = lat
700 if(lon < minlon) minlon = lon
[11422]701 }
702 }
[11964]703 def b = j.getBounds()
[11422]704 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
[11423]705 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]706 }
707 }
[11420]708 }
[7726]709 }
[9667]710
[7726]711 /**
712 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
713 */
714 static String getUrl(Object e) {
715 if (e instanceof ImageryInfo) return e.url
[11412]716 return e.get("properties").getString("url")
[7726]717 }
[12242]718 static String getUrlStripped(Object e) {
719 return getUrl(e).replaceAll("\\?(apikey|access_token)=.*","")
720 }
[11572]721 static String getDate(Object e) {
[11573]722 if (e instanceof ImageryInfo) return e.date ? e.date : ""
723 def p = e.get("properties")
724 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
725 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
726 if(!start.isEmpty() && !end.isEmpty())
[11572]727 return start+";"+end
[11573]728 else if(!start.isEmpty())
[11612]729 return start+";-"
730 else if(!end.isEmpty())
731 return "-;"+end
[11964]732 return ""
[11572]733 }
734 static Date verifyDate(String year, String month, String day) {
735 def date
[11854]736 if(year == null) {
[11572]737 date = "3000-01-01"
[11854]738 } else {
[11572]739 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
[11854]740 }
[11572]741 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
742 df.setLenient(false)
743 return df.parse(date)
744 }
[11420]745 static String getId(Object e) {
746 if (e instanceof ImageryInfo) return e.getId()
747 return e.get("properties").getString("id")
748 }
[7726]749 static String getName(Object e) {
[10517]750 if (e instanceof ImageryInfo) return e.getOriginalName()
[11412]751 return e.get("properties").getString("name")
[7726]752 }
[11967]753 static List<Object> getMirrors(Object e) {
754 if (e instanceof ImageryInfo) return e.getMirrors()
755 return []
756 }
[11975]757 static List<Object> getProjections(Object e) {
758 def r
759 if (e instanceof ImageryInfo) {
760 r = e.getServerProjections()
761 } else {
[11981]762 def s = e.get("properties").get("available_projections")
763 if (s) {
764 r = []
765 for (def p : s)
766 r += p.getString()
767 }
[11975]768 }
769 return r ? r : []
770 }
[11410]771 static List<Shape> getShapes(Object e) {
772 if (e instanceof ImageryInfo) {
[11964]773 def bounds = e.getBounds()
[11411]774 if(bounds != null) {
[11964]775 return bounds.getShapes()
[11411]776 }
777 return []
[11410]778 }
[11412]779 if(!e.isNull("geometry")) {
780 def ex = e.get("geometry")
781 if(ex != null && !ex.isNull("coordinates")) {
782 def poly = ex.get("coordinates")
[11410]783 List<Shape> l = []
784 for(def shapes: poly) {
785 def s = new Shape()
786 for(def point: shapes) {
787 def lon = point[0].toString()
788 def lat = point[1].toString()
789 s.addPoint(lat, lon)
790 }
791 l.add(s)
792 }
793 return l
794 }
795 }
796 return []
797 }
[7726]798 static String getType(Object e) {
799 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
[11412]800 return e.get("properties").getString("type")
[7726]801 }
802 static Integer getMinZoom(Object e) {
803 if (e instanceof ImageryInfo) {
[12007]804 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
805 return null;
[7726]806 int mz = e.getMinZoom()
807 return mz == 0 ? null : mz
808 } else {
[11412]809 def num = e.get("properties").getJsonNumber("min_zoom")
[7726]810 if (num == null) return null
811 return num.intValue()
812 }
813 }
814 static Integer getMaxZoom(Object e) {
815 if (e instanceof ImageryInfo) {
[12007]816 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
817 return null;
[7726]818 int mz = e.getMaxZoom()
819 return mz == 0 ? null : mz
820 } else {
[11412]821 def num = e.get("properties").getJsonNumber("max_zoom")
[7726]822 if (num == null) return null
823 return num.intValue()
824 }
825 }
826 static String getCountryCode(Object e) {
827 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
[11412]828 return e.get("properties").getString("country_code", null)
[7726]829 }
[9505]830 static String getQuality(Object e) {
[11575]831 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
[11603]832 return (e.get("properties").containsKey("best")
833 && e.get("properties").getBoolean("best")) ? "eli-best" : null
[9505]834 }
[11420]835 static String getIcon(Object e) {
836 if (e instanceof ImageryInfo) return e.getIcon()
837 return e.get("properties").getString("icon", null)
838 }
[11975]839 static String getAttributionText(Object e) {
840 if (e instanceof ImageryInfo) return e.getAttributionText(0, null, null)
841 try {return e.get("properties").get("attribution").getString("text", null)} catch (NullPointerException ex) {return null}
842 }
843 static String getAttributionUrl(Object e) {
844 if (e instanceof ImageryInfo) return e.getAttributionLinkURL()
845 try {return e.get("properties").get("attribution").getString("url", null)} catch (NullPointerException ex) {return null}
846 }
847 static String getTermsOfUseText(Object e) {
848 if (e instanceof ImageryInfo) return e.getTermsOfUseText()
849 return null
850 }
851 static String getTermsOfUseUrl(Object e) {
852 if (e instanceof ImageryInfo) return e.getTermsOfUseURL()
853 return null
854 }
[12261]855 static String getLogoImage(Object e) {
856 if (e instanceof ImageryInfo) return e.getAttributionImageRaw()
857 return null
858 }
859 static String getLogoUrl(Object e) {
860 if (e instanceof ImageryInfo) return e.getAttributionImageURL()
861 return null
862 }
[11975]863 static String getPermissionReferenceUrl(Object e) {
864 if (e instanceof ImageryInfo) return e.getPermissionReferenceURL()
865 return e.get("properties").getString("license_url", null)
866 }
867 static Map<String,String> getDescriptions(Object e) {
868 Map<String,String> res = new HashMap<String, String>()
869 if (e instanceof ImageryInfo) {
870 String a = e.getDescription()
871 if (a) res.put("en", a)
872 } else {
873 String a = e.get("properties").getString("description", null)
874 if (a) res.put("en", a)
875 }
876 return res
877 }
878 static Boolean getValidGeoreference(Object e) {
879 if (e instanceof ImageryInfo) return e.isGeoreferenceValid()
880 return false
881 }
[12226]882 static Boolean getDefault(Object e) {
883 if (e instanceof ImageryInfo) return e.isDefaultEntry()
884 return e.get("properties").getBoolean("default", false)
885 }
[7726]886 String getDescription(Object o) {
887 def url = getUrl(o)
888 def cc = getCountryCode(o)
889 if (cc == null) {
890 def j = josmUrls.get(url)
891 if (j != null) cc = getCountryCode(j)
892 if (cc == null) {
[11582]893 def e = eliUrls.get(url)
[7726]894 if (e != null) cc = getCountryCode(e)
895 }
896 }
897 if (cc == null) {
898 cc = ''
899 } else {
900 cc = "[$cc] "
901 }
902 def d = cc + getName(o) + " - " + getUrl(o)
903 if (options.shorten) {
904 def MAXLEN = 140
905 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
906 }
907 return d
908 }
909}
Note: See TracBrowser for help on using the repository browser.