82 pmtiles
Visualizing PMTiles with leafmap
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.
The PMTiles functionality for leafmap is based on the folium-pmtiles package. Credits to Jt Miclat.
In [1]:
Copied!
# %pip install -U leafmap pmtiles
# %pip install -U leafmap pmtiles
In [2]:
Copied!
import leafmap.foliumap as leafmap
import leafmap.foliumap 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, name="PMTiles", style=style, overlay=True, show=True, zoom_to_layer=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, name="PMTiles", style=style, overlay=True, show=True, zoom_to_layer=True
)
m
Out[4]:
Source Cooperative¶
In [5]:
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 [6]:
Copied!
m = leafmap.Map(center=[20, 0], zoom=2, height="800px")
m.add_basemap("CartoDB.DarkMatter")
m.add_basemap("Esri.WorldImagery", show=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, name="Buildings", style=style, overlay=True, show=True, zoom_to_layer=False
)
html = "Source: <a href='https://beta.source.coop/repositories/vida/google-microsoft-open-buildings/description' target='_blank'>source.coop</a>"
m.add_html(html, position="bottomright")
m
m = leafmap.Map(center=[20, 0], zoom=2, height="800px")
m.add_basemap("CartoDB.DarkMatter")
m.add_basemap("Esri.WorldImagery", show=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, name="Buildings", style=style, overlay=True, show=True, zoom_to_layer=False
)
html = "Source: source.coop"
m.add_html(html, position="bottomright")
m
Out[6]:
In [7]:
Copied!
m.save("buildings.html")
m.save("buildings.html")
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 [8]:
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")
Downloading... From: https://raw.githubusercontent.com/opengeos/open-data/main/datasets/libya/Derna_buildings.geojson To: /home/runner/work/leafmap/leafmap/docs/notebooks/buildings.geojson
0%| | 0.00/2.77M [00:00<?, ?B/s]
7.34MB [00:00, 64.4MB/s]
12.8MB [00:00, 87.1MB/s]
Out[8]:
'/home/runner/work/leafmap/leafmap/docs/notebooks/buildings.geojson'
Convert vector to PMTiles.
In [9]:
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 [10]:
Copied!
leafmap.start_server(port=8000)
leafmap.start_server(port=8000)
In [11]:
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 [12]:
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": "buildings",
"type": "fill",
"paint": {"fill-color": "#3388ff", "fill-opacity": 0.5},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(url, name="Buildings", show=True, zoom_to_layer=True, style=style)
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": "buildings",
"type": "fill",
"paint": {"fill-color": "#3388ff", "fill-opacity": 0.5},
},
],
}
# style = leafmap.pmtiles_style(url) # Use default style
m.add_pmtiles(url, name="Buildings", show=True, zoom_to_layer=True, style=style)
m
HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: /buildings.pmtiles (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f139877f980>: Failed to establish a new connection: [Errno 111] Connection refused'))
Out[12]: