Skip to content

Commit

Permalink
Refactor KML writing in examples to use the new write method and simp…
Browse files Browse the repository at this point in the history
…lify file handling
  • Loading branch information
cleder committed Nov 28, 2024
1 parent 49ead8f commit 6807966
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
3 changes: 1 addition & 2 deletions examples/shp2kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 7 additions & 6 deletions examples/shp2kml_timed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python
from __future__ import annotations

import csv
import datetime
import pathlib
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
3 changes: 1 addition & 2 deletions examples/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 6807966

Please sign in to comment.