Skip to content

Categorical Colormap ​

Color OpenStreetMap road segments by highway class using a categorical ak.Scale and explicit category domain.

Python Script ​

python
from __future__ import annotations

import json
from pathlib import Path

import pyautark as ak

DATA_DIR = Path(__file__).resolve().parents[1] / "data"

with (DATA_DIR / "mnt_roads.geojson").open(encoding="utf-8") as f:
    roads_data = json.load(f)

for feature in roads_data["features"]:
    highway = feature.setdefault("properties", {}).get("highway")
    feature["properties"]["compute"] = {
        "highwayGroup": highway if highway in ("primary", "secondary") else "other"
    }

roads = ak.GeoJSON("roads", values=roads_data, layer_type="polylines")
roads_layer = ak.Layer(
    roads,
    id="roads_layer",
    encoding=ak.Encoding(
        color=ak.field(
            "properties.compute.highwayGroup",
            ak.Scale(type="categorical", scheme="observable10"),
        )
    ),
)

spec = ak.Spec(data=[roads], views=[ak.Map(layers=[roads_layer])])

if __name__ == "__main__":
    spec.window(height="760px")

Run in Jupyter or standalone HTML

Replace spec.window(...) with spec.widget() inside a Jupyter notebook (requires pip install "pyautark[widget]") or spec.save_html("out.html") to export a standalone web page.

This example comes from the pyautark repository.

Released under the MIT License.