| 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 |
|
|---|
| 10 | import java.io.BufferedReader
|
|---|
| 11 | import java.util.ArrayList
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.Main
|
|---|
| 14 | import org.openstreetmap.josm.data.coor.LatLon
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.Node
|
|---|
| 16 | import org.openstreetmap.josm.data.projection.Projections
|
|---|
| 17 | import org.openstreetmap.josm.data.Version
|
|---|
| 18 | import org.openstreetmap.josm.gui.mappaint.Cascade
|
|---|
| 19 | import org.openstreetmap.josm.gui.mappaint.Environment
|
|---|
| 20 | import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.SimpleKeyValueCondition
|
|---|
| 21 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource
|
|---|
| 22 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
|
|---|
| 23 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector
|
|---|
| 24 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference
|
|---|
| 25 | import org.openstreetmap.josm.gui.mappaint.MultiCascade
|
|---|
| 26 | import org.openstreetmap.josm.io.CachedFile
|
|---|
| 27 |
|
|---|
| 28 | basedir = "."
|
|---|
| 29 |
|
|---|
| 30 | def 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')")
|
|---|
| 33 | cli.o(args:1, argName: "file", "output file, - prints to stdout (default: -)")
|
|---|
| 34 | cli._(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)")
|
|---|
| 35 | cli.h(longOpt:'help', "show this help")
|
|---|
| 36 | options = cli.parse(args)
|
|---|
| 37 |
|
|---|
| 38 | if (options.h) {
|
|---|
| 39 | cli.usage()
|
|---|
| 40 | System.exit(0)
|
|---|
| 41 | }
|
|---|
| 42 | if (options.arguments().size() > 1) {
|
|---|
| 43 | System.err.println "Error: More than one input file given!"
|
|---|
| 44 | cli.usage()
|
|---|
| 45 | System.exit(-1)
|
|---|
| 46 | }
|
|---|
| 47 | if (options.svnrev) {
|
|---|
| 48 | assert Integer.parseInt(options.svnrev) > 0
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | Main.initApplicationPreferences()
|
|---|
| 52 | Main.pref.enableSaveOnPut(false)
|
|---|
| 53 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857"))
|
|---|
| 54 |
|
|---|
| 55 | josm_svn_revsion = Version.getInstance().getVersion()
|
|---|
| 56 | assert josm_svn_revsion != Version.JOSM_UNKNOWN_VERSION
|
|---|
| 57 |
|
|---|
| 58 | cached_svnrev = null
|
|---|
| 59 | /**
|
|---|
| 60 | * Get revision for the repository http://svn.openstreetmap.org.
|
|---|
| 61 | */
|
|---|
| 62 | def 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 | */
|
|---|
| 81 | def 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 |
|
|---|
| 94 | def input_file
|
|---|
| 95 | if (options.arguments().size() == 0) {
|
|---|
| 96 | input_file = "resource://styles/standard/elemstyles.mapcss"
|
|---|
| 97 | } else {
|
|---|
| 98 | input_file = options.arguments()[0]
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | def file = new CachedFile(input_file)
|
|---|
| 103 | def stream = file.getInputStream()
|
|---|
| 104 | def parser = new MapCSSParser(stream, "UTF-8", MapCSSParser.LexicalState.DEFAULT)
|
|---|
| 105 | def style_source = new MapCSSStyleSource("")
|
|---|
| 106 | style_source.url = ""
|
|---|
| 107 | parser.sheet(style_source)
|
|---|
| 108 |
|
|---|
| 109 | def tags = [] as Set
|
|---|
| 110 |
|
|---|
| 111 | for (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 |
|
|---|
| 125 | output_file = null
|
|---|
| 126 | if (options.o && options.o != "-") {
|
|---|
| 127 | output_file = new FileWriter(options.o)
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | def output(x) {
|
|---|
| 131 | if (output_file != null) {
|
|---|
| 132 | output_file.write(x)
|
|---|
| 133 | } else {
|
|---|
| 134 | print x
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | datetime = new Date().format("yyyyMMdd'T'hhmmssZ")
|
|---|
| 139 |
|
|---|
| 140 | output """{
|
|---|
| 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 |
|
|---|
| 155 | sep = ""
|
|---|
| 156 | for (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 | }
|
|---|
| 190 | output """
|
|---|
| 191 | ]
|
|---|
| 192 | }
|
|---|
| 193 | """
|
|---|
| 194 |
|
|---|
| 195 | if (output_file != null) {
|
|---|
| 196 | output_file.close()
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | System.exit(0)
|
|---|
| 200 |
|
|---|