OpenStreetMap logo OpenStreetMap

Styling MVT layers in JOSM

Posted by RobJN on 7 August 2021 in English.

As of a few weeks ago, it is now possible to add Mapbox Vector Tile (MVT) layers to JOSM. I have been playing around with these to learn how to apply basic styling to the layer. For this tutorial we will use a postcodes layer in Great Britain.

Styling requires us to first create a style.json file. Copy the contents below in to a plain text file (notepad) and save as style.json. If doing this on Windows you will want to make sure it doesn’t also have a .txt extension.

{
  "version": 8,
  "name": "Postcodes",
  "owner": "Postcodes",
  "id": "postcodes",
  "sources": {
    "postcodes-source": {
      "maxzoom": 13,
      "tiles": [
        "https://api.mapbox.com/v4/robjn.3uur23nz/{zoom}/{x}/{y}.mvt?access_token=pk.eyJ1Ijoicm9iam4iLCJhIjoid0dYNkY1QSJ9.A-0lzQOawGYICYPfURsjDA"
      ],
      "type": "vector"
    }
  },
  "layers": [
    {
      "id": "something",
      "type": "circle",
      "source": "postcodes-source",
      "source-layer": "results-4326-deduped",
      "minzoom": 13,
      "paint": {
        "circle-color": "#C70039",
        "circle-radius": 6
      }
    },
    {
      "id": "secondthing",
      "type": "symbol",
      "source": "postcodes-source",
      "source-layer": "results-4326-deduped",
      "minzoom": 13,
      "layout": {
        "text-field": "{postcode}",
        "text-size": 18,
        "text-allow-overlap": true,
        "text-ignore-placement": true
      },
      "paint": {
        "text-color": "#C70039",
        "text-halo-color": "#000000"
      }
    }
  ]
}

Next you will want to find where this file was saved and construct it’s path using the file:// protocol. For me the file is in “file:///home/rob/Documents/OSM/style.json”. For those using Windows yours is likely to start with “file:///C:/” (and I believe all backslashes in the Windows filepath should be changed to forward slashes). Avoid spaces in the filepath as I suspect that will break. You can test you have the right path by pasting it in your web browser as if it was like any other URL.

Finally in JOSM, go to Imagery -> Imagery preferences and click the +MVT button. Paste the filepath in the top box and type a suitable name in box 5 (e.g. postcodes).

The layer can now be added just like any other. If you want to edit the style (e.g. a different hex colour code) then you can do that by editing the style.json file. If you edit the style you will also need to find where JOSM stores it’s cache on you computer and delete postcodes-source.mapcss from the cache folder. The reason for this is because JOSM converts the style.json into a .mapcss format and doesn’t re-create this unless it is first deleted.

There is a lot more that can be done to style MVT layers. For those interest the full MVT style spec is here, however I don’t think it is all implemented in JOSM yet. For example, I couldn’t not get text sizes to vary by zoom level (interpolation). Hopefully that will come soon or someone can show me what I was doing wrong if already available.

I hope this helps. If stuck, give me a shout.

Discussion

Comment from vorpalblade-kaart on 13 August 2021 at 13:31

Spaces in the file path shouldn’t break it, but it wasn’t something I tested when I was coding the MVT support.

As you noted, JOSM does internally convert the Mapbox Vector Tile Style into a mapcss file. I don’t remember coding anything specifically to clear the mapcss on changes from the MVT style, but I also wasn’t coding for that specific use case – I only added that ability to simplify testing while coding.

There is a lot that can be done to style MVT layers, but I didn’t implement everything – that would have been (a) a big lift and (b) would have required more from the mapcss implementation. In the interest of getting support into JOSM, I only added mappings for stuff that mapcss supported.

See https://josm.openstreetmap.de/browser/josm/trunk/src/org/openstreetmap/josm/data/imagery/vectortile/mapbox/style for the source code. Layers.java is probably the file you would be most interested in.

It is great to see people start using the functionality. :)

Source: I was the person who had to implement MVT support.

Log in to leave a comment