source: josm/trunk/taginfoextract.groovy@ 8276

Last change on this file since 8276 was 8275, checked in by bastiK, 9 years ago

see #10512 - compatibility with groovy 1.8.6

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