Visualizing PMTiles with Leafmap and MapLibre
PMTiles is a single-file archive format for tiled data. A PMTiles archive can be hosted on a commodity storage platform such as S3, and enables low-cost, zero-maintenance map applications that are "serverless" - free of a custom tile backend or third party provider.
In [1]:
Copied!
# %pip install -U "leafmap[maplibre]" pmtiles
# %pip install -U "leafmap[maplibre]" pmtiles
In [2]:
Copied!
import leafmap.maplibregl as leafmap
import leafmap.maplibregl as leafmap
In [3]:
Copied!
url = "https://open.gishub.org/data/pmtiles/protomaps_firenze.pmtiles"
metadata = leafmap.pmtiles_metadata(url)
print(f"layer names: {metadata['layer_names']}")
print(f"bounds: {metadata['bounds']}")
url = "https://open.gishub.org/data/pmtiles/protomaps_firenze.pmtiles"
metadata = leafmap.pmtiles_metadata(url)
print(f"layer names: {metadata['layer_names']}")
print(f"bounds: {metadata['bounds']}")
layer names: ['earth', 'natural', 'land', 'water', 'physical_line', 'buildings', 'physical_point', 'places', 'roads', 'transit', 'pois', 'boundaries', 'mask'] bounds: [11.154026, 43.7270125, 11.3289395, 43.8325455]
In [4]:
Copied!
m = leafmap.Map()
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "landuse",
"type": "fill",
"paint": {"fill-color": "steelblue"},
},
{
"id": "roads",
"source": "example_source",
"source-layer": "roads",
"type": "line",
"paint": {"line-color": "black"},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=1.0,
tooltip=True,
)
m
m = leafmap.Map()
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "landuse",
"type": "fill",
"paint": {"fill-color": "steelblue"},
},
{
"id": "roads",
"source": "example_source",
"source-layer": "roads",
"type": "line",
"paint": {"line-color": "black"},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=1.0,
tooltip=True,
)
m
In [5]:
Copied!
m.layer_interact()
m.layer_interact()
Source Cooperative¶
In [6]:
Copied!
url = "https://data.source.coop/vida/google-microsoft-open-buildings/pmtiles/go_ms_building_footprints.pmtiles"
metadata = leafmap.pmtiles_metadata(url)
print(f"layer names: {metadata['layer_names']}")
print(f"bounds: {metadata['bounds']}")
url = "https://data.source.coop/vida/google-microsoft-open-buildings/pmtiles/go_ms_building_footprints.pmtiles"
metadata = leafmap.pmtiles_metadata(url)
print(f"layer names: {metadata['layer_names']}")
print(f"bounds: {metadata['bounds']}")
layer names: ['building_footprints'] bounds: [-160.221701, -55.9756776, 166.709685, 74.7731168]
In [7]:
Copied!
m = leafmap.Map(center=[0, 20], zoom=2, height="800px")
m.add_basemap("Google Hybrid", visible=False)
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "building_footprints",
"type": "fill",
"paint": {"fill-color": "#3388ff", "fill-opacity": 0.5},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=1.0,
tooltip=True,
)
m
m = leafmap.Map(center=[0, 20], zoom=2, height="800px")
m.add_basemap("Google Hybrid", visible=False)
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "building_footprints",
"type": "fill",
"paint": {"fill-color": "#3388ff", "fill-opacity": 0.5},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=1.0,
tooltip=True,
)
m
In [8]:
Copied!
m.layer_interact()
m.layer_interact()
Local PMTiles¶
tippecanoe is required to convert vector data to pmtiles. Install it with conda install -c conda-forge tippecanoe
.
Download building footprints of Derna, Libya.
In [9]:
Copied!
url = "https://raw.githubusercontent.com/opengeos/open-data/main/datasets/libya/Derna_buildings.geojson"
leafmap.download_file(url, "buildings.geojson")
url = "https://raw.githubusercontent.com/opengeos/open-data/main/datasets/libya/Derna_buildings.geojson"
leafmap.download_file(url, "buildings.geojson")
buildings.geojson already exists. Skip downloading. Set overwrite=True to overwrite.
Out[9]:
'/home/runner/work/leafmap/leafmap/docs/notebooks/buildings.geojson'
Convert vector to PMTiles.
In [10]:
Copied!
pmtiles = "buildings.pmtiles"
leafmap.geojson_to_pmtiles(
"buildings.geojson", pmtiles, layer_name="buildings", overwrite=True, quiet=True
)
pmtiles = "buildings.pmtiles"
leafmap.geojson_to_pmtiles(
"buildings.geojson", pmtiles, layer_name="buildings", overwrite=True, quiet=True
)
Error: tippecanoe is not installed. You can install it using conda with the following command: conda install -c conda-forge tippecanoe
Start a HTTP Sever
In [11]:
Copied!
leafmap.start_server(port=8000)
leafmap.start_server(port=8000)
In [12]:
Copied!
url = f"http://127.0.0.1:8000/{pmtiles}"
# leafmap.pmtiles_metadata(url)
url = f"http://127.0.0.1:8000/{pmtiles}"
# leafmap.pmtiles_metadata(url)
Display the PMTiles on the map.
In [13]:
Copied!
m = leafmap.Map()
m.add_basemap("Google Hybrid")
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "buildings",
"type": "fill",
"paint": {"fill-color": "#3388ff", "fill-opacity": 0.5},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=0.8,
tooltip=True,
)
m
m = leafmap.Map()
m.add_basemap("Google Hybrid")
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "buildings",
"type": "fill",
"paint": {"fill-color": "#3388ff", "fill-opacity": 0.5},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=0.8,
tooltip=True,
)
m
In [14]:
Copied!
m.layer_interact()
m.layer_interact()