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

Last change on this file since 11491 was 11423, checked in by stoecker, 8 years ago

see #12706 - add bounds check - use single quote

  • Property svn:eol-style set to native
File size: 20.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 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 if (josmUrls.containsKey(url)) {
214 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
215 } else {
216 josmUrls.put(url, m)
217 josmMirrors.put(url, m)
218 }
219 }
220 }
221 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
222 }
223
224 List inOneButNotTheOther(Map m1, Map m2) {
225 def l = []
226 for (def url : m1.keySet()) {
227 if (!m2.containsKey(url)) {
228 def name = getName(m1.get(url))
229 l += " "+getDescription(m1.get(url))
230 }
231 }
232 l.sort()
233 }
234
235 void checkInOneButNotTheOther() {
236 def l1 = inOneButNotTheOther(eiiUrls, josmUrls)
237 myprintln "*** URLs found in EII but not in JOSM (${l1.size()}): ***"
238 if (!l1.isEmpty()) {
239 for (def l : l1) {
240 myprintln "-" + l
241 }
242 }
243
244 if (options.nomissingeii)
245 return
246 def l2 = inOneButNotTheOther(josmUrls, eiiUrls)
247 myprintln "*** URLs found in JOSM but not in EII (${l2.size()}): ***"
248 if (!l2.isEmpty()) {
249 for (def l : l2) {
250 myprintln "+" + l
251 }
252 }
253 }
254
255 void checkCommonEntries() {
256 myprintln "*** Same URL, but different name: ***"
257 for (def url : eiiUrls.keySet()) {
258 def e = eiiUrls.get(url)
259 if (!josmUrls.containsKey(url)) continue
260 def j = josmUrls.get(url)
261 if (!getName(e).equals(getName(j))) {
262 myprintln " name differs: $url"
263 myprintln " (EII): ${getName(e)}"
264 myprintln " (JOSM): ${getName(j)}"
265 }
266 }
267
268 myprintln "*** Same URL, but different type: ***"
269 for (def url : eiiUrls.keySet()) {
270 def e = eiiUrls.get(url)
271 if (!josmUrls.containsKey(url)) continue
272 def j = josmUrls.get(url)
273 if (!getType(e).equals(getType(j))) {
274 myprintln " type differs: ${getName(j)} - $url"
275 myprintln " (EII): ${getType(e)}"
276 myprintln " (JOSM): ${getType(j)}"
277 }
278 }
279
280 myprintln "*** Same URL, but different zoom bounds: ***"
281 for (def url : eiiUrls.keySet()) {
282 def e = eiiUrls.get(url)
283 if (!josmUrls.containsKey(url)) continue
284 def j = josmUrls.get(url)
285
286 Integer eMinZoom = getMinZoom(e)
287 Integer jMinZoom = getMinZoom(j)
288 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
289 myprintln " minzoom differs: ${getDescription(j)}"
290 myprintln " (EII): ${eMinZoom}"
291 myprintln " (JOSM): ${jMinZoom}"
292 }
293 Integer eMaxZoom = getMaxZoom(e)
294 Integer jMaxZoom = getMaxZoom(j)
295 if (eMaxZoom != jMaxZoom) {
296 myprintln " maxzoom differs: ${getDescription(j)}"
297 myprintln " (EII): ${eMaxZoom}"
298 myprintln " (JOSM): ${jMaxZoom}"
299 }
300 }
301
302 myprintln "*** Same URL, but different country code: ***"
303 for (def url : eiiUrls.keySet()) {
304 def e = eiiUrls.get(url)
305 if (!josmUrls.containsKey(url)) continue
306 def j = josmUrls.get(url)
307 if (!getCountryCode(e).equals(getCountryCode(j))) {
308 myprintln " country code differs: ${getDescription(j)}"
309 myprintln " (EII): ${getCountryCode(e)}"
310 myprintln " (JOSM): ${getCountryCode(j)}"
311 }
312 }
313 /*myprintln "*** Same URL, but different quality: ***"
314 for (def url : eiiUrls.keySet()) {
315 def e = eiiUrls.get(url)
316 if (!josmUrls.containsKey(url)) {
317 def q = getQuality(e)
318 if("best".equals(q)) {
319 myprintln " quality best entry not in JOSM for ${getDescription(e)}"
320 }
321 continue
322 }
323 def j = josmUrls.get(url)
324 if (!getQuality(e).equals(getQuality(j))) {
325 myprintln " quality differs: ${getDescription(j)}"
326 myprintln " (EII): ${getQuality(e)}"
327 myprintln " (JOSM): ${getQuality(j)}"
328 }
329 }*/
330 myprintln "*** Mismatching shapes: ***"
331 for (def url : josmUrls.keySet()) {
332 def j = josmUrls.get(url)
333 def num = 1
334 for (def shape : getShapes(j)) {
335 def p = shape.getPoints()
336 if(!p[0].equals(p[p.size()-1])) {
337 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
338 }
339 ++num
340 }
341 }
342 for (def url : eiiUrls.keySet()) {
343 def e = eiiUrls.get(url)
344 def num = 1
345 def s = getShapes(e)
346 for (def shape : s) {
347 def p = shape.getPoints()
348 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeii) {
349 myprintln "+++ EII shape $num unclosed: ${getDescription(e)}"
350 }
351 ++num
352 }
353 if (!josmUrls.containsKey(url)) {
354 continue
355 }
356 def j = josmUrls.get(url)
357 def js = getShapes(j)
358 if(!s.size() && js.size()) {
359 if(!options.nomissingeii) {
360 myprintln "+ No EII shape: ${getDescription(j)}"
361 }
362 } else if(!js.size() && s.size()) {
363 // don't report boundary like 5 point shapes as difference
364 if (s.size() != 1 || s[0].getPoints().size() != 5) {
365 myprintln "- No JOSM shape: ${getDescription(j)}"
366 }
367 } else if(s.size() != js.size()) {
368 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
369 } else {
370 for(def nums = 0; nums < s.size(); ++nums) {
371 def ep = s[nums].getPoints()
372 def jp = js[nums].getPoints()
373 if(ep.size() != jp.size()) {
374 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
375 } else {
376 for(def nump = 0; nump < ep.size(); ++nump) {
377 def ept = ep[nump]
378 def jpt = jp[nump]
379 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
380 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
381 nump = ep.size()
382 num = s.size()
383 }
384 }
385 }
386 }
387 }
388 }
389 myprintln "*** Mismatching icons: ***"
390 for (def url : eiiUrls.keySet()) {
391 def e = eiiUrls.get(url)
392 if (!josmUrls.containsKey(url)) {
393 continue
394 }
395 def j = josmUrls.get(url)
396 def ij = getIcon(j)
397 def ie = getIcon(e)
398 if(ij != null && ie == null) {
399 if(!options.nomissingeii) {
400 myprintln "+ No EII icon: ${getDescription(j)}"
401 }
402 } else if(ij == null && ie != null) {
403 myprintln "- No JOSM icon: ${getDescription(j)}"
404 } else if(!ij.equals(ie)) {
405 myprintln "* Different icons: ${getDescription(j)}"
406 }
407 }
408 myprintln "*** Miscellaneous checks: ***"
409 def josmIds = new HashMap<String, ImageryInfo>()
410 for (def url : josmUrls.keySet()) {
411 def j = josmUrls.get(url)
412 def id = getId(j)
413 if(josmMirrors.containsKey(url)) {
414 continue;
415 }
416 if(id == null) {
417 myprintln "* No JOSM-ID: ${getDescription(j)}"
418 } else if(josmIds.containsKey(id)) {
419 myprintln "* JOSM-ID ${id} not unique: ${getDescription(j)}"
420 } else {
421 josmIds.put(id, j);
422 }
423 def js = getShapes(j)
424 if(js.size()) {
425 def minlat = 1000;
426 def minlon = 1000;
427 def maxlat = -1000;
428 def maxlon = -1000;
429 for(def s: js) {
430 for(def p: s.getPoints()) {
431 def lat = p.getLat();
432 def lon = p.getLon();
433 if(lat > maxlat) maxlat = lat;
434 if(lon > maxlon) maxlon = lon;
435 if(lat < minlat) minlat = lat;
436 if(lon < minlon) minlon = lon;
437 }
438 }
439 def b = j.getBounds();
440 if(b.getMinLat() != minlat || b.getMinLon() != minlon || b.getMaxLat() != maxlat || b.getMaxLon() != maxlon) {
441 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)}"
442 }
443 }
444 }
445 }
446
447 /**
448 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
449 */
450 static String getUrl(Object e) {
451 if (e instanceof ImageryInfo) return e.url
452 return e.get("properties").getString("url")
453 }
454 static String getId(Object e) {
455 if (e instanceof ImageryInfo) return e.getId()
456 return e.get("properties").getString("id")
457 }
458 static String getName(Object e) {
459 if (e instanceof ImageryInfo) return e.getOriginalName()
460 return e.get("properties").getString("name")
461 }
462 static List<Shape> getShapes(Object e) {
463 if (e instanceof ImageryInfo) {
464 def bounds = e.getBounds();
465 if(bounds != null) {
466 return bounds.getShapes();
467 }
468 return []
469 }
470 if(!e.isNull("geometry")) {
471 def ex = e.get("geometry")
472 if(ex != null && !ex.isNull("coordinates")) {
473 def poly = ex.get("coordinates")
474 List<Shape> l = []
475 for(def shapes: poly) {
476 def s = new Shape()
477 for(def point: shapes) {
478 def lon = point[0].toString()
479 def lat = point[1].toString()
480 s.addPoint(lat, lon)
481 }
482 l.add(s)
483 }
484 return l
485 }
486 }
487 return []
488 }
489 static String getType(Object e) {
490 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
491 return e.get("properties").getString("type")
492 }
493 static Integer getMinZoom(Object e) {
494 if (e instanceof ImageryInfo) {
495 int mz = e.getMinZoom()
496 return mz == 0 ? null : mz
497 } else {
498 def num = e.get("properties").getJsonNumber("min_zoom")
499 if (num == null) return null
500 return num.intValue()
501 }
502 }
503 static Integer getMaxZoom(Object e) {
504 if (e instanceof ImageryInfo) {
505 int mz = e.getMaxZoom()
506 return mz == 0 ? null : mz
507 } else {
508 def num = e.get("properties").getJsonNumber("max_zoom")
509 if (num == null) return null
510 return num.intValue()
511 }
512 }
513 static String getCountryCode(Object e) {
514 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
515 return e.get("properties").getString("country_code", null)
516 }
517 static String getQuality(Object e) {
518 //if (e instanceof ImageryInfo) return "".equals(e.getQuality()) ? null : e.getQuality()
519 if (e instanceof ImageryInfo) return null
520 return e.get("properties").get("best") ? "best" : null
521 }
522 static String getIcon(Object e) {
523 if (e instanceof ImageryInfo) return e.getIcon()
524 return e.get("properties").getString("icon", null)
525 }
526 String getDescription(Object o) {
527 def url = getUrl(o)
528 def cc = getCountryCode(o)
529 if (cc == null) {
530 def j = josmUrls.get(url)
531 if (j != null) cc = getCountryCode(j)
532 if (cc == null) {
533 def e = eiiUrls.get(url)
534 if (e != null) cc = getCountryCode(e)
535 }
536 }
537 if (cc == null) {
538 cc = ''
539 } else {
540 cc = "[$cc] "
541 }
542 def d = cc + getName(o) + " - " + getUrl(o)
543 if (options.shorten) {
544 def MAXLEN = 140
545 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
546 }
547 return d
548 }
549}
Note: See TracBrowser for help on using the repository browser.