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

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

don't issue error when license matches attribution

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