1 | // License: GPL. For details, see LICENSE file. |
---|
2 | /** |
---|
3 | * Extracts tag information for the taginfo project. |
---|
4 | * |
---|
5 | * Run from the base directory of a JOSM checkout: |
---|
6 | * |
---|
7 | * groovy -cp dist/josm-custom.jar scripts/taginfoextract.groovy -t mappaint |
---|
8 | * groovy -cp dist/josm-custom.jar scripts/taginfoextract.groovy -t presets |
---|
9 | * groovy -cp dist/josm-custom.jar scripts/taginfoextract.groovy -t external_presets |
---|
10 | */ |
---|
11 | import groovy.json.JsonBuilder |
---|
12 | |
---|
13 | import java.awt.image.BufferedImage |
---|
14 | import java.nio.file.FileSystems |
---|
15 | import java.nio.file.Files |
---|
16 | import java.nio.file.Path |
---|
17 | |
---|
18 | import javax.imageio.ImageIO |
---|
19 | |
---|
20 | import org.openstreetmap.josm.Main |
---|
21 | import org.openstreetmap.josm.data.Version |
---|
22 | import org.openstreetmap.josm.data.coor.LatLon |
---|
23 | import org.openstreetmap.josm.data.osm.Node |
---|
24 | import org.openstreetmap.josm.data.osm.OsmPrimitive |
---|
25 | import org.openstreetmap.josm.data.osm.Way |
---|
26 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings |
---|
27 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer |
---|
28 | import org.openstreetmap.josm.data.projection.Projections |
---|
29 | import org.openstreetmap.josm.gui.NavigatableComponent |
---|
30 | import org.openstreetmap.josm.gui.mappaint.Environment |
---|
31 | import org.openstreetmap.josm.gui.mappaint.MultiCascade |
---|
32 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference |
---|
33 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource |
---|
34 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.SimpleKeyValueCondition |
---|
35 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector |
---|
36 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser |
---|
37 | import org.openstreetmap.josm.gui.mappaint.styleelement.AreaElement |
---|
38 | import org.openstreetmap.josm.gui.mappaint.styleelement.LineElement |
---|
39 | import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement |
---|
40 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference |
---|
41 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset |
---|
42 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader |
---|
43 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType |
---|
44 | import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem |
---|
45 | import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem.MatchType |
---|
46 | import org.openstreetmap.josm.io.CachedFile |
---|
47 | import org.openstreetmap.josm.tools.Utils |
---|
48 | |
---|
49 | class taginfoextract { |
---|
50 | |
---|
51 | static def options |
---|
52 | static String image_dir |
---|
53 | int josm_svn_revision |
---|
54 | String input_file |
---|
55 | MapCSSStyleSource style_source |
---|
56 | FileWriter output_file |
---|
57 | String base_dir = "." |
---|
58 | Set tags = [] |
---|
59 | |
---|
60 | private def cached_svnrev |
---|
61 | |
---|
62 | /** |
---|
63 | * Check if a certain tag is supported by the style as node / way / area. |
---|
64 | */ |
---|
65 | abstract class Checker { |
---|
66 | |
---|
67 | def tag |
---|
68 | OsmPrimitive osm |
---|
69 | |
---|
70 | Checker(tag) { |
---|
71 | this.tag = tag |
---|
72 | } |
---|
73 | |
---|
74 | Environment apply_stylesheet(OsmPrimitive osm) { |
---|
75 | osm.put(tag[0], tag[1]) |
---|
76 | MultiCascade mc = new MultiCascade() |
---|
77 | |
---|
78 | Environment env = new Environment(osm, mc, null, style_source) |
---|
79 | for (def r in style_source.rules) { |
---|
80 | env.clearSelectorMatchingInformation() |
---|
81 | if (r.selector.matches(env)) { |
---|
82 | // ignore selector range |
---|
83 | if (env.layer == null) { |
---|
84 | env.layer = "default" |
---|
85 | } |
---|
86 | r.execute(env) |
---|
87 | } |
---|
88 | } |
---|
89 | env.layer = "default" |
---|
90 | return env |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Create image file from StyleElement. |
---|
95 | * @return the URL |
---|
96 | */ |
---|
97 | def create_image(StyleElement elem_style, type, nc) { |
---|
98 | def img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB) |
---|
99 | def g = img.createGraphics() |
---|
100 | g.setClip(0, 0, 16, 16) |
---|
101 | def renderer = new StyledMapRenderer(g, nc, false) |
---|
102 | renderer.getSettings(false) |
---|
103 | elem_style.paintPrimitive(osm, MapPaintSettings.INSTANCE, renderer, false, false, false) |
---|
104 | def base_url = options.imgurlprefix ? options.imgurlprefix : image_dir |
---|
105 | def image_name = "${type}_${tag[0]}=${tag[1]}.png" |
---|
106 | ImageIO.write(img, "png", new File("${image_dir}/${image_name}")) |
---|
107 | return "${base_url}/${image_name}" |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Checks, if tag is supported and find URL for image icon in this case. |
---|
112 | * @param generate_image if true, create or find a suitable image icon and return URL, |
---|
113 | * if false, just check if tag is supported and return true or false |
---|
114 | */ |
---|
115 | abstract def find_url(boolean generate_image) |
---|
116 | } |
---|
117 | |
---|
118 | class NodeChecker extends Checker { |
---|
119 | NodeChecker(tag) { |
---|
120 | super(tag) |
---|
121 | } |
---|
122 | |
---|
123 | def find_url(boolean generate_image) { |
---|
124 | osm = new Node(LatLon.ZERO) |
---|
125 | def env = apply_stylesheet(osm) |
---|
126 | def c = env.mc.getCascade("default") |
---|
127 | def image = c.get("icon-image") |
---|
128 | if (image) { |
---|
129 | if (image instanceof IconReference) { |
---|
130 | if (image.iconName != "misc/deprecated.png") |
---|
131 | return find_image_url(image.iconName) |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | class WayChecker extends Checker { |
---|
138 | WayChecker(tag) { |
---|
139 | super(tag) |
---|
140 | } |
---|
141 | |
---|
142 | def find_url(boolean generate_image) { |
---|
143 | osm = new Way() |
---|
144 | def nc = new NavigatableComponent() |
---|
145 | def n1 = new Node(nc.getLatLon(2,8)) |
---|
146 | def n2 = new Node(nc.getLatLon(14,8)) |
---|
147 | ((Way)osm).addNode(n1) |
---|
148 | ((Way)osm).addNode(n2) |
---|
149 | def env = apply_stylesheet(osm) |
---|
150 | def les = LineElement.createLine(env) |
---|
151 | if (les != null) { |
---|
152 | if (!generate_image) return true |
---|
153 | return create_image(les, 'way', nc) |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | class AreaChecker extends Checker { |
---|
159 | AreaChecker(tag) { |
---|
160 | super(tag) |
---|
161 | } |
---|
162 | |
---|
163 | def find_url(boolean generate_image) { |
---|
164 | osm = new Way() |
---|
165 | def nc = new NavigatableComponent() |
---|
166 | def n1 = new Node(nc.getLatLon(2,2)) |
---|
167 | def n2 = new Node(nc.getLatLon(14,2)) |
---|
168 | def n3 = new Node(nc.getLatLon(14,14)) |
---|
169 | def n4 = new Node(nc.getLatLon(2,14)) |
---|
170 | ((Way)osm).addNode(n1) |
---|
171 | ((Way)osm).addNode(n2) |
---|
172 | ((Way)osm).addNode(n3) |
---|
173 | ((Way)osm).addNode(n4) |
---|
174 | ((Way)osm).addNode(n1) |
---|
175 | def env = apply_stylesheet(osm) |
---|
176 | def aes = AreaElement.create(env) |
---|
177 | if (aes != null) { |
---|
178 | if (!generate_image) return true |
---|
179 | return create_image(aes, 'area', nc) |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Main method. |
---|
186 | */ |
---|
187 | static main(def args) { |
---|
188 | parse_command_line_arguments(args) |
---|
189 | def script = new taginfoextract() |
---|
190 | if (!options.t || options.t == 'mappaint') { |
---|
191 | script.run() |
---|
192 | } else if (options.t == 'presets') { |
---|
193 | script.run_presets() |
---|
194 | } else if (options.t == 'external_presets') { |
---|
195 | script.run_external_presets() |
---|
196 | } else { |
---|
197 | System.err.println 'Invalid type ' + options.t |
---|
198 | if (!options.noexit) { |
---|
199 | System.exit(1) |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | if (!options.noexit) { |
---|
204 | System.exit(0) |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * Parse command line arguments. |
---|
210 | */ |
---|
211 | static void parse_command_line_arguments(args) { |
---|
212 | def cli = new CliBuilder(usage:'taginfoextract.groovy [options] [inputfile]', |
---|
213 | header:"Options:", |
---|
214 | footer:"[inputfile] the file to process (optional, default is 'resource://styles/standard/elemstyles.mapcss')") |
---|
215 | cli.o(args:1, argName: "file", "output file (json), - prints to stdout (default: -)") |
---|
216 | cli.t(args:1, argName: "type", "the project type to be generated") |
---|
217 | cli._(longOpt:'svnrev', args:1, argName:"revision", "corresponding revision of the repository https://svn.openstreetmap.org/ (optional, current revision is read from the local checkout or from the web if not given, see --svnweb)") |
---|
218 | cli._(longOpt:'imgdir', args:1, argName:"directory", "directory to put the generated images in (default: ./taginfo-img)") |
---|
219 | cli._(longOpt:'noexit', "don't call System.exit(), for use from Ant script") |
---|
220 | cli._(longOpt:'svnweb', 'fetch revision of the repository https://svn.openstreetmap.org/ from web and not from the local repository') |
---|
221 | cli._(longOpt:'imgurlprefix', args:1, argName:'prefix', 'image URLs prefix for generated image files') |
---|
222 | cli.h(longOpt:'help', "show this help") |
---|
223 | options = cli.parse(args) |
---|
224 | |
---|
225 | if (options.h) { |
---|
226 | cli.usage() |
---|
227 | System.exit(0) |
---|
228 | } |
---|
229 | if (options.arguments().size() > 1) { |
---|
230 | System.err.println "Error: More than one input file given!" |
---|
231 | cli.usage() |
---|
232 | System.exit(-1) |
---|
233 | } |
---|
234 | if (options.svnrev) { |
---|
235 | assert Integer.parseInt(options.svnrev) > 0 |
---|
236 | } |
---|
237 | image_dir = 'taginfo-img' |
---|
238 | if (options.imgdir) { |
---|
239 | image_dir = options.imgdir |
---|
240 | } |
---|
241 | def image_dir_file = new File(image_dir) |
---|
242 | if (!image_dir_file.exists()) { |
---|
243 | image_dir_file.mkdirs() |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | void run_presets() { |
---|
248 | init() |
---|
249 | def presets = TaggingPresetReader.readAll(input_file, true) |
---|
250 | def tags = convert_presets(presets, "", true) |
---|
251 | write_json("JOSM main presets", "Tags supported by the default presets in the OSM editor JOSM", tags) |
---|
252 | } |
---|
253 | |
---|
254 | def convert_presets(Iterable<TaggingPreset> presets, String descriptionPrefix, boolean addImages) { |
---|
255 | def tags = [] |
---|
256 | for (TaggingPreset preset : presets) { |
---|
257 | for (KeyedItem item : Utils.filteredCollection(preset.data, KeyedItem.class)) { |
---|
258 | def values |
---|
259 | switch (MatchType.ofString(item.match)) { |
---|
260 | case MatchType.KEY_REQUIRED: values = item.getValues(); break; |
---|
261 | case MatchType.KEY_VALUE_REQUIRED: values = item.getValues(); break; |
---|
262 | default: values = []; |
---|
263 | } |
---|
264 | for (String value : values) { |
---|
265 | def tag = [ |
---|
266 | description: descriptionPrefix + preset.name, |
---|
267 | key: item.key, |
---|
268 | value: value, |
---|
269 | object_types: preset.types.collect {it == TaggingPresetType.CLOSEDWAY ? "area" : it.toString().toLowerCase()}, |
---|
270 | ] |
---|
271 | if (addImages && preset.iconName) tag += [icon_url: find_image_url(preset.iconName)] |
---|
272 | tags += tag |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | return tags |
---|
277 | } |
---|
278 | |
---|
279 | void run_external_presets() { |
---|
280 | init() |
---|
281 | TaggingPresetReader.setLoadIcons(false) |
---|
282 | def sources = new TaggingPresetPreference.TaggingPresetSourceEditor().loadAndGetAvailableSources() |
---|
283 | def tags = [] |
---|
284 | for (def source : sources) { |
---|
285 | if (source.url.startsWith("resource")) { |
---|
286 | // default presets |
---|
287 | continue; |
---|
288 | } |
---|
289 | try { |
---|
290 | println "Loading ${source.url}" |
---|
291 | def presets = TaggingPresetReader.readAll(source.url, false) |
---|
292 | def t = convert_presets(presets, source.title + " ", false) |
---|
293 | println "Converting ${t.size()} presets of ${source.title}" |
---|
294 | tags += t |
---|
295 | } catch (Exception ex) { |
---|
296 | System.err.println("Skipping ${source.url} due to error") |
---|
297 | ex.printStackTrace() |
---|
298 | } |
---|
299 | } |
---|
300 | write_json("JOSM user presets", "Tags supported by the user contributed presets in the OSM editor JOSM", tags) |
---|
301 | } |
---|
302 | |
---|
303 | void run() { |
---|
304 | init() |
---|
305 | parse_style_sheet() |
---|
306 | collect_tags() |
---|
307 | |
---|
308 | def tags = tags.collect { |
---|
309 | def tag = it |
---|
310 | def types = [] |
---|
311 | def final_url = null |
---|
312 | |
---|
313 | def node_url = new NodeChecker(tag).find_url(true) |
---|
314 | if (node_url) { |
---|
315 | types += 'node' |
---|
316 | final_url = node_url |
---|
317 | } |
---|
318 | def way_url = new WayChecker(tag).find_url(final_url == null) |
---|
319 | if (way_url) { |
---|
320 | types += 'way' |
---|
321 | if (!final_url) { |
---|
322 | final_url = way_url |
---|
323 | } |
---|
324 | } |
---|
325 | def area_url = new AreaChecker(tag).find_url(final_url == null) |
---|
326 | if (area_url) { |
---|
327 | types += 'area' |
---|
328 | if (!final_url) { |
---|
329 | final_url = area_url |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | def obj = [key: tag[0], value: tag[1]] |
---|
334 | if (types) obj += [object_types: types] |
---|
335 | if (final_url) obj += [icon_url: final_url] |
---|
336 | obj |
---|
337 | } |
---|
338 | |
---|
339 | write_json("JOSM main mappaint style", "Tags supported by the main mappaint style in the OSM editor JOSM", tags) |
---|
340 | } |
---|
341 | |
---|
342 | void write_json(name, description, tags) { |
---|
343 | def json = new JsonBuilder() |
---|
344 | def project = [ |
---|
345 | name: name, |
---|
346 | description: description, |
---|
347 | project_url: "https://josm.openstreetmap.de/", |
---|
348 | icon_url: "https://josm.openstreetmap.de/export/7770/josm/trunk/images/logo_16x16x8.png", |
---|
349 | contact_name: "JOSM developer team", |
---|
350 | contact_email: "josm-dev@openstreetmap.org", |
---|
351 | ] |
---|
352 | json data_format: 1, data_updated: new Date().format("yyyyMMdd'T'hhmmssZ"), project: project, tags: tags |
---|
353 | |
---|
354 | if (output_file != null) { |
---|
355 | json.writeTo(output_file) |
---|
356 | output_file.close() |
---|
357 | } else { |
---|
358 | print json.toPrettyString() |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | * Initialize the script. |
---|
364 | */ |
---|
365 | def init() { |
---|
366 | Main.initApplicationPreferences() |
---|
367 | Main.determinePlatformHook() |
---|
368 | Main.pref.enableSaveOnPut(false) |
---|
369 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")) |
---|
370 | Path tmpdir = Files.createTempDirectory(FileSystems.getDefault().getPath(base_dir), "pref") |
---|
371 | tmpdir.toFile().deleteOnExit() |
---|
372 | System.setProperty("josm.home", tmpdir.toString()) |
---|
373 | |
---|
374 | josm_svn_revision = Version.getInstance().getVersion() |
---|
375 | assert josm_svn_revision != Version.JOSM_UNKNOWN_VERSION |
---|
376 | |
---|
377 | if (options.arguments().size() == 0 && (!options.t || options.t == 'mappaint')) { |
---|
378 | input_file = "resource://styles/standard/elemstyles.mapcss" |
---|
379 | } else if (options.arguments().size() == 0 && options.t == 'presets') { |
---|
380 | input_file = "resource://data/defaultpresets.xml" |
---|
381 | } else { |
---|
382 | input_file = options.arguments()[0] |
---|
383 | } |
---|
384 | |
---|
385 | output_file = null |
---|
386 | if (options.o && options.o != "-") { |
---|
387 | output_file = new FileWriter(options.o) |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | /** |
---|
392 | * Determine full image url (can refer to JOSM or OSM repository). |
---|
393 | */ |
---|
394 | def find_image_url(String path) { |
---|
395 | def f = new File("${base_dir}/images/styles/standard/${path}") |
---|
396 | if (f.exists()) { |
---|
397 | def rev = osm_svn_revision() |
---|
398 | return "https://trac.openstreetmap.org/export/${rev}/subversion/applications/share/map-icons/classic.small/${path}" |
---|
399 | } |
---|
400 | f = new File("${base_dir}/images/${path}") |
---|
401 | if (f.exists()) { |
---|
402 | if (path.startsWith("images/styles/standard/")) { |
---|
403 | path = path.substring("images/styles/standard/".length()) |
---|
404 | def rev = osm_svn_revision() |
---|
405 | return "https://trac.openstreetmap.org/export/${rev}/subversion/applications/share/map-icons/classic.small/${path}" |
---|
406 | } else if (path.startsWith("styles/standard/")) { |
---|
407 | path = path.substring("styles/standard/".length()) |
---|
408 | def rev = osm_svn_revision() |
---|
409 | return "https://trac.openstreetmap.org/export/${rev}/subversion/applications/share/map-icons/classic.small/${path}" |
---|
410 | } else { |
---|
411 | return "https://josm.openstreetmap.de/export/${josm_svn_revision}/josm/trunk/images/${path}" |
---|
412 | } |
---|
413 | } |
---|
414 | assert false, "Cannot find image url for ${path}" |
---|
415 | } |
---|
416 | |
---|
417 | /** |
---|
418 | * Get revision for the repository https://svn.openstreetmap.org. |
---|
419 | */ |
---|
420 | def osm_svn_revision() { |
---|
421 | if (cached_svnrev != null) return cached_svnrev |
---|
422 | if (options.svnrev) { |
---|
423 | cached_svnrev = Integer.parseInt(options.svnrev) |
---|
424 | return cached_svnrev |
---|
425 | } |
---|
426 | def xml |
---|
427 | if (options.svnweb) { |
---|
428 | xml = "svn info --xml https://svn.openstreetmap.org/applications/share/map-icons/classic.small".execute().text |
---|
429 | } else { |
---|
430 | xml = "svn info --xml ${base_dir}/images/styles/standard/".execute().text |
---|
431 | } |
---|
432 | |
---|
433 | def svninfo = new XmlParser().parseText(xml) |
---|
434 | def rev = svninfo.entry.'@revision'[0] |
---|
435 | cached_svnrev = Integer.parseInt(rev) |
---|
436 | assert cached_svnrev > 0 |
---|
437 | return cached_svnrev |
---|
438 | } |
---|
439 | |
---|
440 | /** |
---|
441 | * Read the style sheet file and parse the MapCSS code. |
---|
442 | */ |
---|
443 | def parse_style_sheet() { |
---|
444 | def file = new CachedFile(input_file) |
---|
445 | def stream = file.getInputStream() |
---|
446 | def parser = new MapCSSParser(stream, "UTF-8", MapCSSParser.LexicalState.DEFAULT) |
---|
447 | style_source = new MapCSSStyleSource("") |
---|
448 | style_source.url = "" |
---|
449 | parser.sheet(style_source) |
---|
450 | } |
---|
451 | |
---|
452 | /** |
---|
453 | * Collect all the tag from the style sheet. |
---|
454 | */ |
---|
455 | def collect_tags() { |
---|
456 | for (rule in style_source.rules) { |
---|
457 | def selector = rule.selector |
---|
458 | if (selector instanceof GeneralSelector) { |
---|
459 | def conditions = selector.getConditions() |
---|
460 | for (cond in conditions) { |
---|
461 | if (cond instanceof SimpleKeyValueCondition) { |
---|
462 | tags.add([cond.k, cond.v]) |
---|
463 | } |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | } |
---|