Skip to content

Commit

Permalink
Refactor feature parsing in containers.py and kml.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Dec 12, 2023
1 parent 7af7903 commit 8c8d92d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 82 deletions.
44 changes: 14 additions & 30 deletions fastkml/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,36 +143,20 @@ def _get_kwargs(
strict=strict,
)
kwargs["features"] = []
folders = element.findall(f"{ns}Folder")
kwargs["features"] += [
Folder.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=folder,
strict=strict,
)
for folder in folders
]
placemarks = element.findall(f"{ns}Placemark")
kwargs["features"] += [
Placemark.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=placemark,
strict=strict,
)
for placemark in placemarks
]
documents = element.findall(f"{ns}Document")
kwargs["features"] += [
Document.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=document,
strict=strict,
)
for document in documents
]
for klass in (
Folder,
Placemark,
Document,
):
for feature in element.findall(f"{ns}{klass.__name__}"):
kwargs["features"].append(
klass.class_from_element( # type: ignore[attr-defined]
ns=ns,
name_spaces=name_spaces,
element=feature,
strict=strict,
),
)
return kwargs


Expand Down
2 changes: 1 addition & 1 deletion fastkml/gx.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Angle:
roll: float = 0.0


@dataclass(frozen=True)
@dataclass(frozen=True) # XXX replace with _XMLObject
class TrackItem:
"""A track item describes an objects position and heading at a specific time."""

Expand Down
63 changes: 12 additions & 51 deletions fastkml/kml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2012-2022 Christian Ledermann
# Copyright (C) 2012-2023 Christian Ledermann
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -122,56 +122,17 @@ def _get_kwargs(
strict=strict,
)
kwargs["features"] = []
documents = element.findall(f"{ns}Document")
for document in documents:
kwargs["features"].append(
Document.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=document,
strict=False,
),
)
folders = element.findall(f"{ns}Folder")
for folder in folders:
kwargs["features"].append(
Folder.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=folder,
strict=False,
),
)
placemarks = element.findall(f"{ns}Placemark")
for placemark in placemarks:
kwargs["features"].append(
Placemark.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=placemark,
strict=False,
),
)
groundoverlays = element.findall(f"{ns}GroundOverlay")
for groundoverlay in groundoverlays:
kwargs["features"].append(
GroundOverlay.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=groundoverlay,
strict=False,
),
)
photo_overlays = element.findall(f"{ns}PhotoOverlay")
for photo_overlay in photo_overlays:
kwargs["features"].append(
PhotoOverlay.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=photo_overlay,
strict=False,
),
)
for klass in (Document, Folder, Placemark, GroundOverlay, PhotoOverlay):
for feature in element.findall(f"{ns}{klass.__name__}"):
kwargs["features"].append(
klass.class_from_element( # type: ignore[attr-defined]
ns=ns,
name_spaces=name_spaces,
element=feature,
strict=strict,
),
)

return kwargs

@classmethod
Expand Down

0 comments on commit 8c8d92d

Please sign in to comment.