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