OpenLayers Choropleth Map¶
This notebook demonstrates choropleth (thematic) mapping with OpenLayers.
In [ ]:
Copied!
# %pip install anymap-ts geopandas
# %pip install anymap-ts geopandas
In [ ]:
Copied!
import json
from anymap_ts import OpenLayersMap
import json
from anymap_ts import OpenLayersMap
US States Choropleth¶
Load US states GeoJSON and color by population density.
In [ ]:
Copied!
import urllib.request
url = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
with urllib.request.urlopen(url) as response:
us_states = json.loads(response.read())
print(f"Loaded {len(us_states['features'])} states")
print("Properties:", list(us_states["features"][0]["properties"].keys()))
import urllib.request
url = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
with urllib.request.urlopen(url) as response:
us_states = json.loads(response.read())
print(f"Loaded {len(us_states['features'])} states")
print("Properties:", list(us_states["features"][0]["properties"].keys()))
In [ ]:
Copied!
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.Positron")
m.add_choropleth(
us_states,
column="density",
cmap="YlOrRd",
k=6,
classification="quantile",
name="population-density",
opacity=0.7,
legend=True,
)
m
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.Positron")
m.add_choropleth(
us_states,
column="density",
cmap="YlOrRd",
k=6,
classification="quantile",
name="population-density",
opacity=0.7,
legend=True,
)
m
Equal Interval Classification¶
In [ ]:
Copied!
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.Positron")
m.add_choropleth(
us_states,
column="density",
cmap="Blues",
k=5,
classification="equal_interval",
name="equal-interval",
stroke_color="#666",
stroke_width=0.5,
)
m
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.Positron")
m.add_choropleth(
us_states,
column="density",
cmap="Blues",
k=5,
classification="equal_interval",
name="equal-interval",
stroke_color="#666",
stroke_width=0.5,
)
m
Different Colormaps¶
In [ ]:
Copied!
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_choropleth(
us_states,
column="density",
cmap="viridis",
k=7,
classification="quantile",
name="viridis-choropleth",
opacity=0.8,
)
m
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_choropleth(
us_states,
column="density",
cmap="viridis",
k=7,
classification="quantile",
name="viridis-choropleth",
opacity=0.8,
)
m