source: josm/trunk/taginfoextract.groovy@ 7960

Last change on this file since 7960 was 7955, checked in by bastiK, 10 years ago

fixed #10958 - Change Taginfo JOSM icon to rev. 7770

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