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 | */ |
---|
19 | import javax.json.Json |
---|
20 | import javax.json.JsonArray |
---|
21 | import javax.json.JsonObject |
---|
22 | import javax.json.JsonReader |
---|
23 | import javax.json.JsonValue |
---|
24 | |
---|
25 | import org.openstreetmap.josm.data.imagery.ImageryInfo |
---|
26 | import org.openstreetmap.josm.data.imagery.Shape |
---|
27 | import org.openstreetmap.josm.io.imagery.ImageryReader |
---|
28 | |
---|
29 | class 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("&","&").replaceAll("<","<").replaceAll(">",">")+"</pre>" |
---|
147 | } |
---|
148 | if (!options.noskip) { |
---|
149 | return; |
---|
150 | } |
---|
151 | } else if(options.xhtmlbody || options.xhtml) { |
---|
152 | String color = s.startsWith("***") ? "black" : (s.startsWith("+ ") ? "blue" : "red") |
---|
153 | s = "<pre style=\"margin:3px;color:"+color+"\">"+s.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")+"</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])) { |
---|
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.equals(js)) { |
---|
357 | if(!s.size()) { |
---|
358 | myprintln " No EII shape: ${getDescription(j)}" |
---|
359 | } else if(!js.size()) { |
---|
360 | myprintln " No JOSM shape: ${getDescription(j)}" |
---|
361 | } else { |
---|
362 | myprintln " Different shapes: ${getDescription(j)}" |
---|
363 | } |
---|
364 | } |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * Utility functions that allow uniform access for both ImageryInfo and JsonObject. |
---|
370 | */ |
---|
371 | static String getUrl(Object e) { |
---|
372 | if (e instanceof ImageryInfo) return e.url |
---|
373 | return e.get("properties").getString("url") |
---|
374 | } |
---|
375 | static String getName(Object e) { |
---|
376 | if (e instanceof ImageryInfo) return e.getOriginalName() |
---|
377 | return e.get("properties").getString("name") |
---|
378 | } |
---|
379 | static List<Shape> getShapes(Object e) { |
---|
380 | if (e instanceof ImageryInfo) { |
---|
381 | def bounds = e.getBounds(); |
---|
382 | if(bounds != null) { |
---|
383 | return bounds.getShapes(); |
---|
384 | } |
---|
385 | return [] |
---|
386 | } |
---|
387 | if(!e.isNull("geometry")) { |
---|
388 | def ex = e.get("geometry") |
---|
389 | if(ex != null && !ex.isNull("coordinates")) { |
---|
390 | def poly = ex.get("coordinates") |
---|
391 | List<Shape> l = [] |
---|
392 | for(def shapes: poly) { |
---|
393 | def s = new Shape() |
---|
394 | for(def point: shapes) { |
---|
395 | def lon = point[0].toString() |
---|
396 | def lat = point[1].toString() |
---|
397 | s.addPoint(lat, lon) |
---|
398 | } |
---|
399 | l.add(s) |
---|
400 | } |
---|
401 | if (l.size() == 1 && l[0].getPoints().size() == 5) { |
---|
402 | return [] // ignore a bounds equivalent shape |
---|
403 | } |
---|
404 | return l |
---|
405 | } |
---|
406 | } |
---|
407 | return [] |
---|
408 | } |
---|
409 | static String getType(Object e) { |
---|
410 | if (e instanceof ImageryInfo) return e.getImageryType().getTypeString() |
---|
411 | return e.get("properties").getString("type") |
---|
412 | } |
---|
413 | static Integer getMinZoom(Object e) { |
---|
414 | if (e instanceof ImageryInfo) { |
---|
415 | int mz = e.getMinZoom() |
---|
416 | return mz == 0 ? null : mz |
---|
417 | } else { |
---|
418 | def num = e.get("properties").getJsonNumber("min_zoom") |
---|
419 | if (num == null) return null |
---|
420 | return num.intValue() |
---|
421 | } |
---|
422 | } |
---|
423 | static Integer getMaxZoom(Object e) { |
---|
424 | if (e instanceof ImageryInfo) { |
---|
425 | int mz = e.getMaxZoom() |
---|
426 | return mz == 0 ? null : mz |
---|
427 | } else { |
---|
428 | def num = e.get("properties").getJsonNumber("max_zoom") |
---|
429 | if (num == null) return null |
---|
430 | return num.intValue() |
---|
431 | } |
---|
432 | } |
---|
433 | static String getCountryCode(Object e) { |
---|
434 | if (e instanceof ImageryInfo) return "".equals(e.getCountryCode()) ? null : e.getCountryCode() |
---|
435 | return e.get("properties").getString("country_code", null) |
---|
436 | } |
---|
437 | static String getQuality(Object e) { |
---|
438 | //if (e instanceof ImageryInfo) return "".equals(e.getQuality()) ? null : e.getQuality() |
---|
439 | if (e instanceof ImageryInfo) return null |
---|
440 | return e.get("properties").get("best") ? "best" : null |
---|
441 | } |
---|
442 | String getDescription(Object o) { |
---|
443 | def url = getUrl(o) |
---|
444 | def cc = getCountryCode(o) |
---|
445 | if (cc == null) { |
---|
446 | def j = josmUrls.get(url) |
---|
447 | if (j != null) cc = getCountryCode(j) |
---|
448 | if (cc == null) { |
---|
449 | def e = eiiUrls.get(url) |
---|
450 | if (e != null) cc = getCountryCode(e) |
---|
451 | } |
---|
452 | } |
---|
453 | if (cc == null) { |
---|
454 | cc = '' |
---|
455 | } else { |
---|
456 | cc = "[$cc] " |
---|
457 | } |
---|
458 | def d = cc + getName(o) + " - " + getUrl(o) |
---|
459 | if (options.shorten) { |
---|
460 | def MAXLEN = 140 |
---|
461 | if (d.length() > MAXLEN) d = d.substring(0, MAXLEN-1) + "..." |
---|
462 | } |
---|
463 | return d |
---|
464 | } |
---|
465 | } |
---|