Changeset 8272 in josm


Ignore:
Timestamp:
2015-04-25T23:57:19+02:00 (9 years ago)
Author:
simon04
Message:

see #10512 - Use Groovy's JsonBuilder

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/taginfoextract.groovy

    r7955 r8272  
    1111import javax.imageio.ImageIO
    1212
     13import groovy.json.JsonBuilder
    1314import org.openstreetmap.josm.Main
    1415import org.openstreetmap.josm.data.Version
     
    235236        collect_tags()
    236237
    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) {
     238        def tags = tags.collect {
     239            def tag = it
    255240            def types = []
    256241            def final_url = null
     
    258243            def node_url = new NodeChecker(tag).find_url(true)
    259244            if (node_url) {
    260                 types += '"node"'
     245                types += 'node'
    261246                final_url = node_url
    262247            }
    263248            def way_url = new WayChecker(tag).find_url(final_url == null)
    264249            if (way_url) {
    265                 types += '"way"'
     250                types += 'way'
    266251                if (!final_url) {
    267252                    final_url = way_url
     
    270255            def area_url = new AreaChecker(tag).find_url(final_url == null)
    271256            if (area_url) {
    272                 types += '"area"'
     257                types += 'area'
    273258                if (!final_url) {
    274259                    final_url = area_url
     
    276261            }
    277262
    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()
     263            def obj = [key: tag[0], value: tag[1]]
     264            if (types) obj += [object_types: types]
     265            if (final_url) obj += [icon_url: final_url]
     266            obj
     267        }
     268
     269        def json = get_json("JOSM main mappaint style", "Tags supported by the main mappaint style in the OSM editor JOSM", tags)
    296270
    297271        if (output_file != null) {
     272            json.writeTo(output_file)
    298273            output_file.close()
    299         }
     274        } else {
     275            print json.toPrettyString()
     276        }
     277    }
     278
     279    static JsonBuilder get_json(name, description, tags) {
     280        def json = new JsonBuilder()
     281        def project = [
     282                name: name,
     283                description: description,
     284                project_url: "http://josm.openstreetmap.de/",
     285                icon_url: "http://josm.openstreetmap.de/export/7770/josm/trunk/images/logo_16x16x8.png",
     286                contact_name: "JOSM developer team",
     287                contact_email: "josm-dev@openstreetmap.org",
     288        ]
     289        json data_format: 1, data_updated: new Date().format("yyyyMMdd'T'hhmmssZ"), project: project, tags: tags
     290        return json
    300291    }
    301292
     
    375366    }
    376367
    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 
    396368}
    397369
Note: See TracChangeset for help on using the changeset viewer.