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

Last change on this file since 11765 was 11694, checked in by Klumbumbus, 7 years ago

fix #14474 - update preset links, update imagery compare script docu

  • Property svn:eol-style set to native
File size: 23.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2/**
3 * Compare and analyse the differences of the editor imagery index and the JOSM imagery list.
4 * The goal is to keep both lists in sync.
5 *
6 * The editor imagery index project (https://github.com/osmlab/editor-imagery-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 SyncEditorImageryIndex.groovy
16 *
17 * Add option "-h" to show the available command line flags.
18 */
19import javax.json.Json
20import javax.json.JsonArray
21import javax.json.JsonObject
22import javax.json.JsonReader
23import javax.json.JsonValue
24
25import org.openstreetmap.josm.data.imagery.ImageryInfo
26import org.openstreetmap.josm.data.imagery.Shape
27import org.openstreetmap.josm.io.imagery.ImageryReader
28
29class SyncEditorImageryIndex {
30
31 List<ImageryInfo> josmEntries;
32 JsonArray eliEntries;
33
34 def eliUrls = new HashMap<String, JsonObject>()
35 def josmUrls = new HashMap<String, ImageryInfo>()
36 def josmMirrors = new HashMap<String, ImageryInfo>()
37
38 static String eliInputFile = 'imagery.geojson'
39 static String josmInputFile = 'maps.xml'
40 static String ignoreInputFile = 'maps_ignores.txt'
41 static FileWriter outputFile = null
42 static BufferedWriter outputStream = null
43 def skip = [:]
44
45 static def options
46
47 /**
48 * Main method.
49 */
50 static main(def args) {
51 parse_command_line_arguments(args)
52 def script = new SyncEditorImageryIndex()
53 script.loadSkip()
54 script.start()
55 script.loadJosmEntries()
56 script.loadELIEntries()
57 script.checkInOneButNotTheOther()
58 script.checkCommonEntries()
59 script.end()
60 if(outputStream != null) {
61 outputStream.close();
62 }
63 if(outputFile != null) {
64 outputFile.close();
65 }
66 }
67
68 /**
69 * Parse command line arguments.
70 */
71 static void parse_command_line_arguments(args) {
72 def cli = new CliBuilder(width: 160)
73 cli.o(longOpt:'output', args:1, argName: "output", "Output file, - prints to stdout (default: -)")
74 cli.e(longOpt:'eli_input', args:1, argName:"eli_input", "Input file for the editor imagery index (geojson). Default is $eliInputFile (current directory).")
75 cli.j(longOpt:'josm_input', args:1, argName:"josm_input", "Input file for the JOSM imagery list (xml). Default is $josmInputFile (current directory).")
76 cli.i(longOpt:'ignore_input', args:1, argName:"ignore_input", "Input file for the ignore list. Default is $ignoreInputFile (current directory).")
77 cli.s(longOpt:'shorten', "shorten the output, so it is easier to read in a console window")
78 cli.n(longOpt:'noskip', argName:"noskip", "don't skip known entries")
79 cli.x(longOpt:'xhtmlbody', argName:"xhtmlbody", "create XHTML body for display in a web page")
80 cli.X(longOpt:'xhtml', argName:"xhtml", "create XHTML for display in a web page")
81 cli.m(longOpt:'nomissingeli', argName:"nomissingeli", "don't show missing editor imagery index entries")
82 cli.h(longOpt:'help', "show this help")
83 options = cli.parse(args)
84
85 if (options.h) {
86 cli.usage()
87 System.exit(0)
88 }
89 if (options.eli_input) {
90 eliInputFile = options.eli_input
91 }
92 if (options.josm_input) {
93 josmInputFile = options.josm_input
94 }
95 if (options.ignore_input) {
96 ignoreInputFile = options.ignore_input
97 }
98 if (options.output && options.output != "-") {
99 outputFile = new FileWriter(options.output)
100 outputStream = new BufferedWriter(outputFile)
101 }
102 }
103
104 void loadSkip() {
105 FileReader fr = new FileReader(ignoreInputFile)
106 def line
107
108 while((line = fr.readLine()) != null) {
109 def res = (line =~ /^\|\| *(ELI|Ignore) *\|\| *\{\{\{(.+)\}\}\} *\|\|/)
110 if(res.count)
111 {
112 if(res[0][1].equals("Ignore")) {
113 skip[res[0][2]] = "green"
114 } else {
115 skip[res[0][2]] = "darkgoldenrod"
116 }
117 }
118 }
119 }
120
121 void myprintlnfinal(String s) {
122 if(outputStream != null) {
123 outputStream.write(s);
124 outputStream.newLine();
125 } else {
126 println s;
127 }
128 }
129
130 void myprintln(String s) {
131 if(skip.containsKey(s)) {
132 String color = skip.get(s)
133 skip.remove(s)
134 if(options.xhtmlbody || options.xhtml) {
135 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
136 }
137 if (!options.noskip) {
138 return;
139 }
140 } else if(options.xhtmlbody || options.xhtml) {
141 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ ELI")) ? "blue" : "red")
142 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
143 }
144 myprintlnfinal(s)
145 }
146
147 void start() {
148 if (options.xhtml) {
149 myprintlnfinal "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
150 myprintlnfinal "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/><title>JOSM - ELI differences</title></head><body>\n"
151 }
152 }
153
154 void end() {
155 for (def s: skip.keySet()) {
156 myprintln "+++ Obsolete skip entry: " + s
157 }
158 if (options.xhtml) {
159 myprintlnfinal "</body></html>\n"
160 }
161 }
162
163 void loadELIEntries() {
164 FileReader fr = new FileReader(eliInputFile)
165 JsonReader jr = Json.createReader(fr)
166 eliEntries = jr.readObject().get("features")
167 jr.close()
168
169 for (def e : eliEntries) {
170 def url = getUrl(e)
171 if (url.contains("{z}")) {
172 myprintln "+++ ELI-URL uses {z} instead of {zoom}: "+url
173 url = url.replace("{z}","{zoom}")
174 }
175 if (eliUrls.containsKey(url)) {
176 myprintln "+++ ELI-URL is not unique: "+url
177 } else {
178 eliUrls.put(url, e)
179 }
180 }
181 myprintln "*** Loaded ${eliEntries.size()} entries (ELI). ***"
182 }
183
184 void loadJosmEntries() {
185 def reader = new ImageryReader(josmInputFile)
186 josmEntries = reader.parse()
187
188 for (def e : josmEntries) {
189 def url = getUrl(e)
190 if (url.contains("{z}")) {
191 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
192 url = url.replace("{z}","{zoom}")
193 }
194 if (josmUrls.containsKey(url)) {
195 myprintln "+++ JOSM-URL is not unique: "+url
196 } else {
197 josmUrls.put(url, e)
198 }
199 for (def m : e.getMirrors()) {
200 url = getUrl(m)
201 m.origName = m.getOriginalName().replaceAll(" mirror server( \\d+)?","")
202 if (josmUrls.containsKey(url)) {
203 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
204 } else {
205 josmUrls.put(url, m)
206 josmMirrors.put(url, m)
207 }
208 }
209 }
210 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
211 }
212
213 List inOneButNotTheOther(Map m1, Map m2) {
214 def l = []
215 for (def url : m1.keySet()) {
216 if (!m2.containsKey(url)) {
217 def name = getName(m1.get(url))
218 l += " "+getDescription(m1.get(url))
219 }
220 }
221 l.sort()
222 }
223
224 void checkInOneButNotTheOther() {
225 def l1 = inOneButNotTheOther(eliUrls, josmUrls)
226 myprintln "*** URLs found in ELI but not in JOSM (${l1.size()}): ***"
227 if (!l1.isEmpty()) {
228 for (def l : l1) {
229 myprintln "-" + l
230 }
231 }
232
233 if (options.nomissingeli)
234 return
235 def l2 = inOneButNotTheOther(josmUrls, eliUrls)
236 myprintln "*** URLs found in JOSM but not in ELI (${l2.size()}): ***"
237 if (!l2.isEmpty()) {
238 for (def l : l2) {
239 myprintln "+" + l
240 }
241 }
242 }
243
244 void checkCommonEntries() {
245 myprintln "*** Same URL, but different name: ***"
246 for (def url : eliUrls.keySet()) {
247 def e = eliUrls.get(url)
248 if (!josmUrls.containsKey(url)) continue
249 def j = josmUrls.get(url)
250 if (!getName(e).equals(getName(j))) {
251 myprintln "* Name differs ('${getName(e)}' != '${getName(j)}'): $url"
252 }
253 }
254
255 myprintln "*** Same URL, but different type: ***"
256 for (def url : eliUrls.keySet()) {
257 def e = eliUrls.get(url)
258 if (!josmUrls.containsKey(url)) continue
259 def j = josmUrls.get(url)
260 if (!getType(e).equals(getType(j))) {
261 myprintln "* Type differs (${getType(e)} != ${getType(j)}): ${getName(j)} - $url"
262 }
263 }
264
265 myprintln "*** Same URL, but different zoom bounds: ***"
266 for (def url : eliUrls.keySet()) {
267 def e = eliUrls.get(url)
268 if (!josmUrls.containsKey(url)) continue
269 def j = josmUrls.get(url)
270
271 Integer eMinZoom = getMinZoom(e)
272 Integer jMinZoom = getMinZoom(j)
273 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
274 myprintln "* Minzoom differs (${eMinZoom} != ${jMinZoom}): ${getDescription(j)}"
275 }
276 Integer eMaxZoom = getMaxZoom(e)
277 Integer jMaxZoom = getMaxZoom(j)
278 if (eMaxZoom != jMaxZoom) {
279 myprintln "* Maxzoom differs (${eMaxZoom} != ${jMaxZoom}): ${getDescription(j)}"
280 }
281 }
282
283 myprintln "*** Same URL, but different country code: ***"
284 for (def url : eliUrls.keySet()) {
285 def e = eliUrls.get(url)
286 if (!josmUrls.containsKey(url)) continue
287 def j = josmUrls.get(url)
288 if (!getCountryCode(e).equals(getCountryCode(j))) {
289 myprintln "* Country code differs (${getCountryCode(e)} != ${getCountryCode(j)}): ${getDescription(j)}"
290 }
291 }
292 myprintln "*** Same URL, but different quality: ***"
293 for (def url : eliUrls.keySet()) {
294 def e = eliUrls.get(url)
295 if (!josmUrls.containsKey(url)) {
296 def q = getQuality(e)
297 if("eli-best".equals(q)) {
298 myprintln "- Quality best entry not in JOSM for ${getDescription(e)}"
299 }
300 continue
301 }
302 def j = josmUrls.get(url)
303 if (!getQuality(e).equals(getQuality(j))) {
304 myprintln "* Quality differs (${getQuality(e)} != ${getQuality(j)}): ${getDescription(j)}"
305 }
306 }
307 myprintln "*** Same URL, but different dates: ***"
308 for (def url : eliUrls.keySet()) {
309 def ed = getDate(eliUrls.get(url))
310 if (!josmUrls.containsKey(url)) continue
311 def j = josmUrls.get(url)
312 def jd = getDate(j)
313 // The forms 2015;- or -;2015 or 2015;2015 are handled equal to 2015
314 String ef = ed.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1");
315 // ELI has a strange and inconsistent used end_date definition, so we try again with subtraction by one
316 String ed2 = ed;
317 def reg = (ed =~ /^(.*;)(\d\d\d\d)(-(\d\d)(-(\d\d))?)?$/)
318 if(reg != null && reg.count == 1) {
319 Calendar cal = Calendar.getInstance();
320 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)
321 cal.add(Calendar.DAY_OF_MONTH, -1)
322 ed2 = reg[0][1] + cal.get(Calendar.YEAR)
323 if (reg[0][4] != null)
324 ed2 += "-" + String.format("%02d", cal.get(Calendar.MONTH)+1)
325 if (reg[0][6] != null)
326 ed2 += "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))
327 }
328 String ef2 = ed2.replaceAll("\\A-;","").replaceAll(";-\\z","").replaceAll("\\A([0-9-]+);\\1\\z","\$1");
329 if (!ed.equals(jd) && !ef.equals(jd) && !ed2.equals(jd) && !ef2.equals(jd)) {
330 String t = "'${ed}'";
331 if (!ed.equals(ef)) {
332 t += " or '${ef}'";
333 }
334 if (jd.isEmpty()) {
335 myprintln "- Missing JOSM date (${t}): ${getDescription(j)}"
336 } else if (!ed.isEmpty()) {
337 myprintln "* Date differs (${t} != '${jd}'): ${getDescription(j)}"
338 } else if (!options.nomissingeli) {
339 myprintln "+ Missing ELI date ('${jd}'): ${getDescription(j)}"
340 }
341 }
342 }
343 myprintln "*** Mismatching shapes: ***"
344 for (def url : josmUrls.keySet()) {
345 def j = josmUrls.get(url)
346 def num = 1
347 for (def shape : getShapes(j)) {
348 def p = shape.getPoints()
349 if(!p[0].equals(p[p.size()-1])) {
350 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
351 }
352 ++num
353 }
354 }
355 for (def url : eliUrls.keySet()) {
356 def e = eliUrls.get(url)
357 def num = 1
358 def s = getShapes(e)
359 for (def shape : s) {
360 def p = shape.getPoints()
361 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeli) {
362 myprintln "+++ ELI shape $num unclosed: ${getDescription(e)}"
363 }
364 ++num
365 }
366 if (!josmUrls.containsKey(url)) {
367 continue
368 }
369 def j = josmUrls.get(url)
370 def js = getShapes(j)
371 if(!s.size() && js.size()) {
372 if(!options.nomissingeli) {
373 myprintln "+ No ELI shape: ${getDescription(j)}"
374 }
375 } else if(!js.size() && s.size()) {
376 // don't report boundary like 5 point shapes as difference
377 if (s.size() != 1 || s[0].getPoints().size() != 5) {
378 myprintln "- No JOSM shape: ${getDescription(j)}"
379 }
380 } else if(s.size() != js.size()) {
381 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
382 } else {
383 for(def nums = 0; nums < s.size(); ++nums) {
384 def ep = s[nums].getPoints()
385 def jp = js[nums].getPoints()
386 if(ep.size() != jp.size()) {
387 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
388 } else {
389 for(def nump = 0; nump < ep.size(); ++nump) {
390 def ept = ep[nump]
391 def jpt = jp[nump]
392 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
393 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
394 nump = ep.size()
395 num = s.size()
396 }
397 }
398 }
399 }
400 }
401 }
402 myprintln "*** Mismatching icons: ***"
403 for (def url : eliUrls.keySet()) {
404 def e = eliUrls.get(url)
405 if (!josmUrls.containsKey(url)) {
406 continue
407 }
408 def j = josmUrls.get(url)
409 def ij = getIcon(j)
410 def ie = getIcon(e)
411 if(ij != null && ie == null) {
412 if(!options.nomissingeli) {
413 myprintln "+ No ELI icon: ${getDescription(j)}"
414 }
415 } else if(ij == null && ie != null) {
416 myprintln "- No JOSM icon: ${getDescription(j)}"
417 } else if(!ij.equals(ie)) {
418 myprintln "* Different icons: ${getDescription(j)}"
419 }
420 }
421 myprintln "*** Miscellaneous checks: ***"
422 def josmIds = new HashMap<String, ImageryInfo>()
423 for (def url : josmUrls.keySet()) {
424 def j = josmUrls.get(url)
425 def id = getId(j)
426 if(josmMirrors.containsKey(url)) {
427 continue;
428 }
429 if(id == null) {
430 myprintln "* No JOSM-ID: ${getDescription(j)}"
431 } else if(josmIds.containsKey(id)) {
432 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
433 } else {
434 josmIds.put(id, j);
435 }
436 def d = getDate(j)
437 if(!d.isEmpty()) {
438 def reg = (d =~ /^(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?)(;(-|(\d\d\d\d)(-(\d\d)(-(\d\d))?)?))?$/)
439 if(reg == null || reg.count != 1) {
440 myprintln "* JOSM-Date '${d}' is strange: ${getDescription(j)}"
441 } else {
442 try {
443 def first = verifyDate(reg[0][2],reg[0][4],reg[0][6]);
444 def second = verifyDate(reg[0][9],reg[0][11],reg[0][13]);
445 if(second.compareTo(first) < 0) {
446 myprintln "* JOSM-Date '${d}' is strange (second earlier than first): ${getDescription(j)}"
447 }
448 }
449 catch (Exception e) {
450 myprintln "* JOSM-Date '${d}' is strange (${e.getMessage()}): ${getDescription(j)}"
451 }
452 }
453 }
454 def js = getShapes(j)
455 if(js.size()) {
456 def minlat = 1000;
457 def minlon = 1000;
458 def maxlat = -1000;
459 def maxlon = -1000;
460 for(def s: js) {
461 for(def p: s.getPoints()) {
462 def lat = p.getLat();
463 def lon = p.getLon();
464 if(lat > maxlat) maxlat = lat;
465 if(lon > maxlon) maxlon = lon;
466 if(lat < minlat) minlat = lat;
467 if(lon < minlon) minlon = lon;
468 }
469 }
470 def b = j.getBounds();
471 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
472 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)}"
473 }
474 }
475 }
476 }
477
478 /**
479 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
480 */
481 static String getUrl(Object e) {
482 if (e instanceof ImageryInfo) return e.url
483 return e.get("properties").getString("url")
484 }
485 static String getDate(Object e) {
486 if (e instanceof ImageryInfo) return e.date ? e.date : ""
487 def p = e.get("properties")
488 def start = p.containsKey("start_date") ? p.getString("start_date") : ""
489 def end = p.containsKey("end_date") ? p.getString("end_date") : ""
490 if(!start.isEmpty() && !end.isEmpty())
491 return start+";"+end
492 else if(!start.isEmpty())
493 return start+";-"
494 else if(!end.isEmpty())
495 return "-;"+end
496 return "";
497 }
498 static Date verifyDate(String year, String month, String day) {
499 def date
500 if(year == null)
501 date = "3000-01-01"
502 else
503 date = year + "-" + (month == null ? "01" : month) + "-" + (day == null ? "01" : day)
504 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
505 df.setLenient(false)
506 return df.parse(date)
507 }
508 static String getId(Object e) {
509 if (e instanceof ImageryInfo) return e.getId()
510 return e.get("properties").getString("id")
511 }
512 static String getName(Object e) {
513 if (e instanceof ImageryInfo) return e.getOriginalName()
514 return e.get("properties").getString("name")
515 }
516 static List<Shape> getShapes(Object e) {
517 if (e instanceof ImageryInfo) {
518 def bounds = e.getBounds();
519 if(bounds != null) {
520 return bounds.getShapes();
521 }
522 return []
523 }
524 if(!e.isNull("geometry")) {
525 def ex = e.get("geometry")
526 if(ex != null && !ex.isNull("coordinates")) {
527 def poly = ex.get("coordinates")
528 List<Shape> l = []
529 for(def shapes: poly) {
530 def s = new Shape()
531 for(def point: shapes) {
532 def lon = point[0].toString()
533 def lat = point[1].toString()
534 s.addPoint(lat, lon)
535 }
536 l.add(s)
537 }
538 return l
539 }
540 }
541 return []
542 }
543 static String getType(Object e) {
544 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
545 return e.get("properties").getString("type")
546 }
547 static Integer getMinZoom(Object e) {
548 if (e instanceof ImageryInfo) {
549 int mz = e.getMinZoom()
550 return mz == 0 ? null : mz
551 } else {
552 def num = e.get("properties").getJsonNumber("min_zoom")
553 if (num == null) return null
554 return num.intValue()
555 }
556 }
557 static Integer getMaxZoom(Object e) {
558 if (e instanceof ImageryInfo) {
559 int mz = e.getMaxZoom()
560 return mz == 0 ? null : mz
561 } else {
562 def num = e.get("properties").getJsonNumber("max_zoom")
563 if (num == null) return null
564 return num.intValue()
565 }
566 }
567 static String getCountryCode(Object e) {
568 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
569 return e.get("properties").getString("country_code", null)
570 }
571 static String getQuality(Object e) {
572 if (e instanceof ImageryInfo) return e.isBestMarked() ? "eli-best" : null
573 return (e.get("properties").containsKey("best")
574 && e.get("properties").getBoolean("best")) ? "eli-best" : null
575 }
576 static String getIcon(Object e) {
577 if (e instanceof ImageryInfo) return e.getIcon()
578 return e.get("properties").getString("icon", null)
579 }
580 String getDescription(Object o) {
581 def url = getUrl(o)
582 def cc = getCountryCode(o)
583 if (cc == null) {
584 def j = josmUrls.get(url)
585 if (j != null) cc = getCountryCode(j)
586 if (cc == null) {
587 def e = eliUrls.get(url)
588 if (e != null) cc = getCountryCode(e)
589 }
590 }
591 if (cc == null) {
592 cc = ''
593 } else {
594 cc = "[$cc] "
595 }
596 def d = cc + getName(o) + " - " + getUrl(o)
597 if (options.shorten) {
598 def MAXLEN = 140
599 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
600 }
601 return d
602 }
603}
Note: See TracBrowser for help on using the repository browser.