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

Last change on this file since 13831 was 13831, checked in by Don-vip, 6 years ago

fix #16313 - NPE

  • Property svn:eol-style set to native
File size: 47.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
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 if (!getCountryCode(e).equals(getCountryCode(j))) {
510 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
511 }
512 }
513 myprintln "*** Same URL, but different quality: ***"
514 for (def url : eliUrls.keySet()) {
515 def e = eliUrls.get(url)
516 if (!josmUrls.containsKey(url)) {
517 def q = getQuality(e)
518 if("eli-best".equals(q)) {
519 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
520 }
521 continue
522 }
523 def j = josmUrls.get(url)
524 if (!getQuality(e).equals(getQuality(j))) {
525 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
526 }
527 }
528 myprintln "*** Same URL, but different dates: ***"
529 for (def url : eliUrls.keySet()) {
530 def ed = getDate(eliUrls.get(url))
531 if (!josmUrls.containsKey(url)) continue
532 def j = josmUrls.get(url)
533 def jd = getDate(j)
534 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
535 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
536 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
537 String ed2 = ed
538 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
539 if(reg != null && reg.count == 1) {
540 Calendar cal = Calendar.getInstance()
541 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)
542 cal.add(Calendar.DAY_OF_MONTH, -1)
543 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
544 if (reg[0][4] != null)
545 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
546 if (reg[0][6] != null)
547 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
548 }
549 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1")
550 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
551 String t = "'${ed}'"
552 if (!ed.equals(ef)) {
553 t += " or '${ef}'"
554 }
555 if (jd.isEmpty()) {
556 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
557 } else if (!ed.isEmpty()) {
558 myprintln "* Date differs ('${t}' != '${jd}'): ${getDescription(j)}"
559 } else if (!options.nomissingeli) {
560 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
561 }
562 }
563 }
564 myprintln "*** Same URL, but different information: ***"
565 for (def url : eliUrls.keySet()) {
566 if (!josmUrls.containsKey(url)) continue
567 def e = eliUrls.get(url)
568 def j = josmUrls.get(url)
569
570 def et = getDescriptions(e)
571 def jt = getDescriptions(j)
572 et = (et.size() > 0) ? et["en"] : ""
573 jt = (jt.size() > 0) ? jt["en"] : ""
574 if (!et.equals(jt)) {
575 if (!jt) {
576 myprintln "- Missing JOSM description (${et}): ${getDescription(j)}"
577 } else if (et) {
578 myprintln "* Description differs ('${et}' != '${jt}'): ${getDescription(j)}"
579 } else if (!options.nomissingeli) {
580 myprintln "+ Missing ELI description ('${jt}'): ${getDescription(j)}"
581 }
582 }
583
584 et = getPermissionReferenceUrl(e)
585 jt = getPermissionReferenceUrl(j)
586 def jt2 = getTermsOfUseUrl(j)
587 if (!jt) jt = jt2
588 if (!et.equals(jt)) {
589 if (!jt) {
590 myprintln "- Missing JOSM license URL (${et}): ${getDescription(j)}"
591 } else if (et) {
592 def ethttps = et.replace("http:","https:")
593 if(!jt2 || !(jt2.equals(ethttps) || jt2.equals(et+"/") || jt2.equals(ethttps+"/"))) {
594 if(jt.equals(ethttps) || jt.equals(et+"/") || jt.equals(ethttps+"/")) {
595 myprintln "+ License URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
596 } else {
597 def ja = getAttributionUrl(j)
598 if (ja && (ja.equals(et) || ja.equals(ethttps) || ja.equals(et+"/") || ja.equals(ethttps+"/"))) {
599 myprintln "+ ELI License URL in JOSM Attribution: ${getDescription(j)}"
600 } else {
601 myprintln "* License URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
602 }
603 }
604 }
605 } else if (!options.nomissingeli) {
606 myprintln "+ Missing ELI license URL ('${jt}'): ${getDescription(j)}"
607 }
608 }
609
610 et = getAttributionUrl(e)
611 jt = getAttributionUrl(j)
612 if (!et.equals(jt)) {
613 if (!jt) {
614 myprintln "- Missing JOSM attribution URL (${et}): ${getDescription(j)}"
615 } else if (et) {
616 def ethttps = et.replace("http:","https:")
617 if(jt.equals(ethttps) || jt.equals(et+"/") || jt.equals(ethttps+"/")) {
618 myprintln "+ Attribution URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
619 } else {
620 myprintln "* Attribution URL differs ('${et}' != '${jt}'): ${getDescription(j)}"
621 }
622 } else if (!options.nomissingeli) {
623 myprintln "+ Missing ELI attribution URL ('${jt}'): ${getDescription(j)}"
624 }
625 }
626
627 et = getAttributionText(e)
628 jt = getAttributionText(j)
629 if (!et.equals(jt)) {
630 if (!jt) {
631 myprintln "- Missing JOSM attribution text (${et}): ${getDescription(j)}"
632 } else if (et) {
633 myprintln "* Attribution text differs ('${et}' != '${jt}'): ${getDescription(j)}"
634 } else if (!options.nomissingeli) {
635 myprintln "+ Missing ELI attribution text ('${jt}'): ${getDescription(j)}"
636 }
637 }
638
639 et = getProjections(e)
640 jt = getProjections(j)
641 if (et) { et = new LinkedList(et); Collections.sort(et); et = String.join(" ", et) }
642 if (jt) { jt = new LinkedList(jt); Collections.sort(jt); jt = String.join(" ", jt) }
643 if (!et.equals(jt)) {
644 if (!jt) {
645 def t = getType(e)
646 if(t == "wms_endpoint" || t == "tms") {
647 myprintln "+ ELI projections for type ${t}: ${getDescription(j)}"
648 }
649 else {
650 myprintln "- Missing JOSM projections (${et}): ${getDescription(j)}"
651 }
652 } else if (et) {
653 if("EPSG:3857 EPSG:4326".equals(et) || "EPSG:3857".equals(et) || "EPSG:4326".equals(et)) {
654 myprintln "+ ELI has minimal projections ('${et}' != '${jt}'): ${getDescription(j)}"
655 } else {
656 myprintln "* Projections differ ('${et}' != '${jt}'): ${getDescription(j)}"
657 }
658 } else if (!options.nomissingeli && !getType(e).equals("tms")) {
659 myprintln "+ Missing ELI projections ('${jt}'): ${getDescription(j)}"
660 }
661 }
662
663 et = getDefault(e)
664 jt = getDefault(j)
665 if (!et.equals(jt)) {
666 if (!jt) {
667 myprintln "- Missing JOSM default: ${getDescription(j)}"
668 } else if (!options.nomissingeli) {
669 myprintln "+ Missing ELI default: ${getDescription(j)}"
670 }
671 }
672 et = getOverlay(e)
673 jt = getOverlay(j)
674 if (!et.equals(jt)) {
675 if (!jt) {
676 myprintln "- Missing JOSM overlay flag: ${getDescription(j)}"
677 } else if (!options.nomissingeli) {
678 myprintln "+ Missing ELI overlay flag: ${getDescription(j)}"
679 }
680 }
681 }
682 myprintln "*** Mismatching shapes: ***"
683 for (def url : josmUrls.keySet()) {
684 def j = josmUrls.get(url)
685 def num = 1
686 for (def shape : getShapes(j)) {
687 def p = shape.getPoints()
688 if(!p[0].equals(p[p.size()-1])) {
689 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
690 }
691 for (def nump = 1; nump < p.size(); ++nump) {
692 if (p[nump-1] == p[nump]) {
693 myprintln "+++ JOSM shape $num double point at ${nump-1}: ${getDescription(j)}"
694 }
695 }
696 ++num
697 }
698 }
699 for (def url : eliUrls.keySet()) {
700 def e = eliUrls.get(url)
701 def num = 1
702 def s
703 try {
704 s = getShapes(e)
705 for (def shape : s) {
706 def p = shape.getPoints()
707 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
708 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
709 }
710 for (def nump = 1; nump < p.size(); ++nump) {
711 if (p[nump-1] == p[nump]) {
712 myprintln "+++ ELI shape $num double point at ${nump-1}: ${getDescription(e)}"
713 }
714 }
715 ++num
716 }
717 } catch(IllegalArgumentException err) {
718 def desc = getDescription(e)
719 myprintln("* Invalid data in ELI geometry for $desc: ${err.getMessage()}")
720 }
721 if (s == null || !josmUrls.containsKey(url)) {
722 continue
723 }
724 def j = josmUrls.get(url)
725 def js = getShapes(j)
726 if(!s.size() && js.size()) {
727 if(!options.nomissingeli) {
728 myprintln "+ No ELI shape: ${getDescription(j)}"
729 }
730 } else if(!js.size() && s.size()) {
731 // don't report boundary like 5 point shapes as difference
732 if (s.size() != 1 || s[0].getPoints().size() != 5) {
733 myprintln "- No JOSM shape: ${getDescription(j)}"
734 }
735 } else if(s.size() != js.size()) {
736 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
737 } else {
738 for(def nums = 0; nums < s.size(); ++nums) {
739 def ep = s[nums].getPoints()
740 def jp = js[nums].getPoints()
741 if(ep.size() != jp.size()) {
742 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
743 } else {
744 for(def nump = 0; nump < ep.size(); ++nump) {
745 def ept = ep[nump]
746 def jpt = jp[nump]
747 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.00001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.00001) {
748 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
749 nump = ep.size()
750 num = s.size()
751 }
752 }
753 }
754 }
755 }
756 }
757 myprintln "*** Mismatching icons: ***"
758 for (def url : eliUrls.keySet()) {
759 def e = eliUrls.get(url)
760 if (!josmUrls.containsKey(url)) {
761 continue
762 }
763 def j = josmUrls.get(url)
764 def ij = getIcon(j)
765 def ie = getIcon(e)
766 if(ij != null && ie == null) {
767 if(!options.nomissingeli) {
768 myprintln "+ No ELI icon: ${getDescription(j)}"
769 }
770 } else if(ij == null && ie != null) {
771 myprintln "- No JOSM icon: ${getDescription(j)}"
772 } else if(!ij.equals(ie)) {
773 myprintln "* Different icons: ${getDescription(j)}"
774 }
775 }
776 myprintln "*** Miscellaneous checks: ***"
777 def josmIds = new HashMap<String, ImageryInfo>()
778 def all = Projections.getAllProjectionCodes()
779 DomainValidator dv = DomainValidator.getInstance();
780 for (def url : josmUrls.keySet()) {
781 def j = josmUrls.get(url)
782 def id = getId(j)
783 if("wms".equals(getType(j))) {
784 if(!getProjections(j)) {
785 myprintln "* WMS without projections: ${getDescription(j)}"
786 } else {
787 def unsupported = new LinkedList<String>()
788 def old = new LinkedList<String>()
789 for (def p : getProjectionsUnstripped(j)) {
790 if("CRS:84".equals(p)) {
791 if(!(url =~ /(?i)version=1\.3/)) {
792 myprintln "* CRS:84 without WMS 1.3: ${getDescription(j)}"
793 }
794 } else if(oldproj.containsKey(p)) {
795 old.add(p)
796 } else if(!all.contains(p) && !ignoreproj.contains(p)) {
797 unsupported.add(p)
798 }
799 }
800 if (unsupported) {
801 def s = String.join(", ", unsupported)
802 myprintln "* Projections ${s} not supported by JOSM: ${getDescription(j)}"
803 }
804 for (def o : old) {
805 myprintln "* Projection ${o} is an old unsupported code and has been replaced by ${oldproj.get(o)}: ${getDescription(j)}"
806 }
807 }
808 if((url =~ /(?i)version=1\.3/) && !(url =~ /[Cc][Rr][Ss]=\{proj\}/)) {
809 myprintln "* WMS 1.3 with strange CRS specification: ${getDescription(j)}"
810 }
811 if((url =~ /(?i)version=1\.1/) && !(url =~ /[Ss][Rr][Ss]=\{proj\}/)) {
812 myprintln "* WMS 1.1 with strange SRS specification: ${getDescription(j)}"
813 }
814 }
815 def urls = new LinkedList<String>()
816 if(!"scanex".equals(getType(j))) {
817 urls += url
818 }
819 def jt = getPermissionReferenceUrl(j)
820 if(jt && !"Public Domain".equals(jt))
821 urls += jt
822 jt = getTermsOfUseUrl(j)
823 if(jt)
824 urls += jt
825 jt = getAttributionUrl(j)
826 if(jt)
827 urls += jt
828 jt = getIcon(j)
829 if(jt && !(jt =~ /^data:image\/png;base64,/))
830 urls += jt
831 for(def u : urls) {
832 def m = u =~ /^https?:\/\/([^\/]+?)(:\d+)?\//
833 if(!m || u =~ /[ \t]+$/)
834 myprintln "* Strange URL '${u}': ${getDescription(j)}"
835 else {
836 def domain = m[0][1].replaceAll("\\{switch:.*\\}","x")
837 def port = m[0][2]
838 if (!(domain =~ /^\d+\.\d+\.\d+\.\d+$/) && !dv.isValid(domain))
839 myprintln "* Strange Domain '${domain}': ${getDescription(j)}"
840 else if (port != null && (port == ":80" || port == ":443")) {
841 myprintln "* Useless port '${port}': ${getDescription(j)}"
842 }
843 }
844 }
845
846 if(josmMirrors.containsKey(url)) {
847 continue
848 }
849 if(id == null) {
850 myprintln "* No JOSM-ID: ${getDescription(j)}"
851 } else if(josmIds.containsKey(id)) {
852 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
853 } else {
854 josmIds.put(id, j)
855 }
856 def d = getDate(j)
857 if(!d.isEmpty()) {
858 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
859 if(reg == null || reg.count != 1) {
860 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
861 } else {
862 try {
863 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6])
864 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13])
865 if(second.compareTo(first) < 0) {
866 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
867 }
868 }
869 catch (Exception e) {
870 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
871 }
872 }
873 }
874 if(getAttributionUrl(j) && !getAttributionText(j)) {
875 myprintln "* Attribution link without text: ${getDescription(j)}"
876 }
877 if(getLogoUrl(j) && !getLogoImage(j)) {
878 myprintln "* Logo link without image: ${getDescription(j)}"
879 }
880 if(getTermsOfUseText(j) && !getTermsOfUseUrl(j)) {
881 myprintln "* Terms of Use text without link: ${getDescription(j)}"
882 }
883 def js = getShapes(j)
884 if(js.size()) {
885 def minlat = 1000
886 def minlon = 1000
887 def maxlat = -1000
888 def maxlon = -1000
889 for(def s: js) {
890 for(def p: s.getPoints()) {
891 def lat = p.getLat()
892 def lon = p.getLon()
893 if(lat > maxlat) maxlat = lat
894 if(lon > maxlon) maxlon = lon
895 if(lat < minlat) minlat = lat
896 if(lon < minlon) minlon = lon
897 }
898 }
899 def b = j.getBounds()
900 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
901 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)}"
902 }
903 }
904 def cat = getCategory(j)
905 if(cat != null && cat != "photo" && cat != "map" && cat != "historicmap" && cat != "osmbasedmap" && cat != "historicphoto" && cat != "other") {
906 myprintln "* Strange category ${cat}: ${getDescription(j)}"
907 }
908 }
909 }
910
911 /**
912 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
913 */
914 static String getUrl(Object e) {
915 if (e instanceof ImageryInfo) return e.url
916 return e.get("properties").getString("url")
917 }
918 static String getUrlStripped(Object e) {
919 return getUrl(e).replaceAll("\\?(apikey|access_token)=.*","")
920 }
921 static String getDate(Object e) {
922 if (e instanceof ImageryInfo) return e.date ? e.date : ""
923 def p = e.get("properties")
924 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
925 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
926 if(!start.isEmpty() && !end.isEmpty())
927 return start+";"+end
928 else if(!start.isEmpty())
929 return start+";-"
930 else if(!end.isEmpty())
931 return "-;"+end
932 return ""
933 }
934 static Date verifyDate(String year, String month, String day) {
935 def date
936 if(year == null) {
937 date = "3000-01-01"
938 } else {
939 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
940 }
941 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
942 df.setLenient(false)
943 return df.parse(date)
944 }
945 static String getId(Object e) {
946 if (e instanceof ImageryInfo) return e.getId()
947 return e.get("properties").getString("id")
948 }
949 static String getName(Object e) {
950 if (e instanceof ImageryInfo) return e.getOriginalName()
951 return e.get("properties").getString("name")
952 }
953 static List<Object> getMirrors(Object e) {
954 if (e instanceof ImageryInfo) return e.getMirrors()
955 return []
956 }
957 static List<Object> getProjections(Object e) {
958 def r = []
959 def u = getProjectionsUnstripped(e)
960 if(u) {
961 for (def p : u) {
962 if(!oldproj.containsKey(p) && !("CRS:84".equals(p) && !(getUrlStripped(e) =~ /(?i)version=1\.3/))) {
963 r += p
964 }
965 }
966 }
967 return r
968 }
969 static List<Object> getProjectionsUnstripped(Object e) {
970 def r
971 if (e instanceof ImageryInfo) {
972 r = e.getServerProjections()
973 } else {
974 def s = e.get("properties").get("available_projections")
975 if (s) {
976 r = []
977 for (def p : s) {
978 r += p.getString()
979 }
980 }
981 }
982 return r ? r : []
983 }
984 static List<Shape> getShapes(Object e) {
985 if (e instanceof ImageryInfo) {
986 def bounds = e.getBounds()
987 if(bounds != null) {
988 return bounds.getShapes()
989 }
990 return []
991 }
992 def ex = e.get("geometry")
993 if (ex != null && !JsonValue.NULL.equals(ex) && !ex.isNull("coordinates")) {
994 def poly = ex.get("coordinates")
995 List<Shape> l = []
996 for(def shapes: poly) {
997 def s = new Shape()
998 for(def point: shapes) {
999 def lon = point[0].toString()
1000 def lat = point[1].toString()
1001 s.addPoint(lat, lon)
1002 }
1003 l.add(s)
1004 }
1005 return l
1006 }
1007 return []
1008 }
1009 static String getType(Object e) {
1010 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
1011 return e.get("properties").getString("type")
1012 }
1013 static Integer getMinZoom(Object e) {
1014 if (e instanceof ImageryInfo) {
1015 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
1016 return null
1017 int mz = e.getMinZoom()
1018 return mz == 0 ? null : mz
1019 } else {
1020 def num = e.get("properties").getJsonNumber("min_zoom")
1021 if (num == null) return null
1022 return num.intValue()
1023 }
1024 }
1025 static Integer getMaxZoom(Object e) {
1026 if (e instanceof ImageryInfo) {
1027 if("wms".equals(getType(e)) && e.getName() =~ / mirror/)
1028 return null
1029 int mz = e.getMaxZoom()
1030 return mz == 0 ? null : mz
1031 } else {
1032 def num = e.get("properties").getJsonNumber("max_zoom")
1033 if (num == null) return null
1034 return num.intValue()
1035 }
1036 }
1037 static String getCountryCode(Object e) {
1038 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
1039 return e.get("properties").getString("country_code", null)
1040 }
1041 static String getQuality(Object e) {
1042 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
1043 return (e.get("properties").containsKey("best")
1044 && e.get("properties").getBoolean("best")) ? "eli-best" : null
1045 }
1046 static Boolean getOverlay(Object e) {
1047 if (e instanceof ImageryInfo) return e.isOverlay()
1048 return (e.get("properties").containsKey("overlay")
1049 && e.get("properties").getBoolean("overlay"))
1050 }
1051 static String getIcon(Object e) {
1052 if (e instanceof ImageryInfo) return e.getIcon()
1053 return e.get("properties").getString("icon", null)
1054 }
1055 static String getAttributionText(Object e) {
1056 if (e instanceof ImageryInfo) return e.getAttributionText(0, null, null)
1057 try {return e.get("properties").get("attribution").getString("text", null)} catch (NullPointerException ex) {return null}
1058 }
1059 static String getAttributionUrl(Object e) {
1060 if (e instanceof ImageryInfo) return e.getAttributionLinkURL()
1061 try {return e.get("properties").get("attribution").getString("url", null)} catch (NullPointerException ex) {return null}
1062 }
1063 static String getTermsOfUseText(Object e) {
1064 if (e instanceof ImageryInfo) return e.getTermsOfUseText()
1065 return null
1066 }
1067 static String getTermsOfUseUrl(Object e) {
1068 if (e instanceof ImageryInfo) return e.getTermsOfUseURL()
1069 return null
1070 }
1071 static String getCategory(Object e) {
1072 if (e instanceof ImageryInfo) {
1073 return e.getImageryCategoryOriginalString()
1074 }
1075 return null
1076 }
1077 static String getLogoImage(Object e) {
1078 if (e instanceof ImageryInfo) return e.getAttributionImageRaw()
1079 return null
1080 }
1081 static String getLogoUrl(Object e) {
1082 if (e instanceof ImageryInfo) return e.getAttributionImageURL()
1083 return null
1084 }
1085 static String getPermissionReferenceUrl(Object e) {
1086 if (e instanceof ImageryInfo) return e.getPermissionReferenceURL()
1087 return e.get("properties").getString("license_url", null)
1088 }
1089 static Map<String,String> getDescriptions(Object e) {
1090 Map<String,String> res = new HashMap<String, String>()
1091 if (e instanceof ImageryInfo) {
1092 String a = e.getDescription()
1093 if (a) res.put("en", a)
1094 } else {
1095 String a = e.get("properties").getString("description", null)
1096 if (a) res.put("en", a)
1097 }
1098 return res
1099 }
1100 static Boolean getValidGeoreference(Object e) {
1101 if (e instanceof ImageryInfo) return e.isGeoreferenceValid()
1102 return false
1103 }
1104 static Boolean getDefault(Object e) {
1105 if (e instanceof ImageryInfo) return e.isDefaultEntry()
1106 return e.get("properties").getBoolean("default", false)
1107 }
1108 String getDescription(Object o) {
1109 def url = getUrl(o)
1110 def cc = getCountryCode(o)
1111 if (cc == null) {
1112 def j = josmUrls.get(url)
1113 if (j != null) cc = getCountryCode(j)
1114 if (cc == null) {
1115 def e = eliUrls.get(url)
1116 if (e != null) cc = getCountryCode(e)
1117 }
1118 }
1119 if (cc == null) {
1120 cc = ''
1121 } else {
1122 cc = "[$cc] "
1123 }
1124 def d = cc + getName(o) + " - " + getUrl(o)
1125 if (options.shorten) {
1126 def MAXLEN = 140
1127 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
1128 }
1129 return d
1130 }
1131}
Note: See TracBrowser for help on using the repository browser.