| 1 |
|
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import csv |
| 5 |
import datetime |
| 6 |
import pathlib |
| 7 |
import random |
| 8 |
|
| 9 |
import shapefile |
| 10 |
from pygeoif.factories import force_3d |
| 11 |
from pygeoif.factories import shape |
| 12 |
|
| 13 |
import fastkml |
| 14 |
import fastkml.containers |
| 15 |
import fastkml.features |
| 16 |
import fastkml.styles |
| 17 |
import fastkml.times |
| 18 |
from fastkml.enums import AltitudeMode |
| 19 |
from fastkml.enums import DateTimeResolution |
| 20 |
from fastkml.geometry import create_kml_geometry |
| 21 |
|
| 22 |
examples_dir = pathlib.Path(__file__).parent |
| 23 |
|
| 24 |
shp = shapefile.Reader(examples_dir / "ne_110m_admin_0_countries.shp") |
| 25 |
|
| 26 |
co2_csv = pathlib.Path(examples_dir / "owid-co2-data.csv") |
| 27 |
|
| 28 |
co2_pa: dict[str, dict[str, float]] = {str(i): {} for i in range(1995, 2023)} |
| 29 |
|
| 30 |
with co2_csv.open() as csvfile: |
| 31 |
reader = csv.DictReader(csvfile) |
| 32 |
for row in reader: |
| 33 |
if row["year"] >= "1995": |
| 34 |
co2_pa[row["year"]][row["iso_code"]] = ( |
| 35 |
float(row["co2_per_capita"]) if row["co2_per_capita"] else 0 |
| 36 |
) |
| 37 |
|
| 38 |
styles = [] |
| 39 |
folders = [] |
| 40 |
for feature in shp.__geo_interface__["features"]: |
| 41 |
iso3_code = feature["properties"]["ADM0_ISO"] |
| 42 |
geometry = shape(feature["geometry"]) |
| 43 |
color = random.randint(0, 0xFFFFFF) |
| 44 |
styles.append( |
| 45 |
fastkml.styles.Style( |
| 46 |
id=iso3_code, |
| 47 |
styles=[ |
| 48 |
fastkml.styles.LineStyle(color=f"33{color:06X}", width=2), |
| 49 |
fastkml.styles.PolyStyle( |
| 50 |
color=f"88{color:06X}", |
| 51 |
fill=True, |
| 52 |
outline=True, |
| 53 |
), |
| 54 |
], |
| 55 |
), |
| 56 |
) |
| 57 |
style_url = fastkml.styles.StyleUrl(url=f"#{iso3_code}") |
| 58 |
folder = fastkml.containers.Folder(name=feature["properties"]["NAME"]) |
| 59 |
co2_growth = 0.0 |
| 60 |
for year in range(1995, 2023): |
| 61 |
co2_year = co2_pa[str(year)].get(iso3_code, 0.0) |
| 62 |
co2_growth += co2_year |
| 63 |
|
| 64 |
kml_geometry = create_kml_geometry( |
| 65 |
force_3d(geometry, co2_growth * 5_000), |
| 66 |
extrude=True, |
| 67 |
altitude_mode=AltitudeMode.relative_to_ground, |
| 68 |
) |
| 69 |
timespan = fastkml.times.TimeSpan( |
| 70 |
begin=fastkml.times.KmlDateTime( |
| 71 |
datetime.date(year, 1, 1), |
| 72 |
resolution=DateTimeResolution.year_month, |
| 73 |
), |
| 74 |
end=fastkml.times.KmlDateTime( |
| 75 |
datetime.date(year, 12, 31), |
| 76 |
resolution=DateTimeResolution.year_month, |
| 77 |
), |
| 78 |
) |
| 79 |
placemark = fastkml.features.Placemark( |
| 80 |
name=f"{feature['properties']['NAME']} - {year}", |
| 81 |
description=feature["properties"]["FORMAL_EN"], |
| 82 |
kml_geometry=kml_geometry, |
| 83 |
style_url=style_url, |
| 84 |
times=timespan, |
| 85 |
) |
| 86 |
folder.features.append(placemark) |
| 87 |
folders.append(folder) |
| 88 |
|
| 89 |
document = fastkml.containers.Document(features=folders, styles=styles) |
| 90 |
kml = fastkml.KML(features=[document]) |
| 91 |
|
| 92 |
outfile = pathlib.Path("co2_growth_1995_2022.kmz") |
| 93 |
kml.write(outfile, prettyprint=True, precision=3) |