source: josm/trunk/taginfoextract.groovy@ 7544

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

see #10512 - taginfo: first version for mappaint style
only for node tags so far

File size: 6.0 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 */
9
10import java.io.BufferedReader
11import java.util.ArrayList
12
13import org.openstreetmap.josm.Main
14import org.openstreetmap.josm.data.coor.LatLon
15import org.openstreetmap.josm.data.osm.Node
16import org.openstreetmap.josm.data.projection.Projections
17import org.openstreetmap.josm.data.Version
18import org.openstreetmap.josm.gui.mappaint.Cascade
19import org.openstreetmap.josm.gui.mappaint.Environment
20import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.SimpleKeyValueCondition
21import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource
22import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
23import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector
24import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference
25import org.openstreetmap.josm.gui.mappaint.MultiCascade
26import org.openstreetmap.josm.io.CachedFile
27
28basedir = "."
29
30def cli = new CliBuilder(usage:'taginfoextract.groovy [options] [inputfile]',
31 header:"Options:",
32 footer:"[inputfile] the file to process (optional, default is 'resource://styles/standard/elemstyles.mapcss')")
33cli.o(args:1, argName: "file", "output file, - prints to stdout (default: -)")
34cli._(longOpt:'svnrev', args:1, argName:"revision", "corresponding revision of the repository http://svn.openstreetmap.org/ (optional, current revision is fetched from the web if not given)")
35cli.h(longOpt:'help', "show this help")
36options = cli.parse(args)
37
38if (options.h) {
39 cli.usage()
40 System.exit(0)
41}
42if (options.arguments().size() > 1) {
43 System.err.println "Error: More than one input file given!"
44 cli.usage()
45 System.exit(-1)
46}
47if (options.svnrev) {
48 assert Integer.parseInt(options.svnrev) > 0
49}
50
51Main.initApplicationPreferences()
52Main.pref.enableSaveOnPut(false)
53Main.setProjection(Projections.getProjectionByCode("EPSG:3857"))
54
55josm_svn_revsion = Version.getInstance().getVersion()
56assert josm_svn_revsion != Version.JOSM_UNKNOWN_VERSION
57
58cached_svnrev = null
59/**
60 * Get revision for the repository http://svn.openstreetmap.org.
61 */
62def osm_svn_revision() {
63 if (cached_svnrev != null) return cached_svnrev
64 if (options.svnrev) {
65 cached_svnrev = Integer.parseInt(options.svnrev)
66 return cached_svnrev
67 }
68 //xml = "svn info --xml http://svn.openstreetmap.org/applications/share/map-icons/classic.small".execute().text
69 xml = ("svn info --xml ${basedir}/images/styles/standard/").execute().text
70
71 def svninfo = new XmlParser().parseText(xml)
72 def rev = svninfo.entry.'@revision'[0]
73 cached_svnrev = Integer.parseInt(rev)
74 assert cached_svnrev > 0
75 return cached_svnrev
76}
77
78/**
79 * Determine full image url (can refer to JOSM or OSM repository).
80 */
81def find_image_url(path) {
82 def f = new File("${basedir}/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("${basedir}/images/${path}")
88 if (f.exists()) {
89 return "https://josm.openstreetmap.de/export/${josm_svn_revsion}/josm/trunk/images/${path}"
90 }
91 assert false, "Cannot find image url for ${path}"
92}
93
94def input_file
95if (options.arguments().size() == 0) {
96 input_file = "resource://styles/standard/elemstyles.mapcss"
97} else {
98 input_file = options.arguments()[0]
99}
100
101
102def file = new CachedFile(input_file)
103def stream = file.getInputStream()
104def parser = new MapCSSParser(stream, "UTF-8", MapCSSParser.LexicalState.DEFAULT)
105def style_source = new MapCSSStyleSource("")
106style_source.url = ""
107parser.sheet(style_source)
108
109def tags = [] as Set
110
111for (rule in style_source.rules) {
112 def selector = rule.selector
113 if (selector instanceof GeneralSelector) {
114 def conditions = selector.getConditions()
115 for (cond in conditions) {
116 if (cond instanceof SimpleKeyValueCondition) {
117 if (selector.base == "node") {
118 tags.add([cond.k, cond.v])
119 }
120 }
121 }
122 }
123}
124
125output_file = null
126if (options.o && options.o != "-") {
127 output_file = new FileWriter(options.o)
128}
129
130def output(x) {
131 if (output_file != null) {
132 output_file.write(x)
133 } else {
134 print x
135 }
136}
137
138datetime = new Date().format("yyyyMMdd'T'hhmmssZ")
139
140output """{
141 "data_format": 1,
142 "data_url": "FIXME",
143 "data_updated": "${datetime}",
144 "project": {
145 "name": "JOSM main mappaint style",
146 "description": "Tags supported by the main mappaint style in the OSM editor JOSM",
147 "project_url": "http://josm.openstreetmap.de/",
148 "icon_url": "http://josm.openstreetmap.de/export/7543/josm/trunk/images/logo_16x16x8.png",
149 "contact_name": "JOSM developer team",
150 "contact_email": "josm-dev@openstreetmap.org"
151 },
152 "tags": [
153"""
154
155sep = ""
156for (tag in tags) {
157 def k = tag[0]
158 def v = tag[1]
159 def osm = new Node(new LatLon(0,0))
160 osm.put(k, v)
161 def mc = new MultiCascade()
162
163 def env = new Environment(osm, mc, null, style_source)
164 for (def r in style_source.rules) {
165 env.clearSelectorMatchingInformation()
166 env.layer = r.selector.getSubpart()
167 if (r.selector.matches(env)) {
168 // ignore selector range
169 if (env.layer == null) {
170 env.layer = "default"
171 }
172 r.execute(env)
173 }
174 }
175 def c = mc.getCascade("default")
176 def image = c.get("icon-image")
177 if (image) {
178 if (!(image instanceof IconReference)) continue
179 def image_url = find_image_url(image.iconName)
180
181 output """${sep} {
182 | "key": "${k}",
183 | "value": "${v}",
184 | "object_types": ["node"],
185 | "icon_url": "${image_url}"
186 | }""".stripMargin()
187 sep = ",\n"
188 }
189}
190output """
191 ]
192}
193"""
194
195if (output_file != null) {
196 output_file.close()
197}
198
199System.exit(0)
200
Note: See TracBrowser for help on using the repository browser.