diff --git a/examples/shp2kml.py b/examples/shp2kml.py index 1b0a1634..2535fbc6 100755 --- a/examples/shp2kml.py +++ b/examples/shp2kml.py @@ -68,5 +68,4 @@ kml = fastkml.KML(features=[document]) outfile = pathlib.Path("co2_per_capita_2020.kml") -with outfile.open("w") as f: - f.write(kml.to_string(prettyprint=True, precision=3)) +kml.write(outfile, prettyprint=True, precision=3) diff --git a/examples/shp2kml_timed.py b/examples/shp2kml_timed.py index 2f7dd343..e3f0dc8f 100755 --- a/examples/shp2kml_timed.py +++ b/examples/shp2kml_timed.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import annotations + import csv import datetime import pathlib @@ -23,7 +25,7 @@ co2_csv = pathlib.Path(examples_dir / "owid-co2-data.csv") -co2_pa = {str(i): {} for i in range(1995, 2023)} +co2_pa: dict[str, dict[str, float]] = {str(i): {} for i in range(1995, 2023)} with co2_csv.open() as csvfile: reader = csv.DictReader(csvfile) @@ -54,9 +56,9 @@ ) style_url = fastkml.styles.StyleUrl(url=f"#{iso3_code}") folder = fastkml.containers.Folder(name=feature["properties"]["NAME"]) - co2_growth = 0 + co2_growth = 0.0 for year in range(1995, 2023): - co2_year = co2_pa[str(year)].get(iso3_code, 0) + co2_year = co2_pa[str(year)].get(iso3_code, 0.0) co2_growth += co2_year kml_geometry = create_kml_geometry( @@ -87,6 +89,5 @@ document = fastkml.containers.Document(features=folders, styles=styles) kml = fastkml.KML(features=[document]) -outfile = pathlib.Path("co2_growth_1995_2022.kml") -with outfile.open("w") as f: - f.write(kml.to_string(prettyprint=True, precision=3)) +outfile = pathlib.Path("co2_growth_1995_2022.kmz") +kml.write(outfile, prettyprint=True, precision=3) diff --git a/examples/simple_example.py b/examples/simple_example.py index c5b9e139..183d856a 100755 --- a/examples/simple_example.py +++ b/examples/simple_example.py @@ -17,7 +17,6 @@ def print_child_features(element, depth=0): examples_dir = pathlib.Path(__file__).parent fname = pathlib.Path(examples_dir / "KML_Samples.kml") - with fname.open(encoding="utf-8") as kml_file: - k = kml.KML.from_string(kml_file.read().encode("utf-8")) + k = kml.KML.parse(fname) print_child_features(k)