How to use and filter data for MapTiler Countries
This source code of this example is adapted from the MapTiler SDK JS example - How to use and filter data for MapTiler Countries.
This tutorial shows the process of utilizing and refining data from the MapTiler Countries to create a Choropleth map of the US states. The MapTiler Countries dataset primarily consists of information regarding the administrative divisions within various countries and their respective territories.
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!
MAPTILER_KEY = leafmap.get_api_key("MAPTILER_KEY")
MAPTILER_KEY = leafmap.get_api_key("MAPTILER_KEY")
In [5]:
Copied!
m = leafmap.Map(center=[-94.28, 38.45], zoom=3, style="streets")
source = {
"type": "vector",
"url": f"https://api.maptiler.com/tiles/countries/tiles.json?key={MAPTILER_KEY}",
}
m.add_source("statesData", source)
layer = {
"id": "US_states",
"source": "statesData",
"source-layer": "administrative",
"type": "fill",
"filter": ["all", ["==", "level", 1], ["==", "level_0", "US"]],
"paint": {
"fill-color": [
"match",
["get", "name"],
[
"Nebraska",
"Alaska",
"Washington",
"Nevada",
"New Mexico",
"Montana",
"Minnesota",
"Louisiana",
"North Carolina",
"Kentucky",
"Massachusetts",
"Delaware",
"Michigan",
],
"#D5CD85",
[
"Oklahoma",
"Florida",
"Idaho",
"Wisconsin",
"Arizona",
"Tennessee",
"Pennsylvania",
"New Hampshire",
"Rhode Island",
],
"#D58785",
[
"New York",
"California",
"Wyoming",
"Kansas",
"Illinois",
"Mississippi",
"South Carolina",
"West Virginia",
],
"#735F91",
[
"Texas",
"Georgia",
"Utah",
"Missouri",
"South Dakota",
"Ohio",
"Maryland",
"Vermont",
],
"#567986",
[
"Colorado",
"Oregon",
"Alabama",
"Indiana",
"North Dakota",
"Iowa",
"Arkansas",
"Virginia",
"New Jersey",
"Maine",
"Connecticut",
],
"#69A86D",
"rgba(0, 0, 0, 0.5)",
],
"fill-opacity": 1,
"fill-outline-color": "#000",
},
}
first_symbol_layer_id = m.find_first_symbol_layer()["id"]
m.add_layer(layer, before_id=first_symbol_layer_id)
m.add_layer_control()
m
m = leafmap.Map(center=[-94.28, 38.45], zoom=3, style="streets")
source = {
"type": "vector",
"url": f"https://api.maptiler.com/tiles/countries/tiles.json?key={MAPTILER_KEY}",
}
m.add_source("statesData", source)
layer = {
"id": "US_states",
"source": "statesData",
"source-layer": "administrative",
"type": "fill",
"filter": ["all", ["==", "level", 1], ["==", "level_0", "US"]],
"paint": {
"fill-color": [
"match",
["get", "name"],
[
"Nebraska",
"Alaska",
"Washington",
"Nevada",
"New Mexico",
"Montana",
"Minnesota",
"Louisiana",
"North Carolina",
"Kentucky",
"Massachusetts",
"Delaware",
"Michigan",
],
"#D5CD85",
[
"Oklahoma",
"Florida",
"Idaho",
"Wisconsin",
"Arizona",
"Tennessee",
"Pennsylvania",
"New Hampshire",
"Rhode Island",
],
"#D58785",
[
"New York",
"California",
"Wyoming",
"Kansas",
"Illinois",
"Mississippi",
"South Carolina",
"West Virginia",
],
"#735F91",
[
"Texas",
"Georgia",
"Utah",
"Missouri",
"South Dakota",
"Ohio",
"Maryland",
"Vermont",
],
"#567986",
[
"Colorado",
"Oregon",
"Alabama",
"Indiana",
"North Dakota",
"Iowa",
"Arkansas",
"Virginia",
"New Jersey",
"Maine",
"Connecticut",
],
"#69A86D",
"rgba(0, 0, 0, 0.5)",
],
"fill-opacity": 1,
"fill-outline-color": "#000",
},
}
first_symbol_layer_id = m.find_first_symbol_layer()["id"]
m.add_layer(layer, before_id=first_symbol_layer_id)
m.add_layer_control()
m
Failed to retrieve the MapTiler style. Defaulting to OpenFreeMap 'liberty' style.