Skip to content

Layer Opacity ​

Overlay semi-transparent neighborhood polygons on top of OpenStreetMap layers.

Python Script ​

python
"""Layer opacity — OpenStreetMap layers with the neighborhoods drawn over them at 75% opacity (gallery: layer-opacity).

The OSM extract comes first because the first source sets the extent of the workspace; the neighborhoods are cut to it.
"""
from __future__ import annotations

from pathlib import Path

import pyautark as ak

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

osm = ak.OSM(
    "mnt",
    bbox=(-74.02, 40.700, -73.995, 40.72),
    layers=["surface", "parks", "water", "roads", "buildings"],
    source="pbf",
    pbf_file_url=(DATA_DIR / "lower_mnt.osm.pbf").as_uri(),
)
neighborhoods = ak.GeoJSON("neighborhoods", url=(DATA_DIR / "mnt_neighs.geojson").as_uri())

spec = ak.Spec(
    metadata=ak.Metadata(title="Layer Opacity"),
    data=[osm, neighborhoods],
    views=[
        ak.Map(
            camera=ak.Camera(pitch=40),
            layers=[
                *(ak.Layer(osm.layer_source(layer), id=layer) for layer in osm.layers),
                ak.Layer(neighborhoods, id="neighborhoods").style(color="#e45756", opacity=0.75),
            ],
        )
    ],
)

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.