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

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

try another color

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