Skip to content

Commit

Permalink
Fix namespace issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Nov 28, 2023
1 parent 40bf140 commit 426354a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 18 deletions.
32 changes: 27 additions & 5 deletions fastkml/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,32 @@ def _get_kwargs(
kwargs["features"] = []
folders = element.findall(f"{ns}Folder")
kwargs["features"] += [
Folder.class_from_element(ns=ns, element=folder, strict=strict)
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, element=placemark, strict=strict)
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, element=document, strict=strict)
Document.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=document,
strict=strict,
)
for document in documents
]
return kwargs
Expand Down Expand Up @@ -244,7 +259,9 @@ def etree_element(
element = super().etree_element(precision=precision, verbosity=verbosity)
if self._schemata is not None:
for schema in self._schemata:
element.append(schema.etree_element())
element.append(
schema.etree_element(precision=precision, verbosity=verbosity),
)
return element

def get_style_by_url(self, style_url: str) -> Optional[Union[Style, StyleMap]]:
Expand All @@ -268,7 +285,12 @@ def _get_kwargs(
)
schemata = element.findall(f"{ns}Schema")
kwargs["schemata"] = [
Schema.class_from_element(ns=ns, element=schema, strict=strict)
Schema.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=schema,
strict=strict,
)
for schema in schemata
]
return kwargs
14 changes: 12 additions & 2 deletions fastkml/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def _get_fields_kwargs_from_element(
cls,
*,
ns: str,
name_spaces: Optional[Dict[str, str]] = None,
element: Element,
strict: bool,
) -> List[SimpleField]:
Expand Down Expand Up @@ -191,6 +192,7 @@ def _get_kwargs(
kwargs["name"] = element.get("name")
kwargs["fields"] = cls._get_fields_kwargs_from_element(
ns=ns,
name_spaces=name_spaces,
element=element,
strict=strict,
)
Expand Down Expand Up @@ -397,7 +399,9 @@ def etree_element(
) -> Element:
element = super().etree_element(precision=precision, verbosity=verbosity)
for subelement in self.elements:
element.append(subelement.etree_element())
element.append(
subelement.etree_element(precision=precision, verbosity=verbosity),
)
return element

@classmethod
Expand All @@ -418,12 +422,18 @@ def _get_kwargs(
elements = []
untyped_data = element.findall(f"{ns}Data")
for ud in untyped_data:
el_data = Data.class_from_element(ns=ns, element=ud, strict=strict)
el_data = Data.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=ud,
strict=strict,
)
elements.append(el_data)
typed_data = element.findall(f"{ns}SchemaData")
for sd in typed_data:
el_schema_data = SchemaData.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=sd,
strict=strict,
)
Expand Down
27 changes: 21 additions & 6 deletions fastkml/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ def _get_kwargs(
element=element,
strict=strict,
)
name_spaces = kwargs["name_spaces"]
assert name_spaces is not None
name = element.find(f"{ns}name")
if name is not None:
kwargs["name"] = name.text
Expand Down Expand Up @@ -429,18 +431,18 @@ def _get_kwargs(
element=timestamp,
strict=strict,
)
atom_link = element.find(f"{atom.NS}link")
atom_link = element.find(f"{name_spaces['atom']}link")
if atom_link is not None:
kwargs["atom_link"] = atom.Link.class_from_element(
ns=atom.NS,
ns=name_spaces["atom"],
name_spaces=name_spaces,
element=atom_link,
strict=strict,
)
atom_author = element.find(f"{atom.NS}author")
atom_author = element.find(f"{name_spaces['atom']}author")
if atom_author is not None:
kwargs["atom_author"] = atom.Author.class_from_element(
ns=atom.NS,
ns=name_spaces["atom"],
name_spaces=name_spaces,
element=atom_author,
strict=strict,
Expand All @@ -449,6 +451,7 @@ def _get_kwargs(
if extended_data is not None:
kwargs["extended_data"] = ExtendedData.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=extended_data,
strict=strict,
)
Expand All @@ -462,13 +465,15 @@ def _get_kwargs(
if camera is not None:
kwargs["view"] = Camera.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=camera,
strict=strict,
)
lookat = element.find(f"{ns}LookAt")
if lookat is not None:
kwargs["view"] = LookAt.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=lookat,
strict=strict,
)
Expand Down Expand Up @@ -564,10 +569,13 @@ def _get_kwargs(
element=element,
strict=strict,
)
name_spaces = kwargs["name_spaces"]
assert name_spaces is not None
point = element.find(f"{ns}Point")
if point is not None:
kwargs["geometry"] = Point.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=point,
strict=strict,
)
Expand All @@ -576,6 +584,7 @@ def _get_kwargs(
if line is not None:
kwargs["geometry"] = LineString.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=line,
strict=strict,
)
Expand All @@ -584,6 +593,7 @@ def _get_kwargs(
if polygon is not None:
kwargs["geometry"] = Polygon.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=polygon,
strict=strict,
)
Expand All @@ -592,6 +602,7 @@ def _get_kwargs(
if linearring is not None:
kwargs["geometry"] = LinearRing.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=linearring,
strict=strict,
)
Expand All @@ -600,22 +611,25 @@ def _get_kwargs(
if multigeometry is not None:
kwargs["geometry"] = MultiGeometry.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=multigeometry,
strict=strict,
)
return kwargs
track = element.find(f"{ns}Track")
if track is not None:
kwargs["geometry"] = gx.Track.class_from_element(
ns=config.GXNS,
ns=name_spaces["gx"],
name_spaces=name_spaces,
element=track,
strict=strict,
)
return kwargs
multitrack = element.find(f"{ns}MultiTrack")
if multitrack is not None:
kwargs["geometry"] = gx.MultiTrack.class_from_element(
ns=config.GXNS,
ns=name_spaces["gx"],
name_spaces=name_spaces,
element=multitrack,
strict=strict,
)
Expand Down Expand Up @@ -763,6 +777,7 @@ def _get_kwargs(
element=element,
strict=strict,
)
name_spaces = kwargs["name_spaces"]
visibility = element.find(f"{ns}refreshVisibility")
if visibility is not None:
kwargs["refresh_visibility"] = bool(int(visibility.text))
Expand Down
1 change: 1 addition & 0 deletions fastkml/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ def etree_element(
element.append(
geometry_class(
ns=self.ns,
name_spaces=self.name_spaces,
extrude=None,
tessellate=None,
altitude_mode=None,
Expand Down
4 changes: 4 additions & 0 deletions fastkml/gx.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def _get_interpolate(
cls,
*,
ns: str,
name_spaces: Optional[Dict[str, str]] = None,
element: Element,
strict: bool,
) -> Optional[bool]:
Expand All @@ -417,6 +418,7 @@ def _get_track_kwargs_from_element(
cls,
*,
ns: str,
name_spaces: Optional[Dict[str, str]] = None,
element: Element,
strict: bool,
) -> List[Track]:
Expand Down Expand Up @@ -450,11 +452,13 @@ def _get_kwargs(
)
kwargs["interpolate"] = cls._get_interpolate(
ns=ns,
name_spaces=name_spaces,
element=element,
strict=strict,
)
kwargs["tracks"] = cls._get_track_kwargs_from_element(
ns=kwargs["name_spaces"].get("gx", ""),
name_spaces=name_spaces,
element=element,
strict=strict,
)
Expand Down
12 changes: 7 additions & 5 deletions fastkml/kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def _get_kwargs(
kwargs["features"].append(
Document.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=document,
strict=False,
),
Expand All @@ -136,6 +137,7 @@ def _get_kwargs(
kwargs["features"].append(
Folder.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=folder,
strict=False,
),
Expand All @@ -145,6 +147,7 @@ def _get_kwargs(
kwargs["features"].append(
Placemark.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=placemark,
strict=False,
),
Expand All @@ -154,6 +157,7 @@ def _get_kwargs(
kwargs["features"].append(
GroundOverlay.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=groundoverlay,
strict=False,
),
Expand All @@ -163,6 +167,7 @@ def _get_kwargs(
kwargs["features"].append(
PhotoOverlay.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=photo_overlay,
strict=False,
),
Expand Down Expand Up @@ -203,6 +208,8 @@ def class_from_string(
raise TypeError
if ns is None:
ns = cast(str, element.tag.rstrip("kml"))
name_spaces = name_spaces or {}
name_spaces = {**config.NAME_SPACES, **name_spaces}
return cls.class_from_element(
ns=ns,
name_spaces=name_spaces,
Expand All @@ -212,10 +219,5 @@ def class_from_string(


__all__ = [
"Document",
"Folder",
"PhotoOverlay",
"GroundOverlay",
"KML",
"Placemark",
]

0 comments on commit 426354a

Please sign in to comment.