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

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

ignore ELI ZZ country code

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