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

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

see #12706 - small cleanup

  • Property svn:eol-style set to native
File size: 17.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
37 static String eiiInputFile = '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 int skipCount = 0;
43 String skipColor = "greenyellow" // should never be visible
44 def skipEntries = [:]
45 def skipColors = [:]
46
47 static def options
48
49 /**
50 * Main method.
51 */
52 static main(def args) {
53 parse_command_line_arguments(args)
54 def script = new SyncEditorImageryIndex()
55 script.loadSkip()
56 script.start()
57 script.loadJosmEntries()
58 script.loadEIIEntries()
59 script.checkInOneButNotTheOther()
60 script.checkCommonEntries()
61 script.end()
62 if(outputStream != null) {
63 outputStream.close();
64 }
65 if(outputFile != null) {
66 outputFile.close();
67 }
68 }
69
70 /**
71 * Parse command line arguments.
72 */
73 static void parse_command_line_arguments(args) {
74 def cli = new CliBuilder(width: 160)
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).")
78 cli.i(longOpt:'ignore_input', args:1, argName:"ignore_input", "Input file for the ignore list. Default is $ignoreInputFile (current directory).")
79 cli.s(longOpt:'shorten', "shorten the output, so it is easier to read in a console window")
80 cli.n(longOpt:'noskip', argName:"noskip", "don't skip known entries")
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")
83 cli.m(longOpt:'nomissingeii', argName:"nomissingeii", "don't show missing editor imagery index entries")
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 }
97 if (options.ignore_input) {
98 ignoreInputFile = options.ignore_input
99 }
100 if (options.output && options.output != "-") {
101 outputFile = new FileWriter(options.output)
102 outputStream = new BufferedWriter(outputFile)
103 }
104 }
105
106 void loadSkip() {
107 FileReader fr = new FileReader(ignoreInputFile)
108 def line
109
110 while((line = fr.readLine()) != null) {
111 def res = (line =~ /^\|\| *(\d) *\|\| *(EII|Ignore) *\|\| *\{\{\{(.+)\}\}\} *\|\|/)
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 {
118 skipColors[res[0][3]] = "darkgoldenrod"
119 }
120 }
121 }
122 }
123
124 void myprintlnfinal(String s) {
125 if(outputStream != null) {
126 outputStream.write(s);
127 outputStream.newLine();
128 } else {
129 println s;
130 }
131 }
132
133 void myprintln(String s) {
134 if(skipEntries.containsKey(s)) {
135 skipCount = skipEntries.get(s)
136 skipEntries.remove(s)
137 if(skipColors.containsKey(s)) {
138 skipColor = skipColors.get(s)
139 } else {
140 skipColor = "greenyellow"
141 }
142 }
143 if(skipCount) {
144 skipCount -= 1;
145 if(options.xhtmlbody || options.xhtml) {
146 s = "<pre style=\"margin:3px;color:"+skipColor+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
147 }
148 if (!options.noskip) {
149 return;
150 }
151 } else if(options.xhtmlbody || options.xhtml) {
152 String color = s.startsWith("***") ? "black" : ((s.startsWith("+ ") || s.startsWith("+++ EII")) ? "blue" : "red")
153 s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;")+"</pre>"
154 }
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"
162 }
163 }
164
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
174 void loadEIIEntries() {
175 FileReader fr = new FileReader(eiiInputFile)
176 JsonReader jr = Json.createReader(fr)
177 eiiEntries = jr.readObject().get("features")
178 jr.close()
179
180 for (def e : eiiEntries) {
181 def url = getUrl(e)
182 if (url.contains("{z}")) {
183 myprintln "+++ EII-URL uses {z} instead of {zoom}: "+url
184 url = url.replace("{z}","{zoom}")
185 }
186 if (eiiUrls.containsKey(url)) {
187 myprintln "+++ EII-URL is not unique: "+url
188 } else {
189 eiiUrls.put(url, e)
190 }
191 }
192 myprintln "*** Loaded ${eiiEntries.size()} entries (EII). ***"
193 }
194
195 void loadJosmEntries() {
196 def reader = new ImageryReader(josmInputFile)
197 josmEntries = reader.parse()
198
199 for (def e : josmEntries) {
200 def url = getUrl(e)
201 if (url.contains("{z}")) {
202 myprintln "+++ JOSM-URL uses {z} instead of {zoom}: "+url
203 url = url.replace("{z}","{zoom}")
204 }
205 if (josmUrls.containsKey(url)) {
206 myprintln "+++ JOSM-URL is not unique: "+url
207 } else {
208 josmUrls.put(url, e)
209 }
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 }
218 }
219 myprintln "*** Loaded ${josmEntries.size()} entries (JOSM). ***"
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 }
232
233 void checkInOneButNotTheOther() {
234 def l1 = inOneButNotTheOther(eiiUrls, josmUrls)
235 myprintln "*** URLs found in EII but not in JOSM (${l1.size()}): ***"
236 if (!l1.isEmpty()) {
237 for (def l : l1) {
238 myprintln "-" + l
239 }
240 }
241
242 if (options.nomissingeii)
243 return
244 def l2 = inOneButNotTheOther(josmUrls, eiiUrls)
245 myprintln "*** URLs found in JOSM but not in EII (${l2.size()}): ***"
246 if (!l2.isEmpty()) {
247 for (def l : l2) {
248 myprintln "+" + l
249 }
250 }
251 }
252
253 void checkCommonEntries() {
254 myprintln "*** Same URL, but different name: ***"
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))) {
260 myprintln " name differs: $url"
261 myprintln " (EII): ${getName(e)}"
262 myprintln " (JOSM): ${getName(j)}"
263 }
264 }
265
266 myprintln "*** Same URL, but different type: ***"
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))) {
272 myprintln " type differs: ${getName(j)} - $url"
273 myprintln " (EII): ${getType(e)}"
274 myprintln " (JOSM): ${getType(j)}"
275 }
276 }
277
278 myprintln "*** Same URL, but different zoom bounds: ***"
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)
286 if (eMinZoom != jMinZoom && !(eMinZoom == 0 && jMinZoom == null)) {
287 myprintln " minzoom differs: ${getDescription(j)}"
288 myprintln " (EII): ${eMinZoom}"
289 myprintln " (JOSM): ${jMinZoom}"
290 }
291 Integer eMaxZoom = getMaxZoom(e)
292 Integer jMaxZoom = getMaxZoom(j)
293 if (eMaxZoom != jMaxZoom) {
294 myprintln " maxzoom differs: ${getDescription(j)}"
295 myprintln " (EII): ${eMaxZoom}"
296 myprintln " (JOSM): ${jMaxZoom}"
297 }
298 }
299
300 myprintln "*** Same URL, but different country code: ***"
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))) {
306 myprintln " country code differs: ${getDescription(j)}"
307 myprintln " (EII): ${getCountryCode(e)}"
308 myprintln " (JOSM): ${getCountryCode(j)}"
309 }
310 }
311 /*myprintln "*** Same URL, but different quality: ***"
312 for (def url : eiiUrls.keySet()) {
313 def e = eiiUrls.get(url)
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 }
321 def j = josmUrls.get(url)
322 if (!getQuality(e).equals(getQuality(j))) {
323 myprintln " quality differs: ${getDescription(j)}"
324 myprintln " (EII): ${getQuality(e)}"
325 myprintln " (JOSM): ${getQuality(j)}"
326 }
327 }*/
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()
346 if(!p[0].equals(p[p.size()-1]) && !options.nomissingeii) {
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)
355 def js = getShapes(j)
356 if(!s.size() && js.size()) {
357 if(!options.nomissingeii) {
358 myprintln "+ No EII shape: ${getDescription(j)}"
359 }
360 } else if(!js.size() && s.size()) {
361 myprintln "- No JOSM shape: ${getDescription(j)}"
362 } else if(s.size() != js.size()) {
363 myprintln "* Different number of shapes (${s.size()} != ${js.size()}): ${getDescription(j)}"
364 } else {
365 for(def nums = 0; nums < s.size(); ++nums) {
366 def ep = s[nums].getPoints()
367 def jp = js[nums].getPoints()
368 if(ep.size() != jp.size()) {
369 myprintln "* Different number of points for shape ${nums+1} (${ep.size()} ! = ${jp.size()})): ${getDescription(j)}"
370 } else {
371 for(def nump = 0; nump < ep.size(); ++nump) {
372 def ept = ep[nump]
373 def jpt = jp[nump]
374 if(Math.abs(ept.getLat()-jpt.getLat()) > 0.000001 || Math.abs(ept.getLon()-jpt.getLon()) > 0.000001) {
375 myprintln "* Different coordinate for point ${nump+1} of shape ${nums+1}: ${getDescription(j)}"
376 nump = ep.size()
377 num = s.size()
378 }
379 }
380 }
381 }
382 }
383 }
384 }
385
386 /**
387 * Utility functions that allow uniform access for both ImageryInfo and JsonObject.
388 */
389 static String getUrl(Object e) {
390 if (e instanceof ImageryInfo) return e.url
391 return e.get("properties").getString("url")
392 }
393 static String getName(Object e) {
394 if (e instanceof ImageryInfo) return e.getOriginalName()
395 return e.get("properties").getString("name")
396 }
397 static List<Shape> getShapes(Object e) {
398 if (e instanceof ImageryInfo) {
399 def bounds = e.getBounds();
400 if(bounds != null) {
401 return bounds.getShapes();
402 }
403 return []
404 }
405 if(!e.isNull("geometry")) {
406 def ex = e.get("geometry")
407 if(ex != null && !ex.isNull("coordinates")) {
408 def poly = ex.get("coordinates")
409 List<Shape> l = []
410 for(def shapes: poly) {
411 def s = new Shape()
412 for(def point: shapes) {
413 def lon = point[0].toString()
414 def lat = point[1].toString()
415 s.addPoint(lat, lon)
416 }
417 l.add(s)
418 }
419 if (l.size() == 1 && l[0].getPoints().size() == 5) {
420 return [] // ignore a bounds equivalent shape
421 }
422 return l
423 }
424 }
425 return []
426 }
427 static String getType(Object e) {
428 if (e instanceof ImageryInfo) return e.getImageryType().getTypeString()
429 return e.get("properties").getString("type")
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 {
436 def num = e.get("properties").getJsonNumber("min_zoom")
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 {
446 def num = e.get("properties").getJsonNumber("max_zoom")
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()
453 return e.get("properties").getString("country_code", null)
454 }
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
458 return e.get("properties").get("best") ? "best" : null
459 }
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.