Skip to content

Countries filter

image image image

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.

1
# %pip install "leafmap[maplibre]"
1
import leafmap.maplibregl as leafmap

To run this notebook, you will need an API key from MapTiler. Once you have the API key, you can uncomment the following code block and replace YOUR_API_KEY with your actual API key. Then, run the code block code to set the API key as an environment variable.

1
2
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
1
MAPTILER_KEY = leafmap.get_api_key("MAPTILER_KEY")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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