source: josm/trunk/scripts/TagInfoExtract.groovy@ 14149

Last change on this file since 14149 was 14149, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.pref

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