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

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

add useless port check

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