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

Last change on this file since 13619 was 13619, checked in by stoecker, 6 years ago

better encoding support, see #14655

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