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

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

better encoding support, see #14655 - don't do OS detection, that's probably done automatically

  • Property svn:eol-style set to native
File size: 45.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2/**
3 * Compare and analyse the differences of the editor layer index and the JOSM imagery list.
4 * The goal is to keep both lists in sync.
5 *
6 * The editor layer index project (https://github.com/osmlab/editor-layer-index)
7 * provides also a version in the JOSM format, but the GEOJSON is the original source
8 * format, so we read that.
9 *
10 * How to run:
11 * -----------
12 *
13 * Main JOSM binary needs to be in classpath, e.g.
14 *
15 * $ groovy -cp ../dist/josm-custom.jar SyncEditorLayerIndex.groovy
16 *
17 * Add option "-h" to show the available command line flags.
18 */
19import java.text.DecimalFormat
20import javax.json.Json
21import javax.json.JsonArray
22import javax.json.JsonObject
23import javax.json.JsonReader
24
25import org.openstreetmap.josm.data.imagery.ImageryInfo
26import org.openstreetmap.josm.data.imagery.Shape
27import org.openstreetmap.josm.data.projection.Projections
28import org.openstreetmap.josm.data.validation.routines.DomainValidator
29import org.openstreetmap.josm.io.imagery.ImageryReader
30
31class SyncEditorLayerIndex {
32
33 List<ImageryInfo> josmEntries;
34 JsonArray eliEntries;
35
36 def eliUrls = new HashMap<String, JsonObject>()
37 def josmUrls = new HashMap<String, ImageryInfo>()
38 def josmMirrors = new HashMap<String, ImageryInfo>()
39 static def oldproj = new HashMap<String, String>()
40 static def ignoreproj = new LinkedList<String>()
41
42 static String eliInputFile = 'imagery_eli.geojson'
43 static String josmInputFile = 'imagery_josm.imagery.xml'
44 static String ignoreInputFile = 'imagery_josm.ignores.txt'
45 static FileOutputStream outputFile = null
46 static OutputStreamWriter outputStream = null
47 def skip = [:]
48
49 static def options
50
51 /**
52 * Main method.
53 */
54 static main(def args) {
55 Locale.setDefault(Locale.ROOT);
56 parse_command_line_arguments(args)
57 def script = new SyncEditorLayerIndex()
58 script.setupProj()
59 script.loadSkip()
60 script.start()
61 script.loadJosmEntries()
62 if(options.josmxml) {
63 def file = new FileOutputStream(options.josmxml)
64 def stream = new OutputStreamWriter(file, "UTF-8")
65 script.printentries(script.josmEntries, stream)
66 stream.close();
67 file.close();
68 }
69 script.loadELIEntries()
70 if(options.elixml) {
71 def file = new FileOutputStream(options.elixml)
72 def stream = new OutputStreamWriter(file, "UTF-8")
73 script.printentries(script.eliEntries, stream)
74 stream.close();
75 file.close();
76 }
77 script.checkInOneButNotTheOther()
78 script.checkCommonEntries()
79 script.end()
80 if(outputStream != null) {
81 outputStream.close();
82 }
83 if(outputFile != null) {
84 outputFile.close();
85 }
86 }
87
88 /**
89 * Parse command line arguments.
90 */
91 static void parse_command_line_arguments(args) {
92 def cli = new CliBuilder(width: 160)
93 cli.o(longOpt:'output', args:1, argName: "output", "Output file, - prints to stdout (default: -)")
94 cli.e(longOpt:'eli_input', args:1, argName:"eli_input", "Input file for the editor layer index (geojson). Default is $eliInputFile (current directory).")
95 cli.j(longOpt:'josm_input', args:1, argName:"josm_input", "Input file for the JOSM imagery list (xml). Default is $josmInputFile (current directory).")
96 cli.i(longOpt:'ignore_input', args:1, argName:"ignore_input", "Input file for the ignore list. Default is $ignoreInputFile (current directory).")
97 cli.s(longOpt:'shorten', "shorten the output, so it is easier to read in a console window")
98 cli.n(longOpt:'noskip', argName:"noskip", "don't skip known entries")
99 cli.x(longOpt:'xhtmlbody', argName:"xhtmlbody", "create XHTML body for display in a web page")
100 cli.X(longOpt:'xhtml', argName:"xhtml", "create XHTML for display in a web page")
101 cli.p(longOpt:'elixml', args:1, argName:"elixml", "ELI entries for use in JOSM as XML file (incomplete)")
102 cli.q(longOpt:'josmxml', args:1, argName:"josmxml", "JOSM entries reoutput as XML file (incomplete)")
103 cli.m(longOpt:'noeli', argName:"noeli", "don't show output for ELI problems")
104 cli.c(longOpt:'encoding', args:1, argName:"encoding", "output encoding (defaults to UTF-8 or cp850 on Windows)")
105 cli.h(longOpt:'help', "show this help")
106 options = cli.parse(args)
107
108 if (options.h) {
109 cli.usage()
110 System.exit(0)
111 }
112 if (options.eli_input) {
113 eliInputFile = options.eli_input
114 }
115 if (options.josm_input) {
116 josmInputFile = options.josm_input
117 }
118 if (options.ignore_input) {
119 ignoreInputFile = options.ignore_input
120 }
121 if (options.output && options.output != "-") {
122 String ccp = options.encoding
123 if (ccp == null)
124 ccp = "UTF-8"
125 outputFile = new FileOutputStream(options.output)
126 outputStream = new OutputStreamWriter(outputFile, ccp)
127 } else if (options.encoding) {
128 outputStream = new OutputStreamWriter(System.out, options.encoding)
129 }
130 }
131
132 void setupProj() {
133 oldproj.put("EPSG:3359", "EPSG:3404")
134 oldproj.put("EPSG:3785", "EPSG:3857")
135 oldproj.put("EPSG:31297", "EPGS:31287")
136 oldproj.put("EPSG:31464", "EPSG:31468")
137 oldproj.put("EPSG:54004", "EPSG:3857")
138 oldproj.put("EPSG:102100", "EPSG:3857")
139 oldproj.put("EPSG:102113", "EPSG:3857")
140 oldproj.put("EPSG:900913", "EPGS:3857")
141 ignoreproj.add("EPSG:4267")
142 ignoreproj.add("EPSG:5221")
143 ignoreproj.add("EPSG:5514")
144 ignoreproj.add("EPSG:32019")
145 ignoreproj.add("EPSG:102066")
146 ignoreproj.add("EPSG:102067")
147 ignoreproj.add("EPSG:102685")
148 ignoreproj.add("EPSG:102711")
149 }
150
151 void loadSkip() {
152 def fr = new InputStreamReader(new FileInputStream(ignoreInputFile), "UTF-8")
153 def line
154
155 while((line = fr.readLine()) != null) {
156 def res = (line =~ /^\|\| *(ELI|Ignore) *\|\| *\{\{\{(.+)\}\}\} *\|\|/)
157 if(res.count)
158 {
159 if(res[0][1].equals("Ignore")) {
160 skip[res[0][2]] = "green"
161 } else {
162 skip[res[0][2]] = "darkgoldenrod"
163 }
164 }
165 }
166 }
167
168 void myprintlnfinal(String s) {
169 if(outputStream != null) {
170 outputStream.write(s+System.getProperty("line.separator"))
171 } else {
172 println s
173 }
174 }
175
176 void myprintln(String s) {
177 if(skip.containsKey(s)) {
178 String color = skip.get(s)
179 skip.remove(s)
180 if(options.xhtmlbody || options.xhtml) {
181 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
182 }
183 if (!options.noskip) {
184 return
185 }
186 } else if(options.xhtmlbody || options.xhtml) {
187 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ ELI")) ? "blue" :
188 (s.startsWith("#") ? "indigo" : (s.startsWith("!") ? "orange" : "red")))
189 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
190 }
191 if ((s.startsWith("+ ") || s.startsWith("+++ ELI") || s.startsWith("#")) && options.noeli) {
192 return
193 }
194 myprintlnfinal(s)
195 }
196
197 void start() {
198 if (options.xhtml) {
199 myprintlnfinal "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
200 myprintlnfinal "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/><title>JOSM - ELI differences</title></head><body>\n"
201 }
202 }
203
204 void end() {
205 for (def s: skip.keySet()) {
206 myprintln "+++ Obsolete skip entry: " + s
207 }
208 if (options.xhtml) {
209 myprintlnfinal "</body></html>\n"
210 }
211 }
212
213 void loadELIEntries() {
214 def fr = new InputStreamReader(new FileInputStream(eliInputFile), "UTF-8")
215 JsonReader jr = Json.createReader(fr)
216 eliEntries = jr.readObject().get("features")
217 jr.close()
218
219 for (def e : eliEntries) {
220 def url = getUrlStripped(e)
221 if (url.contains("{z}")) {
222 myprintln "+++ ELI-URL uses {z} instead of {zoom}: "+url
223 url = url.replace("{z}","{zoom}")
224 }
225 if (eliUrls.containsKey(url)) {
226 myprintln "+++ ELI-URL is not unique: "+url
227 } else {
228 eliUrls.put(url, e)
229 }
230 def s = e.get("properties").get("available_projections")
231 if (s) {
232 def old = new LinkedList<String>()
233 for (def p : s) {
234 def proj = p.getString()
235 if(oldproj.containsKey(proj) || ("CRS:84".equals(proj) && !(url =~ /(?i)version=1\.3/))) {
236 old.add(proj)
237 }
238 }
239 if (old) {
240 def str = String.join(", ", old)
241 myprintln "+ ELI Projections ${str} not useful: ${getDescription(e)}"
242 }
243 }
244 }
245 myprintln "*** Loaded ${eliEntries.size()} entries (ELI). ***"
246 }
247 String cdata(def s, boolean escape = false) {
248 if(escape) {
249 return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;")
250 } else if(s =~ /[<>&]/)
251 return "<![CDATA[$s]]>"
252 return s
253 }
254
255 String maininfo(def entry, String offset) {
256 String t = getType(entry)
257 String res = offset + "<type>$t</type>\n"
258 res += offset + "<url>${cdata(getUrl(entry))}</url>\n"
259 if(getMinZoom(entry) != null)
260 res += offset + "<min-zoom>${getMinZoom(entry)}</min-zoom>\n"
261 if(getMaxZoom(entry) != null)
262 res += offset + "<max-zoom>${getMaxZoom(entry)}</max-zoom>\n"
263 if (t == "wms") {
264 def p = getProjections(entry)
265 if (p) {
266 res += offset + "<projections>\n"
267 for (def c : p)
268 res += offset + " <code>$c</code>\n"
269 res += offset + "</projections>\n"
270 }
271 }
272 return res
273 }
274
275 void printentries(def entries, def stream) {
276 DecimalFormat df = new DecimalFormat("#.#######")
277 df.setRoundingMode(java.math.RoundingMode.CEILING)
278 stream.write "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
279 stream.write "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n"
280 for (def e : entries) {
281 stream.write(" <entry"
282 + ("eli-best".equals(getQuality(e)) ? " eli-best=\"true\"" : "" )
283 + (getOverlay(e) ? " overlay=\"true\"" : "" )
284 + ">\n")
285 stream.write " <name>${cdata(getName(e), true)}</name>\n"
286 stream.write " <id>${getId(e)}</id>\n"
287 def t
288 if((t = getDate(e)))
289 stream.write " <date>$t</date>\n"
290 if((t = getCountryCode(e)))
291 stream.write " <country-code>$t</country-code>\n"
292 if((getDefault(e)))
293 stream.write " <default>true</default>\n"
294 stream.write maininfo(e, " ")
295 if((t = getAttributionText(e)))
296 stream.write " <attribution-text mandatory=\"true\">${cdata(t, true)}</attribution-text>\n"
297 if((t = getAttributionUrl(e)))
298 stream.write " <attribution-url>${cdata(t)}</attribution-url>\n"
299 if((t = getLogoImage(e)))
300 stream.write " <logo-image>${cdata(t, true)}</logo-image>\n"
301 if((t = getLogoUrl(e)))
302 stream.write " <logo-url>${cdata(t)}</logo-url>\n"
303 if((t = getTermsOfUseText(e)))
304 stream.write " <terms-of-use-text>${cdata(t, true)}</terms-of-use-text>\n"
305 if((t = getTermsOfUseUrl(e)))
306 stream.write " <terms-of-use-url>${cdata(t)}</terms-of-use-url>\n"
307 if((t = getPermissionReferenceUrl(e)))
308 stream.write " <permission-ref>${cdata(t)}</permission-ref>\n"
309 if((getValidGeoreference(e)))
310 stream.write " <valid-georeference>true</valid-georeference>\n"
311 if((t = getIcon(e)))
312 stream.write " <icon>${cdata(t)}</icon>\n"
313 for (def d : getDescriptions(e)) {
314 stream.write " <description lang=\"${d.getKey()}\">${d.getValue()}</description>\n"
315 }
316 for (def m : getMirrors(e)) {
317 stream.write " <mirror>\n"+maininfo(m, " ")+" </mirror>\n"
318 }
319 def minlat = 1000
320 def minlon = 1000
321 def maxlat = -1000
322 def maxlon = -1000
323 def shapes = ""
324 def sep = "\n "
325 for(def s: getShapes(e)) {
326 shapes += " <shape>"
327 def i = 0
328 for(def p: s.getPoints()) {
329 def lat = p.getLat()
330 def lon = p.getLon()
331 if(lat > maxlat) maxlat = lat
332 if(lon > maxlon) maxlon = lon
333 if(lat < minlat) minlat = lat
334 if(lon < minlon) minlon = lon
335 if(!(i++%3)) {
336 shapes += sep + " "
337 }
338 shapes += "<point lat='${df.format(lat)}' lon='${df.format(lon)}'/>"
339 }
340 shapes += sep + "</shape>\n"
341 }
342 if(shapes) {
343 stream.write " <bounds min-lat='${df.format(minlat)}' min-lon='${df.format(minlon)}' max-lat='${df.format(maxlat)}' max-lon='${df.format(maxlon)}'>\n"
344 stream.write shapes + " </bounds>\n"
345 }
346 stream.write " </entry>\n"
347 }
348 stream.write "</imagery>\n"
349 stream.close()
350 }
351
352 void loadJosmEntries() {
353 def reader = new ImageryReader(josmInputFile)
354 josmEntries = reader.parse()
355
356 for (def e : josmEntries) {
357 def url = getUrlStripped(e)
358 if (url.contains("{z}")) {
359 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
360 url = url.replace("{z}","{zoom}")
361 }
362 if (josmUrls.containsKey(url)) {
363 myprintln "+++ JOSM-URL is not unique: "+url
364 } else {
365 josmUrls.put(url, e)
366 }
367 for (def m : e.getMirrors()) {
368 url = getUrlStripped(m)
369 m.origName = m.getOriginalName().replaceAll(" mirror server( \\d+)?","")
370 if (josmUrls.containsKey(url)) {
371 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
372 } else {
373 josmUrls.put(url, m)
374 josmMirrors.put(url, m)
375 }
376 }
377 }
378 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
379 }
380
381 void checkInOneButNotTheOther() {
382 def le = new LinkedList<String>(eliUrls.keySet())
383 def lj = new LinkedList<String>(josmUrls.keySet())
384
385 def ke = new LinkedList<String>(le)
386 for (def url : ke) {
387 if(lj.contains(url)) {
388 le.remove(url)
389 lj.remove(url)
390 }
391 }
392
393 if(le && lj) {
394 ke = new LinkedList<String>(le)
395 for (def urle : ke) {
396 def e = eliUrls.get(urle)
397 def ide = getId(e)
398 String urlhttps = urle.replace("http:","https:")
399 if(lj.contains(urlhttps))
400 {
401 myprintln l += "+ Missing https: ${getDescription(e)}"
402 eliUrls.put(urlhttps, eliUrls.get(urle))
403 eliUrls.remove(urle)
404 le.remove(urle)
405 lj.remove(urlj)
406 } else if(ide) {
407 def kj = new LinkedList<String>(lj)
408 for (def urlj : kj) {
409 def j = josmUrls.get(urlj)
410 def idj = getId(j)
411
412 if (ide.equals(idj) && getType(j) == getType(e)) {
413 myprintln "* URL for id ${idj} differs ($urle): ${getDescription(j)}"
414 le.remove(urle)
415 lj.remove(urlj)
416 /* replace key for this entry with JOSM URL */
417 eliUrls.remove(e)
418 eliUrls.put(urlj,e)
419 break;
420 }
421 }
422 }
423 }
424 }
425
426 myprintln "*** URLs found in ELI but not in JOSM (${le.size()}): ***"
427 le.sort()
428 if (!le.isEmpty()) {
429 for (def l : le) {
430 myprintln "- " + getDescription(eliUrls.get(l))
431 }
432 }
433 myprintln "*** URLs found in JOSM but not in ELI (${lj.size()}): ***"
434 lj.sort()
435 if (!lj.isEmpty()) {
436 for (def l : lj) {
437 myprintln "+ " + getDescription(josmUrls.get(l))
438 }
439 }
440 }
441
442 void checkCommonEntries() {
443 myprintln "*** Same URL, but different name: ***"
444 for (def url : eliUrls.keySet()) {
445 def e = eliUrls.get(url)
446 if (!josmUrls.containsKey(url)) continue
447 def j = josmUrls.get(url)
448 def ename = getName(e).replace("'","\u2019")
449 def jname = getName(j).replace("'","\u2019")
450 if (!ename.equals(jname)) {
451 myprintln "* Name differs ('${getName(e)}' != '${getName(j)}'): ${getUrl(j)}"
452 }
453 }
454
455 myprintln "*** Same URL, but different Id: ***"
456 for (def url : eliUrls.keySet()) {
457 def e = eliUrls.get(url)
458 if (!josmUrls.containsKey(url)) continue
459 def j = josmUrls.get(url)
460 def ename = getId(e)
461 def jname = getId(j)
462 if (!ename.equals(jname)) {
463 myprintln "# Id differs ('${getId(e)}' != '${getId(j)}'): ${getUrl(j)}"
464 }
465 }
466
467 myprintln "*** Same URL, but different type: ***"
468 for (def url : eliUrls.keySet()) {
469 def e = eliUrls.get(url)
470 if (!josmUrls.containsKey(url)) continue
471 def j = josmUrls.get(url)
472 if (!getType(e).equals(getType(j))) {
473 myprintln "* Type differs (${getType(e)} != ${getType(j)}): ${getName(j)} - ${getUrl(j)}"
474 }
475 }
476
477 myprintln "*** Same URL, but different zoom bounds: ***"
478 for (def url : eliUrls.keySet()) {
479 def e = eliUrls.get(url)
480 if (!josmUrls.containsKey(url)) continue
481 def j = josmUrls.get(url)
482
483 Integer eMinZoom = getMinZoom(e)
484 Integer jMinZoom = getMinZoom(j)
485 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
486 myprintln "* Minzoom differs (${eMinZoom} != ${jMinZoom}): ${getDescription(j)}"
487 }
488 Integer eMaxZoom = getMaxZoom(e)
489 Integer jMaxZoom = getMaxZoom(j)
490 if (eMaxZoom != jMaxZoom) {
491 myprintln "* Maxzoom differs (${eMaxZoom} != ${jMaxZoom}): ${getDescription(j)}"
492 }
493 }
494
495 myprintln "*** Same URL, but different country code: ***"
496 for (def url : eliUrls.keySet()) {
497 def e = eliUrls.get(url)
498 if (!josmUrls.containsKey(url)) continue
499 def j = josmUrls.get(url)
500 if (!getCountryCode(e).equals(getCountryCode(j))) {
501 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
502 }
503 }
504 myprintln "*** Same URL, but different quality: ***"
505 for (def url : eliUrls.keySet()) {
506 def e = eliUrls.get(url)
507 if (!josmUrls.containsKey(url)) {
508 def q = getQuality(e)
509 if("eli-best".equals(q)) {
510 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
511 }
512 continue
513 }
514 def j = josmUrls.get(url)
515 if (!getQuality(e).equals(getQuality(j))) {
516 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
517 }
518 }
519 myprintln "*** Same URL, but different dates: ***"
520 for (def url : eliUrls.keySet()) {
521 def ed = getDate(eliUrls.get(url))
522 if (!josmUrls.containsKey(url)) continue
523 def j = josmUrls.get(url)
524 def jd = getDate(j)
525 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
526 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
527 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
528 String ed2 = ed
529 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
530 if(reg != null && reg.count == 1) {
531 Calendar cal = Calendar.getInstance()
532 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)
533 cal.add(Calendar.DAY_OF_MONTH, -1)
534 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
535 if (reg[0][4] != null)
536 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
537 if (reg[0][6] != null)
538 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
539 }
540 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
541 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
542 String t = "'${ed}'"
543 if (!ed.equals(ef)) {
544 t += " or '${ef}'"
545 }
546 if (jd.isEmpty()) {
547 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
548 } else if (!ed.isEmpty()) {
549 myprintln "* Date differs ('${t}' != '${jd}'): ${getDescription(j)}"
550 } else if (!options.nomissingeli) {
551 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
552 }
553 }
554 }
555 myprintln "*** Same URL, but different information: ***"
556 for (def url : eliUrls.keySet()) {
557 if (!josmUrls.containsKey(url)) continue
558 def e = eliUrls.get(url)
559 def j = josmUrls.get(url)
560
561 def et = getDescriptions(e)
562 def jt = getDescriptions(j)
563 et = (et.size() > 0) ? et["en"] : ""
564 jt = (jt.size() > 0) ? jt["en"] : ""
565 if (!et.equals(jt)) {
566 if (!jt) {
567 myprintln "- Missing JOSM description (${et}): ${getDescription(j)}"
568 } else if (et) {
569 myprintln "* Description differs ('${et}' != '${jt}'): ${getDescription(j)}"
570 } else if (!options.nomissingeli) {
571 myprintln "+ Missing ELI description ('${jt}'): ${getDescription(j)}"
572 }
573 }
574
575 et = getPermissionReferenceUrl(e)
576 jt = getPermissionReferenceUrl(j)
577 def jt2 = getTermsOfUseUrl(j)
578 if (!jt) jt = jt2
579 if (!et.equals(jt)) {
580 if (!jt) {
581 myprintln "- Missing JOSM license URL (${et}): ${getDescription(j)}"
582 } else if (et) {
583 def ethttps = et.replace("http:","https:")
584 if(!jt2 || !(jt2.equals(ethttps) || jt2.equals(et+"/") || jt2.equals(ethttps+"/"))) {
585 if(jt.equals(ethttps) || jt.equals(et+"/") || jt.equals(ethttps+"/")) {
586 myprintln "+ License URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
587 } else {
588 def ja = getAttributionUrl(j)
589 if (ja && (ja.equals(et) || ja.equals(ethttps) || ja.equals(et+"/") || ja.equals(ethttps+"/"))) {
590 myprintln "+ ELI License URL in JOSM Attribution: ${getDescription(j)}"
591 } else {
592 myprintln "* License URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
593 }
594 }
595 }
596 } else if (!options.nomissingeli) {
597 myprintln "+ Missing ELI license URL ('${jt}'): ${getDescription(j)}"
598 }
599 }
600
601 et = getAttributionUrl(e)
602 jt = getAttributionUrl(j)
603 if (!et.equals(jt)) {
604 if (!jt) {
605 myprintln "- Missing JOSM attribution URL (${et}): ${getDescription(j)}"
606 } else if (et) {
607 def ethttps = et.replace("http:","https:")
608 if(jt.equals(ethttps) || jt.equals(et+"/") || jt.equals(ethttps+"/")) {
609 myprintln "+ Attribution URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
610 } else {
611 myprintln "* Attribution URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
612 }
613 } else if (!options.nomissingeli) {
614 myprintln "+ Missing ELI attribution URL ('${jt}'): ${getDescription(j)}"
615 }
616 }
617
618 et = getAttributionText(e)
619 jt = getAttributionText(j)
620 if (!et.equals(jt)) {
621 if (!jt) {
622 myprintln "- Missing JOSM attribution text (${et}): ${getDescription(j)}"
623 } else if (et) {
624 myprintln "* Attribution text differs ('${et}' != '${jt}'): ${getDescription(j)}"
625 } else if (!options.nomissingeli) {
626 myprintln "+ Missing ELI attribution text ('${jt}'): ${getDescription(j)}"
627 }
628 }
629
630 et = getProjections(e)
631 jt = getProjections(j)
632 if (et) { et = new LinkedList(et); Collections.sort(et); et = String.join(" ", et) }
633 if (jt) { jt = new LinkedList(jt); Collections.sort(jt); jt = String.join(" ", jt) }
634 if (!et.equals(jt)) {
635 if (!jt) {
636 def t = getType(e)
637 if(t == "wms_endpoint" || t == "tms") {
638 myprintln "+ ELI projections for type ${t}: ${getDescription(j)}"
639 }
640 else {
641 myprintln "- Missing JOSM projections (${et}): ${getDescription(j)}"
642 }
643 } else if (et) {
644 if("EPSG:3857 EPSG:4326".equals(et) || "EPSG:3857".equals(et) || "EPSG:4326".equals(et)) {
645 myprintln "+ ELI has minimal projections ('${et}' != '${jt}'): ${getDescription(j)}"
646 } else {
647 myprintln "* Projections differ ('${et}' != '${jt}'): ${getDescription(j)}"
648 }
649 } else if (!options.nomissingeli && !getType(e).equals("tms")) {
650 myprintln "+ Missing ELI projections ('${jt}'): ${getDescription(j)}"
651 }
652 }
653
654 et = getDefault(e)
655 jt = getDefault(j)
656 if (!et.equals(jt)) {
657 if (!jt) {
658 myprintln "- Missing JOSM default: ${getDescription(j)}"
659 } else if (!options.nomissingeli) {
660 myprintln "+ Missing ELI default: ${getDescription(j)}"
661 }
662 }
663 et = getOverlay(e)
664 jt = getOverlay(j)
665 if (!et.equals(jt)) {
666 if (!jt) {
667 myprintln "- Missing JOSM overlay flag: ${getDescription(j)}"
668 } else if (!options.nomissingeli) {
669 myprintln "+ Missing ELI overlay flag: ${getDescription(j)}"
670 }
671 }
672 }
673 myprintln "*** Mismatching shapes: ***"
674 for (def url : josmUrls.keySet()) {
675 def j = josmUrls.get(url)
676 def num = 1
677 for (def shape : getShapes(j)) {
678 def p = shape.getPoints()
679 if(!p[0].equals(p[p.size()-1])) {
680 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
681 }
682 for (def nump = 1; nump < p.size(); ++nump) {
683 if (p[nump-1] == p[nump]) {
684 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
685 }
686 }
687 ++num
688 }
689 }
690 for (def url : eliUrls.keySet()) {
691 def e = eliUrls.get(url)
692 def num = 1
693 def s = getShapes(e)
694 for (def shape : s) {
695 def p = shape.getPoints()
696 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
697 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
698 }
699 for (def nump = 1; nump < p.size(); ++nump) {
700 if (p[nump-1] == p[nump]) {
701 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
702 }
703 }
704 ++num
705 }
706 if (!josmUrls.containsKey(url)) {
707 continue
708 }
709 def j = josmUrls.get(url)
710 def js = getShapes(j)
711 if(!s.size() && js.size()) {
712 if(!options.nomissingeli) {
713 myprintln "+ No ELI shape: ${getDescription(j)}"
714 }
715 } else if(!js.size() && s.size()) {
716 // don't report boundary like 5 point shapes as difference
717 if (s.size() != 1 || s[0].getPoints().size() != 5) {
718 myprintln "- No JOSM shape: ${getDescription(j)}"
719 }
720 } else if(s.size() != js.size()) {
721 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
722 } else {
723 for(def nums = 0; nums < s.size(); ++nums) {
724 def ep = s[nums].getPoints()
725 def jp = js[nums].getPoints()
726 if(ep.size() != jp.size()) {
727 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
728 } else {
729 for(def nump = 0; nump < ep.size(); ++nump) {
730 def ept = ep[nump]
731 def jpt = jp[nump]
732 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
733 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
734 nump = ep.size()
735 num = s.size()
736 }
737 }
738 }
739 }
740 }
741 }
742 myprintln "*** Mismatching icons: ***"
743 for (def url : eliUrls.keySet()) {
744 def e = eliUrls.get(url)
745 if (!josmUrls.containsKey(url)) {
746 continue
747 }
748 def j = josmUrls.get(url)
749 def ij = getIcon(j)
750 def ie = getIcon(e)
751 if(ij != null && ie == null) {
752 if(!options.nomissingeli) {
753 myprintln "+ No ELI icon: ${getDescription(j)}"
754 }
755 } else if(ij == null && ie != null) {
756 myprintln "- No JOSM icon: ${getDescription(j)}"
757 } else if(!ij.equals(ie)) {
758 myprintln "* Different icons: ${getDescription(j)}"
759 }
760 }
761 myprintln "*** Miscellaneous checks: ***"
762 def josmIds = new HashMap<String, ImageryInfo>()
763 def all = Projections.getAllProjectionCodes()
764 DomainValidator dv = DomainValidator.getInstance();
765 for (def url : josmUrls.keySet()) {
766 def j = josmUrls.get(url)
767 def id = getId(j)
768 if("wms".equals(getType(j))) {
769 if(!getProjections(j)) {
770 myprintln "* WMS without projections: ${getDescription(j)}"
771 } else {
772 def unsupported = new LinkedList<String>()
773 def old = new LinkedList<String>()
774 for (def p : getProjectionsUnstripped(j)) {
775 if("CRS:84".equals(p)) {
776 if(!(url =~ /(?i)version=1\.3/)) {
777 myprintln "* CRS:84 without WMS 1.3: ${getDescription(j)}"
778 }
779 } else if(oldproj.containsKey(p)) {
780 old.add(p)
781 } else if(!all.contains(p) && !ignoreproj.contains(p)) {
782 unsupported.add(p)
783 }
784 }
785 if (unsupported) {
786 def s = String.join(", ", unsupported)
787 myprintln "* Projections ${s} not supported by JOSM: ${getDescription(j)}"
788 }
789 for (def o : old) {
790 myprintln "* Projection ${o} is an old unsupported code and has been replaced by ${oldproj.get(o)}: ${getDescription(j)}"
791 }
792 }
793 if((url =~ /(?i)version=1\.3/) && !(url =~ /[Cc][Rr][Ss]=\{proj\}/)) {
794 myprintln "* WMS 1.3 with strange CRS specification: ${getDescription(j)}"
795 }
796 if((url =~ /(?i)version=1\.1/) && !(url =~ /[Ss][Rr][Ss]=\{proj\}/)) {
797 myprintln "* WMS 1.1 with strange SRS specification: ${getDescription(j)}"
798 }
799 }
800 def urls = new LinkedList<String>()
801 if(!"scanex".equals(getType(j))) {
802 urls += url
803 }
804 def jt = getPermissionReferenceUrl(j)
805 if(jt && !"Public Domain".equals(jt))
806 urls += jt
807 jt = getTermsOfUseUrl(j)
808 if(jt)
809 urls += jt
810 jt = getAttributionUrl(j)
811 if(jt)
812 urls += jt
813 jt = getIcon(j)
814 if(jt && !(jt =~ /^data:image\/png;base64,/))
815 urls += jt
816 for(def u : urls) {
817 def m = u =~ /^https?:\/\/([^\/]+?)(:\d+)?\//
818 if(!m)
819 myprintln "* Strange URL '${u}': ${getDescription(j)}"
820 else {
821 def domain = m[0][1].replaceAll("\\{switch:.*\\}","x")
822 def port = m[0][2]
823 if (!(domain =~ /^\d+\.\d+\.\d+\.\d+$/) && !dv.isValid(domain))
824 myprintln "* Strange Domain '${domain}': ${getDescription(j)}"
825 else if (port != null && (port == ":80" || port == ":443")) {
826 myprintln "* Useless port '${port}': ${getDescription(j)}"
827 }
828 }
829 }
830
831 if(josmMirrors.containsKey(url)) {
832 continue
833 }
834 if(id == null) {
835 myprintln "* No JOSM-ID: ${getDescription(j)}"
836 } else if(josmIds.containsKey(id)) {
837 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
838 } else {
839 josmIds.put(id, j)
840 }
841 def d = getDate(j)
842 if(!d.isEmpty()) {
843 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
844 if(reg == null || reg.count != 1) {
845 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
846 } else {
847 try {
848 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
849 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
850 if(second.compareTo(first) < 0) {
851 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
852 }
853 }
854 catch (Exception e) {
855 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
856 }
857 }
858 }
859 if(getAttributionUrl(j) && !getAttributionText(j)) {
860 myprintln "* Attribution link without text: ${getDescription(j)}"
861 }
862 if(getLogoUrl(j) && !getLogoImage(j)) {
863 myprintln "* Logo link without image: ${getDescription(j)}"
864 }
865 if(getTermsOfUseText(j) && !getTermsOfUseUrl(j)) {
866 myprintln "* Terms of Use text without link: ${getDescription(j)}"
867 }
868 def js = getShapes(j)
869 if(js.size()) {
870 def minlat = 1000
871 def minlon = 1000
872 def maxlat = -1000
873 def maxlon = -1000
874 for(def s: js) {
875 for(def p: s.getPoints()) {
876 def lat = p.getLat()
877 def lon = p.getLon()
878 if(lat > maxlat) maxlat = lat
879 if(lon > maxlon) maxlon = lon
880 if(lat < minlat) minlat = lat
881 if(lon < minlon) minlon = lon
882 }
883 }
884 def b = j.getBounds()
885 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
886 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)}"
887 }
888 }
889 }
890 }
891
892 /**
893 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
894 */
895 static String getUrl(Object e) {
896 if (e instanceof ImageryInfo) return e.url
897 return e.get("properties").getString("url")
898 }
899 static String getUrlStripped(Object e) {
900 return getUrl(e).replaceAll("\\?(apikey|access_token)=.*","")
901 }
902 static String getDate(Object e) {
903 if (e instanceof ImageryInfo) return e.date ? e.date : ""
904 def p = e.get("properties")
905 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
906 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
907 if(!start.isEmpty() && !end.isEmpty())
908 return start+";"+end
909 else if(!start.isEmpty())
910 return start+";-"
911 else if(!end.isEmpty())
912 return "-;"+end
913 return ""
914 }
915 static Date verifyDate(String year, String month, String day) {
916 def date
917 if(year == null) {
918 date = "3000-01-01"
919 } else {
920 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
921 }
922 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
923 df.setLenient(false)
924 return df.parse(date)
925 }
926 static String getId(Object e) {
927 if (e instanceof ImageryInfo) return e.getId()
928 return e.get("properties").getString("id")
929 }
930 static String getName(Object e) {
931 if (e instanceof ImageryInfo) return e.getOriginalName()
932 return e.get("properties").getString("name")
933 }
934 static List<Object> getMirrors(Object e) {
935 if (e instanceof ImageryInfo) return e.getMirrors()
936 return []
937 }
938 static List<Object> getProjections(Object e) {
939 def r = []
940 def u = getProjectionsUnstripped(e)
941 if(u) {
942 for (def p : u) {
943 if(!oldproj.containsKey(p) && !("CRS:84".equals(p) && !(getUrlStripped(e) =~ /(?i)version=1\.3/))) {
944 r += p
945 }
946 }
947 }
948 return r
949 }
950 static List<Object> getProjectionsUnstripped(Object e) {
951 def r
952 if (e instanceof ImageryInfo) {
953 r = e.getServerProjections()
954 } else {
955 def s = e.get("properties").get("available_projections")
956 if (s) {
957 r = []
958 for (def p : s) {
959 r += p.getString()
960 }
961 }
962 }
963 return r ? r : []
964 }
965 static List<Shape> getShapes(Object e) {
966 if (e instanceof ImageryInfo) {
967 def bounds = e.getBounds()
968 if(bounds != null) {
969 return bounds.getShapes()
970 }
971 return []
972 }
973 if(!e.isNull("geometry")) {
974 def ex = e.get("geometry")
975 if(ex != null && !ex.isNull("coordinates")) {
976 def poly = ex.get("coordinates")
977 List<Shape> l = []
978 for(def shapes: poly) {
979 def s = new Shape()
980 for(def point: shapes) {
981 def lon = point[0].toString()
982 def lat = point[1].toString()
983 s.addPoint(lat, lon)
984 }
985 l.add(s)
986 }
987 return l
988 }
989 }
990 return []
991 }
992 static String getType(Object e) {
993 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
994 return e.get("properties").getString("type")
995 }
996 static Integer getMinZoom(Object e) {
997 if (e instanceof ImageryInfo) {
998 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
999 return null;
1000 int mz = e.getMinZoom()
1001 return mz == 0 ? null : mz
1002 } else {
1003 def num = e.get("properties").getJsonNumber("min_zoom")
1004 if (num == null) return null
1005 return num.intValue()
1006 }
1007 }
1008 static Integer getMaxZoom(Object e) {
1009 if (e instanceof ImageryInfo) {
1010 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
1011 return null;
1012 int mz = e.getMaxZoom()
1013 return mz == 0 ? null : mz
1014 } else {
1015 def num = e.get("properties").getJsonNumber("max_zoom")
1016 if (num == null) return null
1017 return num.intValue()
1018 }
1019 }
1020 static String getCountryCode(Object e) {
1021 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
1022 return e.get("properties").getString("country_code", null)
1023 }
1024 static String getQuality(Object e) {
1025 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
1026 return (e.get("properties").containsKey("best")
1027 && e.get("properties").getBoolean("best")) ? "eli-best" : null
1028 }
1029 static Boolean getOverlay(Object e) {
1030 if (e instanceof ImageryInfo) return e.isOverlay()
1031 return (e.get("properties").containsKey("overlay")
1032 && e.get("properties").getBoolean("overlay"))
1033 }
1034 static String getIcon(Object e) {
1035 if (e instanceof ImageryInfo) return e.getIcon()
1036 return e.get("properties").getString("icon", null)
1037 }
1038 static String getAttributionText(Object e) {
1039 if (e instanceof ImageryInfo) return e.getAttributionText(0, null, null)
1040 try {return e.get("properties").get("attribution").getString("text", null)} catch (NullPointerException ex) {return null}
1041 }
1042 static String getAttributionUrl(Object e) {
1043 if (e instanceof ImageryInfo) return e.getAttributionLinkURL()
1044 try {return e.get("properties").get("attribution").getString("url", null)} catch (NullPointerException ex) {return null}
1045 }
1046 static String getTermsOfUseText(Object e) {
1047 if (e instanceof ImageryInfo) return e.getTermsOfUseText()
1048 return null
1049 }
1050 static String getTermsOfUseUrl(Object e) {
1051 if (e instanceof ImageryInfo) return e.getTermsOfUseURL()
1052 return null
1053 }
1054 static String getLogoImage(Object e) {
1055 if (e instanceof ImageryInfo) return e.getAttributionImageRaw()
1056 return null
1057 }
1058 static String getLogoUrl(Object e) {
1059 if (e instanceof ImageryInfo) return e.getAttributionImageURL()
1060 return null
1061 }
1062 static String getPermissionReferenceUrl(Object e) {
1063 if (e instanceof ImageryInfo) return e.getPermissionReferenceURL()
1064 return e.get("properties").getString("license_url", null)
1065 }
1066 static Map<String,String> getDescriptions(Object e) {
1067 Map<String,String> res = new HashMap<String, String>()
1068 if (e instanceof ImageryInfo) {
1069 String a = e.getDescription()
1070 if (a) res.put("en", a)
1071 } else {
1072 String a = e.get("properties").getString("description", null)
1073 if (a) res.put("en", a)
1074 }
1075 return res
1076 }
1077 static Boolean getValidGeoreference(Object e) {
1078 if (e instanceof ImageryInfo) return e.isGeoreferenceValid()
1079 return false
1080 }
1081 static Boolean getDefault(Object e) {
1082 if (e instanceof ImageryInfo) return e.isDefaultEntry()
1083 return e.get("properties").getBoolean("default", false)
1084 }
1085 String getDescription(Object o) {
1086 def url = getUrl(o)
1087 def cc = getCountryCode(o)
1088 if (cc == null) {
1089 def j = josmUrls.get(url)
1090 if (j != null) cc = getCountryCode(j)
1091 if (cc == null) {
1092 def e = eliUrls.get(url)
1093 if (e != null) cc = getCountryCode(e)
1094 }
1095 }
1096 if (cc == null) {
1097 cc = ''
1098 } else {
1099 cc = "[$cc] "
1100 }
1101 def d = cc + getName(o) + " - " + getUrl(o)
1102 if (options.shorten) {
1103 def MAXLEN = 140
1104 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
1105 }
1106 return d
1107 }
1108}
Note: See TracBrowser for help on using the repository browser.