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
RevLine 
[7726]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 *
[11260]15 * $ groovy -cp ../dist/josm-custom.jar SyncEditorImageryIndex.groovy
[9667]16 *
[7726]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
[11412]23import javax.json.JsonValue
[7726]24
[9953]25import org.openstreetmap.josm.data.imagery.ImageryInfo
[11410]26import org.openstreetmap.josm.data.imagery.Shape
[7726]27import org.openstreetmap.josm.io.imagery.ImageryReader
28
[9880]29class SyncEditorImageryIndex {
[7726]30
31 List<ImageryInfo> josmEntries;
32 JsonArray eiiEntries;
33
34 def eiiUrls = new HashMap<String, JsonObject>()
35 def josmUrls = new HashMap<String, ImageryInfo>()
[11420]36 def josmMirrors = new HashMap<String, ImageryInfo>()
[9667]37
[11412]38 static String eiiInputFile = 'imagery.geojson'
[7726]39 static String josmInputFile = 'maps.xml'
[11238]40 static String ignoreInputFile = 'maps_ignores.txt'
[9505]41 static FileWriter outputFile = null
42 static BufferedWriter outputStream = null
[10515]43 int skipCount = 0;
[11238]44 String skipColor = "greenyellow" // should never be visible
[10515]45 def skipEntries = [:]
[11238]46 def skipColors = [:]
[9505]47
[7726]48 static def options
[9658]49
[7726]50 /**
51 * Main method.
52 */
53 static main(def args) {
54 parse_command_line_arguments(args)
[9880]55 def script = new SyncEditorImageryIndex()
[9505]56 script.loadSkip()
[9658]57 script.start()
[7726]58 script.loadJosmEntries()
59 script.loadEIIEntries()
60 script.checkInOneButNotTheOther()
61 script.checkCommonEntries()
[9658]62 script.end()
[9505]63 if(outputStream != null) {
64 outputStream.close();
65 }
66 if(outputFile != null) {
67 outputFile.close();
68 }
[7726]69 }
[9653]70
[7726]71 /**
72 * Parse command line arguments.
73 */
74 static void parse_command_line_arguments(args) {
[9658]75 def cli = new CliBuilder(width: 160)
[9505]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).")
[11238]79 cli.i(longOpt:'ignore_input', args:1, argName:"ignore_input", "Input file for the ignore list. Default is $ignoreInputFile (current directory).")
[7726]80 cli.s(longOpt:'shorten', "shorten the output, so it is easier to read in a console window")
[9505]81 cli.n(longOpt:'noskip', argName:"noskip", "don't skip known entries")
[9658]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")
[9505]84 cli.m(longOpt:'nomissingeii', argName:"nomissingeii", "don't show missing editor imagery index entries")
[7726]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 }
[11238]98 if (options.ignore_input) {
99 ignoreInputFile = options.ignore_input
100 }
[9505]101 if (options.output && options.output != "-") {
102 outputFile = new FileWriter(options.output)
103 outputStream = new BufferedWriter(outputFile)
104 }
[7726]105 }
106
[9505]107 void loadSkip() {
[11238]108 FileReader fr = new FileReader(ignoreInputFile)
109 def line
110
111 while((line = fr.readLine()) != null) {
[11242]112 def res = (line =~ /^\|\| *(\d) *\|\| *(EII|Ignore) *\|\| *\{\{\{(.+)\}\}\} *\|\|/)
[11238]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 {
[11245]119 skipColors[res[0][3]] = "darkgoldenrod"
[11238]120 }
121 }
[11234]122 }
[11238]123 }
[9653]124
[9658]125 void myprintlnfinal(String s) {
126 if(outputStream != null) {
127 outputStream.write(s);
128 outputStream.newLine();
129 } else {
130 println s;
131 }
132 }
133
[9505]134 void myprintln(String s) {
135 if(skipEntries.containsKey(s)) {
136 skipCount = skipEntries.get(s)
[9658]137 skipEntries.remove(s)
[11238]138 if(skipColors.containsKey(s)) {
139 skipColor = skipColors.get(s)
140 } else {
141 skipColor = "greenyellow"
142 }
[9505]143 }
144 if(skipCount) {
145 skipCount -= 1;
[9658]146 if(options.xhtmlbody || options.xhtml) {
[11238]147 s = "<pre style=\"margin:3px;color:"+skipColor+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9658]148 }
[9662]149 if (!options.noskip) {
150 return;
151 }
[9658]152 } else if(options.xhtmlbody || options.xhtml) {
[11413]153 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ EII")) ? "blue" : "red")
[9658]154 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9505]155 }
[9658]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"
[9505]163 }
164 }
[9653]165
[9658]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
[7726]175 void loadEIIEntries() {
176 FileReader fr = new FileReader(eiiInputFile)
177 JsonReader jr = Json.createReader(fr)
[11412]178 eiiEntries = jr.readObject().get("features")
[7726]179 jr.close()
[9653]180
[7726]181 for (def e : eiiEntries) {
182 def url = getUrl(e)
[9653]183 if (url.contains("{z}")) {
184 myprintln "+++ EII-URL uses {z} instead of {zoom}: "+url
185 url = url.replace("{z}","{zoom}")
186 }
[9505]187 if (eiiUrls.containsKey(url)) {
188 myprintln "+++ EII-URL is not unique: "+url
189 } else {
190 eiiUrls.put(url, e)
191 }
[7726]192 }
[9505]193 myprintln "*** Loaded ${eiiEntries.size()} entries (EII). ***"
[7726]194 }
195
196 void loadJosmEntries() {
197 def reader = new ImageryReader(josmInputFile)
198 josmEntries = reader.parse()
[9667]199
[7726]200 for (def e : josmEntries) {
201 def url = getUrl(e)
[9658]202 if (url.contains("{z}")) {
203 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
204 url = url.replace("{z}","{zoom}")
205 }
[9505]206 if (josmUrls.containsKey(url)) {
207 myprintln "+++ JOSM-URL is not unique: "+url
208 } else {
[11420]209 josmUrls.put(url, e)
[7726]210 }
[9658]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 {
[11420]216 josmUrls.put(url, m)
217 josmMirrors.put(url, m)
[9658]218 }
219 }
[7726]220 }
[9505]221 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
[7726]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 }
[9667]234
[7726]235 void checkInOneButNotTheOther() {
236 def l1 = inOneButNotTheOther(eiiUrls, josmUrls)
[9505]237 myprintln "*** URLs found in EII but not in JOSM (${l1.size()}): ***"
[9658]238 if (!l1.isEmpty()) {
[11412]239 for (def l : l1) {
240 myprintln "-" + l
241 }
[7726]242 }
243
[9505]244 if (options.nomissingeii)
245 return
[7726]246 def l2 = inOneButNotTheOther(josmUrls, eiiUrls)
[9505]247 myprintln "*** URLs found in JOSM but not in EII (${l2.size()}): ***"
[9658]248 if (!l2.isEmpty()) {
[11412]249 for (def l : l2) {
[9658]250 myprintln "+" + l
[11412]251 }
[7726]252 }
253 }
[9667]254
[7726]255 void checkCommonEntries() {
[9505]256 myprintln "*** Same URL, but different name: ***"
[7726]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))) {
[9505]262 myprintln " name differs: $url"
[11246]263 myprintln " (EII): ${getName(e)}"
[9505]264 myprintln " (JOSM): ${getName(j)}"
[7726]265 }
266 }
[9667]267
[9505]268 myprintln "*** Same URL, but different type: ***"
[7726]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))) {
[9505]274 myprintln " type differs: ${getName(j)} - $url"
[11246]275 myprintln " (EII): ${getType(e)}"
[9505]276 myprintln " (JOSM): ${getType(j)}"
[7726]277 }
278 }
[9667]279
[9505]280 myprintln "*** Same URL, but different zoom bounds: ***"
[7726]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)
[9518]288 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
[9505]289 myprintln " minzoom differs: ${getDescription(j)}"
[11246]290 myprintln " (EII): ${eMinZoom}"
[9505]291 myprintln " (JOSM): ${jMinZoom}"
[7726]292 }
293 Integer eMaxZoom = getMaxZoom(e)
294 Integer jMaxZoom = getMaxZoom(j)
295 if (eMaxZoom != jMaxZoom) {
[9505]296 myprintln " maxzoom differs: ${getDescription(j)}"
[11246]297 myprintln " (EII): ${eMaxZoom}"
[9505]298 myprintln " (JOSM): ${jMaxZoom}"
[7726]299 }
300 }
[9667]301
[9505]302 myprintln "*** Same URL, but different country code: ***"
[7726]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))) {
[9505]308 myprintln " country code differs: ${getDescription(j)}"
[11246]309 myprintln " (EII): ${getCountryCode(e)}"
[9505]310 myprintln " (JOSM): ${getCountryCode(j)}"
[7726]311 }
312 }
[10077]313 /*myprintln "*** Same URL, but different quality: ***"
[9505]314 for (def url : eiiUrls.keySet()) {
315 def e = eiiUrls.get(url)
[9515]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 }
[9505]323 def j = josmUrls.get(url)
324 if (!getQuality(e).equals(getQuality(j))) {
325 myprintln " quality differs: ${getDescription(j)}"
[11246]326 myprintln " (EII): ${getQuality(e)}"
[9505]327 myprintln " (JOSM): ${getQuality(j)}"
328 }
[10077]329 }*/
[11410]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()
[11413]348 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeii) {
[11410]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)
[11411]357 def js = getShapes(j)
[11414]358 if(!s.size() && js.size()) {
359 if(!options.nomissingeii) {
360 myprintln "+ No EII shape: ${getDescription(j)}"
361 }
[11413]362 } else if(!js.size() && s.size()) {
[11415]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 }
[11414]367 } else if(s.size() != js.size()) {
368 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
[11413]369 } else {
[11414]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()
[11413]383 }
384 }
385 }
[11411]386 }
[11410]387 }
388 }
[11420]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 }
[11422]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) {
[11423]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)}"
[11422]442 }
443 }
[11420]444 }
[7726]445 }
[9667]446
[7726]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
[11412]452 return e.get("properties").getString("url")
[7726]453 }
[11420]454 static String getId(Object e) {
455 if (e instanceof ImageryInfo) return e.getId()
456 return e.get("properties").getString("id")
457 }
[7726]458 static String getName(Object e) {
[10517]459 if (e instanceof ImageryInfo) return e.getOriginalName()
[11412]460 return e.get("properties").getString("name")
[7726]461 }
[11410]462 static List<Shape> getShapes(Object e) {
463 if (e instanceof ImageryInfo) {
[11411]464 def bounds = e.getBounds();
465 if(bounds != null) {
466 return bounds.getShapes();
467 }
468 return []
[11410]469 }
[11412]470 if(!e.isNull("geometry")) {
471 def ex = e.get("geometry")
472 if(ex != null && !ex.isNull("coordinates")) {
473 def poly = ex.get("coordinates")
[11410]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 }
[7726]489 static String getType(Object e) {
490 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
[11412]491 return e.get("properties").getString("type")
[7726]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 {
[11412]498 def num = e.get("properties").getJsonNumber("min_zoom")
[7726]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 {
[11412]508 def num = e.get("properties").getJsonNumber("max_zoom")
[7726]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()
[11412]515 return e.get("properties").getString("country_code", null)
[7726]516 }
[9505]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
[11412]520 return e.get("properties").get("best") ? "best" : null
[9505]521 }
[11420]522 static String getIcon(Object e) {
523 if (e instanceof ImageryInfo) return e.getIcon()
524 return e.get("properties").getString("icon", null)
525 }
[7726]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.