Skip to content

Commit

Permalink
#133 type annotate gx
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Oct 8, 2022
1 parent 57f957c commit 94ce71f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 62 deletions.
41 changes: 30 additions & 11 deletions fastkml/gx.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,58 +77,77 @@
"""

import logging
from typing import List
from typing import Optional
from typing import Union
from typing import cast

from pygeoif.geometry import GeometryCollection
from pygeoif.geometry import LineString
from pygeoif.geometry import MultiLineString
from pygeoif.types import PointType

from .config import GXNS as NS
from .geometry import Geometry
from fastkml.config import GXNS as NS
from fastkml.geometry import Geometry
from fastkml.types import Element

logger = logging.getLogger(__name__)


class GxGeometry(Geometry):
def __init__(
self,
ns=None,
id=None,
):
ns: None = None,
id: None = None,
) -> None:
"""
gxgeometry: a read-only subclass of geometry supporting gx: features,
like gx:Track
"""
super().__init__(ns, id)
self.ns = NS if ns is None else ns

def _get_geometry(self, element):
def _get_geometry(self, element: Element) -> Optional[LineString]:
# Track
if element.tag == (f"{self.ns}Track"):
coords = self._get_coordinates(element)
self._get_geometry_spec(element)
return LineString(coords)
return LineString(
coords,
)
return None

def _get_multigeometry(self, element):
def _get_multigeometry(
self,
element: Element,
) -> Union[MultiLineString, GeometryCollection, None]:
# MultiTrack
geoms = []
if element.tag == (f"{self.ns}MultiTrack"):
tracks = element.findall(f"{self.ns}Track")
for track in tracks:
self._get_geometry_spec(track)
geoms.append(LineString(self._get_coordinates(track)))
geoms.append(
LineString(
self._get_coordinates(track),
)
)

geom_types = {geom.geom_type for geom in geoms}
if len(geom_types) > 1:
return GeometryCollection(geoms)
if "LineString" in geom_types:
return MultiLineString.from_linestrings(*geoms)
return None

def _get_coordinates(self, element):
def _get_coordinates(self, element: Element) -> List[PointType]:
coordinates = element.findall(f"{self.ns}coord")
if coordinates is not None:
return [
[float(c) for c in coord.text.strip().split()] for coord in coordinates
cast(PointType, tuple(float(c) for c in coord.text.strip().split()))
for coord in coordinates
]
return [] # type: ignore[unreachable]


__all__ = ["GxGeometry"]
109 changes: 60 additions & 49 deletions fastkml/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
target_id: Optional[str] = None,
url: Optional[str] = None,
) -> None:
super().__init__(ns, id, target_id)
super().__init__(ns=ns, id=id, target_id=target_id)
self.url = url

def etree_element(self) -> Element:
Expand Down Expand Up @@ -109,21 +109,21 @@ def __init__(
color: Optional[str] = None,
color_mode: Optional[str] = None,
) -> None:
super().__init__(ns, id, target_id)
super().__init__(ns=ns, id=id, target_id=target_id)
self.color = color
self.color_mode = color_mode

def etree_element(self) -> Element:
element = super().etree_element()
if self.color:
color = config.etree.SubElement(
element, # type: ignore[arg-type]
color = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}color",
)
color.text = self.color
if self.color_mode:
color_mode = config.etree.SubElement(
element, # type: ignore[arg-type]
color_mode = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}colorMode",
)
color_mode.text = self.color_mode
Expand Down Expand Up @@ -170,9 +170,12 @@ def __init__(
scale: float = 1.0,
heading: Optional[float] = None,
icon_href: Optional[str] = None,
hot_spot: HotSpot = None,
hot_spot: Optional[HotSpot] = None,
) -> None:
super().__init__(ns, id, target_id, color, color_mode)
super().__init__(
ns=ns, id=id, target_id=target_id, color=color, color_mode=color_mode
)

self.scale = scale
self.heading = heading
self.icon_href = icon_href
Expand All @@ -181,29 +184,31 @@ def __init__(
def etree_element(self) -> Element:
element = super().etree_element()
if self.scale is not None:
scale = config.etree.SubElement(
element, # type: ignore[arg-type]
scale = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}scale",
)
scale.text = str(self.scale)
if self.heading is not None:
heading = config.etree.SubElement(
element, # type: ignore[arg-type]
heading = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}heading",
)
heading.text = str(self.heading)
if self.icon_href:
icon = config.etree.SubElement(
element, # type: ignore[arg-type]
icon = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}Icon",
)
href = config.etree.SubElement(
href = config.etree.SubElement( # type: ignore[attr-defined]
icon,
f"{self.ns}href",
)
href.text = self.icon_href
if self.hot_spot:
hot_spot = config.etree.SubElement(element, f"{self.ns}hotSpot")
hot_spot = config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}hotSpot"
)
hot_spot.attrib["x"] = str(self.hot_spot["x"])
hot_spot.attrib["y"] = str(self.hot_spot["y"])
hot_spot.attrib["xunits"] = str(self.hot_spot["xunits"])
Expand All @@ -225,11 +230,11 @@ def from_element(self, element: Element) -> None:
self.icon_href = href.text
hot_spot = element.find(f"{self.ns}hotSpot")
if hot_spot is not None:
self.hot_spot: HotSpot = {
"x": hot_spot.attrib["x"],
"y": hot_spot.attrib["y"],
"xunits": hot_spot.attrib["xunits"],
"yunits": hot_spot.attrib["yunits"],
self.hot_spot: HotSpot = { # type: ignore[no-redef]
"x": hot_spot.attrib["x"], # type: ignore[attr-defined]
"y": hot_spot.attrib["y"], # type: ignore[attr-defined]
"xunits": hot_spot.attrib["xunits"], # type: ignore[attr-defined]
"yunits": hot_spot.attrib["yunits"], # type: ignore[attr-defined]
}


Expand All @@ -254,14 +259,16 @@ def __init__(
color_mode: Optional[str] = None,
width: Union[int, float] = 1,
) -> None:
super().__init__(ns, id, target_id, color, color_mode)
super().__init__(
ns=ns, id=id, target_id=target_id, color=color, color_mode=color_mode
)
self.width = width

def etree_element(self) -> Element:
element = super().etree_element()
if self.width is not None:
width = config.etree.SubElement(
element, # type: ignore[arg-type]
width = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}width",
)
width.text = str(self.width)
Expand Down Expand Up @@ -298,21 +305,23 @@ def __init__(
fill: int = 1,
outline: int = 1,
) -> None:
super().__init__(ns, id, target_id, color, color_mode)
super().__init__(
ns=ns, id=id, target_id=target_id, color=color, color_mode=color_mode
)
self.fill = fill
self.outline = outline

def etree_element(self) -> Element:
element = super().etree_element()
if self.fill is not None:
fill = config.etree.SubElement(
element, # type: ignore[arg-type]
fill = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}fill",
)
fill.text = str(self.fill)
if self.outline is not None:
outline = config.etree.SubElement(
element, # type: ignore[arg-type]
outline = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}outline",
)
outline.text = str(self.outline)
Expand Down Expand Up @@ -354,14 +363,16 @@ def __init__(
color_mode: Optional[str] = None,
scale: float = 1.0,
) -> None:
super().__init__(ns, id, target_id, color, color_mode)
super().__init__(
ns=ns, id=id, target_id=target_id, color=color, color_mode=color_mode
)
self.scale = scale

def etree_element(self) -> Element:
element = super().etree_element()
if self.scale is not None:
scale = config.etree.SubElement(
element, # type: ignore[arg-type]
scale = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}scale",
)
scale.text = str(self.scale)
Expand Down Expand Up @@ -435,7 +446,7 @@ def __init__(
text: Optional[str] = None,
display_mode: Optional[str] = None,
) -> None:
super().__init__(ns, id, target_id)
super().__init__(ns=ns, id=id, target_id=target_id)
self.bg_color = bg_color
self.text_color = text_color
self.text = text
Expand Down Expand Up @@ -463,26 +474,26 @@ def from_element(self, element: Element) -> None:
def etree_element(self) -> Element:
element = super().etree_element()
if self.bg_color is not None:
elem = config.etree.SubElement(
element, # type: ignore[arg-type]
elem = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}bgColor",
)
elem.text = self.bg_color
if self.text_color is not None:
elem = config.etree.SubElement(
element, # type: ignore[arg-type]
elem = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}textColor",
)
elem.text = self.text_color
if self.text is not None:
elem = config.etree.SubElement(
element, # type: ignore[arg-type]
elem = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}text",
)
elem.text = self.text
if self.display_mode is not None:
elem = config.etree.SubElement(
element, # type: ignore[arg-type]
elem = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}displayMode",
)
elem.text = self.display_mode
Expand Down Expand Up @@ -611,27 +622,27 @@ def from_element(self, element: Element) -> None:
def etree_element(self) -> Element:
element = super().etree_element()
if self.normal and isinstance(self.normal, (Style, StyleUrl)):
pair = config.etree.SubElement(
element, # type: ignore[arg-type]
pair = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}Pair",
)
key = config.etree.SubElement(
key = config.etree.SubElement( # type: ignore[attr-defined]
pair,
f"{self.ns}key",
)
key.text = "normal"
pair.append(self.normal.etree_element()) # type: ignore[arg-type]
pair.append(self.normal.etree_element())
if self.highlight and isinstance(self.highlight, (Style, StyleUrl)):
pair = config.etree.SubElement(
element, # type: ignore[arg-type]
pair = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}Pair",
)
key = config.etree.SubElement(
key = config.etree.SubElement( # type: ignore[attr-defined]
pair,
f"{self.ns}key",
)
key.text = "highlight"
pair.append(self.highlight.etree_element()) # type: ignore[arg-type]
pair.append(self.highlight.etree_element())
return element


Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ show_error_codes = true

[[tool.mypy.overrides]]
module = [
"fastkml.kml", "fastkml.gx",
"fastkml.styles",
"fastkml.kml",
"fastkml.tests.oldunit_test", "fastkml.tests.config_test"]
ignore_errors = true

0 comments on commit 94ce71f

Please sign in to comment.