Add multiple geometries from one GeoJSON source
This source code of this example is adapted from the MapLibre GL JS example - Add multiple geometries from one GeoJSON source.
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# %pip install "leafmap[maplibre]"
# %pip install "leafmap[maplibre]"
In [2]:
Copied!
import leafmap.maplibregl as leafmap
import leafmap.maplibregl as leafmap
In [3]:
Copied!
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
In [4]:
Copied!
m = leafmap.Map(center=[-121.403732, 40.492392], zoom=10, style="streets")
source = {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-121.353637, 40.584978],
[-121.284551, 40.584758],
[-121.275349, 40.541646],
[-121.246768, 40.541017],
[-121.251343, 40.423383],
[-121.32687, 40.423768],
[-121.360619, 40.43479],
[-121.363694, 40.409124],
[-121.439713, 40.409197],
[-121.439711, 40.423791],
[-121.572133, 40.423548],
[-121.577415, 40.550766],
[-121.539486, 40.558107],
[-121.520284, 40.572459],
[-121.487219, 40.550822],
[-121.446951, 40.56319],
[-121.370644, 40.563267],
[-121.353637, 40.584978],
]
],
},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-121.415061, 40.506229]},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-121.505184, 40.488084]},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-121.354465, 40.488737]},
},
],
},
}
m.add_source("national-park", source)
polygon_layer = {
"id": "park-boundary",
"type": "fill",
"source": "national-park",
"paint": {"fill-color": "#888888", "fill-opacity": 0.4},
"filter": ["==", "$type", "Polygon"],
}
point_layer = {
"id": "park-volcanoes",
"type": "circle",
"source": "national-park",
"paint": {"circle-radius": 6, "circle-color": "#B42222"},
"filter": ["==", "$type", "Point"],
}
m.add_layer(polygon_layer)
m.add_layer(point_layer)
m
m = leafmap.Map(center=[-121.403732, 40.492392], zoom=10, style="streets")
source = {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-121.353637, 40.584978],
[-121.284551, 40.584758],
[-121.275349, 40.541646],
[-121.246768, 40.541017],
[-121.251343, 40.423383],
[-121.32687, 40.423768],
[-121.360619, 40.43479],
[-121.363694, 40.409124],
[-121.439713, 40.409197],
[-121.439711, 40.423791],
[-121.572133, 40.423548],
[-121.577415, 40.550766],
[-121.539486, 40.558107],
[-121.520284, 40.572459],
[-121.487219, 40.550822],
[-121.446951, 40.56319],
[-121.370644, 40.563267],
[-121.353637, 40.584978],
]
],
},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-121.415061, 40.506229]},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-121.505184, 40.488084]},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-121.354465, 40.488737]},
},
],
},
}
m.add_source("national-park", source)
polygon_layer = {
"id": "park-boundary",
"type": "fill",
"source": "national-park",
"paint": {"fill-color": "#888888", "fill-opacity": 0.4},
"filter": ["==", "$type", "Polygon"],
}
point_layer = {
"id": "park-volcanoes",
"type": "circle",
"source": "national-park",
"paint": {"circle-radius": 6, "circle-color": "#B42222"},
"filter": ["==", "$type", "Point"],
}
m.add_layer(polygon_layer)
m.add_layer(point_layer)
m
Failed to retrieve the MapTiler style. Defaulting to 'dark-matter' style.