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

Last change on this file since 11415 was 11415, checked in by stoecker, 9 years ago

see #12706 - fix 5 point shape handling

  • Property svn:eol-style set to native
File size: 17.5 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>()
[9667]36
[11412]37 static String eiiInputFile = 'imagery.geojson'
[7726]38 static String josmInputFile = 'maps.xml'
[11238]39 static String ignoreInputFile = 'maps_ignores.txt'
[9505]40 static FileWriter outputFile = null
41 static BufferedWriter outputStream = null
[10515]42 int skipCount = 0;
[11238]43 String skipColor = "greenyellow" // should never be visible
[10515]44 def skipEntries = [:]
[11238]45 def skipColors = [:]
[9505]46
[7726]47 static def options
[9658]48
[7726]49 /**
50 * Main method.
51 */
52 static main(def args) {
53 parse_command_line_arguments(args)
[9880]54 def script = new SyncEditorImageryIndex()
[9505]55 script.loadSkip()
[9658]56 script.start()
[7726]57 script.loadJosmEntries()
58 script.loadEIIEntries()
59 script.checkInOneButNotTheOther()
60 script.checkCommonEntries()
[9658]61 script.end()
[9505]62 if(outputStream != null) {
63 outputStream.close();
64 }
65 if(outputFile != null) {
66 outputFile.close();
67 }
[7726]68 }
[9653]69
[7726]70 /**
71 * Parse command line arguments.
72 */
73 static void parse_command_line_arguments(args) {
[9658]74 def cli = new CliBuilder(width: 160)
[9505]75 cli.o(longOpt:'output', args:1, argName: "output", "Output file, - prints to stdout (default: -)")
76 cli.e(longOpt:'eii_input', args:1, argName:"eii_input", "Input file for the editor imagery index (json). Default is $eiiInputFile (current directory).")
77 cli.j(longOpt:'josm_input', args:1, argName:"josm_input", "Input file for the JOSM imagery list (xml). Default is $josmInputFile (current directory).")
[11238]78 cli.i(longOpt:'ignore_input', args:1, argName:"ignore_input", "Input file for the ignore list. Default is $ignoreInputFile (current directory).")
[7726]79 cli.s(longOpt:'shorten', "shorten the output, so it is easier to read in a console window")
[9505]80 cli.n(longOpt:'noskip', argName:"noskip", "don't skip known entries")
[9658]81 cli.x(longOpt:'xhtmlbody', argName:"xhtmlbody", "create XHTML body for display in a web page")
82 cli.X(longOpt:'xhtml', argName:"xhtml", "create XHTML for display in a web page")
[9505]83 cli.m(longOpt:'nomissingeii', argName:"nomissingeii", "don't show missing editor imagery index entries")
[7726]84 cli.h(longOpt:'help', "show this help")
85 options = cli.parse(args)
86
87 if (options.h) {
88 cli.usage()
89 System.exit(0)
90 }
91 if (options.eii_input) {
92 eiiInputFile = options.eii_input
93 }
94 if (options.josm_input) {
95 josmInputFile = options.josm_input
96 }
[11238]97 if (options.ignore_input) {
98 ignoreInputFile = options.ignore_input
99 }
[9505]100 if (options.output && options.output != "-") {
101 outputFile = new FileWriter(options.output)
102 outputStream = new BufferedWriter(outputFile)
103 }
[7726]104 }
105
[9505]106 void loadSkip() {
[11238]107 FileReader fr = new FileReader(ignoreInputFile)
108 def line
109
110 while((line = fr.readLine()) != null) {
[11242]111 def res = (line =~ /^\|\| *(\d) *\|\| *(EII|Ignore) *\|\| *\{\{\{(.+)\}\}\} *\|\|/)
[11238]112 if(res.count)
113 {
114 skipEntries[res[0][3]] = res[0][1] as int
115 if(res[0][2].equals("Ignore")) {
116 skipColors[res[0][3]] = "green"
117 } else {
[11245]118 skipColors[res[0][3]] = "darkgoldenrod"
[11238]119 }
120 }
[11234]121 }
[11238]122 }
[9653]123
[9658]124 void myprintlnfinal(String s) {
125 if(outputStream != null) {
126 outputStream.write(s);
127 outputStream.newLine();
128 } else {
129 println s;
130 }
131 }
132
[9505]133 void myprintln(String s) {
134 if(skipEntries.containsKey(s)) {
135 skipCount = skipEntries.get(s)
[9658]136 skipEntries.remove(s)
[11238]137 if(skipColors.containsKey(s)) {
138 skipColor = skipColors.get(s)
139 } else {
140 skipColor = "greenyellow"
141 }
[9505]142 }
143 if(skipCount) {
144 skipCount -= 1;
[9658]145 if(options.xhtmlbody || options.xhtml) {
[11238]146 s = "<pre style=\"margin:3px;color:"+skipColor+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9658]147 }
[9662]148 if (!options.noskip) {
149 return;
150 }
[9658]151 } else if(options.xhtmlbody || options.xhtml) {
[11413]152 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ EII")) ? "blue" : "red")
[9658]153 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
[9505]154 }
[9658]155 myprintlnfinal(s)
156 }
157
158 void start() {
159 if (options.xhtml) {
160 myprintlnfinal "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
161 myprintlnfinal "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/><title>JOSM - EII differences</title></head><body>\n"
[9505]162 }
163 }
[9653]164
[9658]165 void end() {
166 for (def s: skipEntries.keySet()) {
167 myprintln "+++ Obsolete skip entry: " + s
168 }
169 if (options.xhtml) {
170 myprintlnfinal "</body></html>\n"
171 }
172 }
173
[7726]174 void loadEIIEntries() {
175 FileReader fr = new FileReader(eiiInputFile)
176 JsonReader jr = Json.createReader(fr)
[11412]177 eiiEntries = jr.readObject().get("features")
[7726]178 jr.close()
[9653]179
[7726]180 for (def e : eiiEntries) {
181 def url = getUrl(e)
[9653]182 if (url.contains("{z}")) {
183 myprintln "+++ EII-URL uses {z} instead of {zoom}: "+url
184 url = url.replace("{z}","{zoom}")
185 }
[9505]186 if (eiiUrls.containsKey(url)) {
187 myprintln "+++ EII-URL is not unique: "+url
188 } else {
189 eiiUrls.put(url, e)
190 }
[7726]191 }
[9505]192 myprintln "*** Loaded ${eiiEntries.size()} entries (EII). ***"
[7726]193 }
194
195 void loadJosmEntries() {
196 def reader = new ImageryReader(josmInputFile)
197 josmEntries = reader.parse()
[9667]198
[7726]199 for (def e : josmEntries) {
200 def url = getUrl(e)
[9658]201 if (url.contains("{z}")) {
202 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
203 url = url.replace("{z}","{zoom}")
204 }
[9505]205 if (josmUrls.containsKey(url)) {
206 myprintln "+++ JOSM-URL is not unique: "+url
207 } else {
208 josmUrls.put(url, e)
[7726]209 }
[9658]210 for (def m : e.getMirrors()) {
211 url = getUrl(m)
212 if (josmUrls.containsKey(url)) {
213 myprintln "+++ JOSM-Mirror-URL is not unique: "+url
214 } else {
215 josmUrls.put(url, m)
216 }
217 }
[7726]218 }
[9505]219 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
[7726]220 }
221
222 List inOneButNotTheOther(Map m1, Map m2) {
223 def l = []
224 for (def url : m1.keySet()) {
225 if (!m2.containsKey(url)) {
226 def name = getName(m1.get(url))
227 l += " "+getDescription(m1.get(url))
228 }
229 }
230 l.sort()
231 }
[9667]232
[7726]233 void checkInOneButNotTheOther() {
234 def l1 = inOneButNotTheOther(eiiUrls, josmUrls)
[9505]235 myprintln "*** URLs found in EII but not in JOSM (${l1.size()}): ***"
[9658]236 if (!l1.isEmpty()) {
[11412]237 for (def l : l1) {
238 myprintln "-" + l
239 }
[7726]240 }
241
[9505]242 if (options.nomissingeii)
243 return
[7726]244 def l2 = inOneButNotTheOther(josmUrls, eiiUrls)
[9505]245 myprintln "*** URLs found in JOSM but not in EII (${l2.size()}): ***"
[9658]246 if (!l2.isEmpty()) {
[11412]247 for (def l : l2) {
[9658]248 myprintln "+" + l
[11412]249 }
[7726]250 }
251 }
[9667]252
[7726]253 void checkCommonEntries() {
[9505]254 myprintln "*** Same URL, but different name: ***"
[7726]255 for (def url : eiiUrls.keySet()) {
256 def e = eiiUrls.get(url)
257 if (!josmUrls.containsKey(url)) continue
258 def j = josmUrls.get(url)
259 if (!getName(e).equals(getName(j))) {
[9505]260 myprintln " name differs: $url"
[11246]261 myprintln " (EII): ${getName(e)}"
[9505]262 myprintln " (JOSM): ${getName(j)}"
[7726]263 }
264 }
[9667]265
[9505]266 myprintln "*** Same URL, but different type: ***"
[7726]267 for (def url : eiiUrls.keySet()) {
268 def e = eiiUrls.get(url)
269 if (!josmUrls.containsKey(url)) continue
270 def j = josmUrls.get(url)
271 if (!getType(e).equals(getType(j))) {
[9505]272 myprintln " type differs: ${getName(j)} - $url"
[11246]273 myprintln " (EII): ${getType(e)}"
[9505]274 myprintln " (JOSM): ${getType(j)}"
[7726]275 }
276 }
[9667]277
[9505]278 myprintln "*** Same URL, but different zoom bounds: ***"
[7726]279 for (def url : eiiUrls.keySet()) {
280 def e = eiiUrls.get(url)
281 if (!josmUrls.containsKey(url)) continue
282 def j = josmUrls.get(url)
283
284 Integer eMinZoom = getMinZoom(e)
285 Integer jMinZoom = getMinZoom(j)
[9518]286 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
[9505]287 myprintln " minzoom differs: ${getDescription(j)}"
[11246]288 myprintln " (EII): ${eMinZoom}"
[9505]289 myprintln " (JOSM): ${jMinZoom}"
[7726]290 }
291 Integer eMaxZoom = getMaxZoom(e)
292 Integer jMaxZoom = getMaxZoom(j)
293 if (eMaxZoom != jMaxZoom) {
[9505]294 myprintln " maxzoom differs: ${getDescription(j)}"
[11246]295 myprintln " (EII): ${eMaxZoom}"
[9505]296 myprintln " (JOSM): ${jMaxZoom}"
[7726]297 }
298 }
[9667]299
[9505]300 myprintln "*** Same URL, but different country code: ***"
[7726]301 for (def url : eiiUrls.keySet()) {
302 def e = eiiUrls.get(url)
303 if (!josmUrls.containsKey(url)) continue
304 def j = josmUrls.get(url)
305 if (!getCountryCode(e).equals(getCountryCode(j))) {
[9505]306 myprintln " country code differs: ${getDescription(j)}"
[11246]307 myprintln " (EII): ${getCountryCode(e)}"
[9505]308 myprintln " (JOSM): ${getCountryCode(j)}"
[7726]309 }
310 }
[10077]311 /*myprintln "*** Same URL, but different quality: ***"
[9505]312 for (def url : eiiUrls.keySet()) {
313 def e = eiiUrls.get(url)
[9515]314 if (!josmUrls.containsKey(url)) {
315 def q = getQuality(e)
316 if("best".equals(q)) {
317 myprintln " quality best entry not in JOSM for ${getDescription(e)}"
318 }
319 continue
320 }
[9505]321 def j = josmUrls.get(url)
322 if (!getQuality(e).equals(getQuality(j))) {
323 myprintln " quality differs: ${getDescription(j)}"
[11246]324 myprintln " (EII): ${getQuality(e)}"
[9505]325 myprintln " (JOSM): ${getQuality(j)}"
326 }
[10077]327 }*/
[11410]328 myprintln "*** Mismatching shapes: ***"
329 for (def url : josmUrls.keySet()) {
330 def j = josmUrls.get(url)
331 def num = 1
332 for (def shape : getShapes(j)) {
333 def p = shape.getPoints()
334 if(!p[0].equals(p[p.size()-1])) {
335 myprintln "+++ JOSM shape $num unclosed: ${getDescription(j)}"
336 }
337 ++num
338 }
339 }
340 for (def url : eiiUrls.keySet()) {
341 def e = eiiUrls.get(url)
342 def num = 1
343 def s = getShapes(e)
344 for (def shape : s) {
345 def p = shape.getPoints()
[11413]346 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeii) {
[11410]347 myprintln "+++ EII shape $num unclosed: ${getDescription(e)}"
348 }
349 ++num
350 }
351 if (!josmUrls.containsKey(url)) {
352 continue
353 }
354 def j = josmUrls.get(url)
[11411]355 def js = getShapes(j)
[11414]356 if(!s.size() && js.size()) {
357 if(!options.nomissingeii) {
358 myprintln "+ No EII shape: ${getDescription(j)}"
359 }
[11413]360 } else if(!js.size() && s.size()) {
[11415]361 // don't report boundary like 5 point shapes as difference
362 if (s.size() != 1 || s[0].getPoints().size() != 5) {
363 myprintln "- No JOSM shape: ${getDescription(j)}"
364 }
[11414]365 } else if(s.size() != js.size()) {
366 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
[11413]367 } else {
[11414]368 for(def nums = 0; nums < s.size(); ++nums) {
369 def ep = s[nums].getPoints()
370 def jp = js[nums].getPoints()
371 if(ep.size() != jp.size()) {
372 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
373 } else {
374 for(def nump = 0; nump < ep.size(); ++nump) {
375 def ept = ep[nump]
376 def jpt = jp[nump]
377 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
378 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
379 nump = ep.size()
380 num = s.size()
[11413]381 }
382 }
383 }
[11411]384 }
[11410]385 }
386 }
[7726]387 }
[9667]388
[7726]389 /**
390 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
391 */
392 static String getUrl(Object e) {
393 if (e instanceof ImageryInfo) return e.url
[11412]394 return e.get("properties").getString("url")
[7726]395 }
396 static String getName(Object e) {
[10517]397 if (e instanceof ImageryInfo) return e.getOriginalName()
[11412]398 return e.get("properties").getString("name")
[7726]399 }
[11410]400 static List<Shape> getShapes(Object e) {
401 if (e instanceof ImageryInfo) {
[11411]402 def bounds = e.getBounds();
403 if(bounds != null) {
404 return bounds.getShapes();
405 }
406 return []
[11410]407 }
[11412]408 if(!e.isNull("geometry")) {
409 def ex = e.get("geometry")
410 if(ex != null && !ex.isNull("coordinates")) {
411 def poly = ex.get("coordinates")
[11410]412 List<Shape> l = []
413 for(def shapes: poly) {
414 def s = new Shape()
415 for(def point: shapes) {
416 def lon = point[0].toString()
417 def lat = point[1].toString()
418 s.addPoint(lat, lon)
419 }
420 l.add(s)
421 }
422 return l
423 }
424 }
425 return []
426 }
[7726]427 static String getType(Object e) {
428 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
[11412]429 return e.get("properties").getString("type")
[7726]430 }
431 static Integer getMinZoom(Object e) {
432 if (e instanceof ImageryInfo) {
433 int mz = e.getMinZoom()
434 return mz == 0 ? null : mz
435 } else {
[11412]436 def num = e.get("properties").getJsonNumber("min_zoom")
[7726]437 if (num == null) return null
438 return num.intValue()
439 }
440 }
441 static Integer getMaxZoom(Object e) {
442 if (e instanceof ImageryInfo) {
443 int mz = e.getMaxZoom()
444 return mz == 0 ? null : mz
445 } else {
[11412]446 def num = e.get("properties").getJsonNumber("max_zoom")
[7726]447 if (num == null) return null
448 return num.intValue()
449 }
450 }
451 static String getCountryCode(Object e) {
452 if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode()
[11412]453 return e.get("properties").getString("country_code", null)
[7726]454 }
[9505]455 static String getQuality(Object e) {
456 //if (e instanceof ImageryInfo) return "".equals(e.getQuality()) ? null : e.getQuality()
457 if (e instanceof ImageryInfo) return null
[11412]458 return e.get("properties").get("best") ? "best" : null
[9505]459 }
[7726]460 String getDescription(Object o) {
461 def url = getUrl(o)
462 def cc = getCountryCode(o)
463 if (cc == null) {
464 def j = josmUrls.get(url)
465 if (j != null) cc = getCountryCode(j)
466 if (cc == null) {
467 def e = eiiUrls.get(url)
468 if (e != null) cc = getCountryCode(e)
469 }
470 }
471 if (cc == null) {
472 cc = ''
473 } else {
474 cc = "[$cc] "
475 }
476 def d = cc + getName(o) + " - " + getUrl(o)
477 if (options.shorten) {
478 def MAXLEN = 140
479 if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..."
480 }
481 return d
482 }
483}
Note: See TracBrowser for help on using the repository browser.