# screenshot: 1000x560
"""Графическая переменная «цветовой тон»: часть света, в которой находится город.

Часть света — качественная (номинальная) характеристика, порядка в ней нет.
Цветовой тон для неё подходит: он селективный (легко выделить все «красные»
города) и ассоциативный (разноцветные кружки одного размера всё равно
воспринимаются как одна группа — «города»). Размер у всех кружков одинаковый.
"""
import sys
from pathlib import Path

import folium
import geopandas as gpd
from branca.utilities import color_brewer

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from maptools import add_legend, add_title, plain_attribution

DATA = Path(__file__).resolve().parents[2] / "data"
cities = gpd.read_file(DATA / "cities.geojson")
world = gpd.read_file(DATA / "countries.geojson")

# часть света берём из таблицы стран по коду iso_a3
big = cities[cities["pop_max"] >= 1_000_000].merge(world[["iso_a3", "continent"]], on="iso_a3")
names = {"Asia": "Азия", "Europe": "Европа", "Africa": "Африка", "North America": "Северная Америка",
         "South America": "Южная Америка", "Oceania": "Австралия и Океания"}
colors = dict(zip(names, color_brewer("Dark2", 6)))   # качественная шкала ColorBrewer
print(big["continent"].map(names).value_counts().to_string())

m = folium.Map(location=[25, 20], zoom_start=2, tiles="Esri.WorldGrayCanvas")
for _, c in big.iterrows():
    folium.CircleMarker(
        [c.latitude, c.longitude], radius=4, color="white", weight=0.8,
        fill=True, fill_color=colors[c.continent], fill_opacity=0.95,
        tooltip=f"{c.name_ru} ({c.country_ru})",
    ).add_to(m)

add_legend(m, "Часть света", list(colors.values()), list(names.values()), shape="circle")
add_title(m, "Цветовой тон — качественные различия", "города мира с населением от 1 млн человек")
plain_attribution(m)  # убираем флажок из подписи Leaflet
m.save("02_hue.html")
