Add a pattern to a polygon
This source code of this example is adapted from the MapLibre GL JS example - Add a pattern to a polygon.
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(zoom=1, style="streets")
source = {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[-30, -25], [-30, 35], [30, 35], [30, -25], [-30, -25]]],
},
},
}
m.add_source("source", source)
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/64px-Cat_silhouette.svg.png"
m.add_image("pattern", url)
layer = {
"id": "pattern-layer",
"type": "fill",
"source": "source",
"paint": {"fill-pattern": "pattern"},
}
m.add_layer(layer)
m
m = leafmap.Map(zoom=1, style="streets")
source = {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[-30, -25], [-30, 35], [30, 35], [30, -25], [-30, -25]]],
},
},
}
m.add_source("source", source)
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/64px-Cat_silhouette.svg.png"
m.add_image("pattern", url)
layer = {
"id": "pattern-layer",
"type": "fill",
"source": "source",
"paint": {"fill-pattern": "pattern"},
}
m.add_layer(layer)
m
Failed to retrieve the MapTiler style. Defaulting to 'dark-matter' style.