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

Last change on this file since 11951 was 11951, checked in by stoecker, 7 years ago

don't print difference for ’ sign

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