source: josm/trunk/scripts/SyncEditorImageryIndex.groovy@ 11573

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

fix imagery compare date check, add (disabled) date compare

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