Skip to content

Commit

Permalink
Merge pull request #265 from cleder/pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
[pre-commit.ci] pre-commit autoupdate
  • Loading branch information
cleder committed Nov 6, 2023
2 parents 89a3918 + df46955 commit a1794ff
Show file tree
Hide file tree
Showing 26 changed files with 201 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.3'
rev: 'v0.1.4'
hooks:
- id: ruff
- repo: https://github.com/PyCQA/flake8
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
("index", "fastkml", "FastKML Documentation", ["Christian Ledermann & Ian Lee"], 1)
("index", "fastkml", "FastKML Documentation", ["Christian Ledermann & Ian Lee"], 1),
]

# If true, show URL addresses after external links.
Expand Down
2 changes: 1 addition & 1 deletion examples/CreateKml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Create the root KML object
k = kml.KML()
ns = "{http://www.opengis.net/kml/2.2}" # noqa: FS003
ns = "{http://www.opengis.net/kml/2.2}"

# Create a KML Document and add it to the KML root object
d = kml.Document(ns, "docid", "doc name", "doc description")
Expand Down
13 changes: 8 additions & 5 deletions fastkml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def etree_element(
"""Return the KML Object as an Element."""
if self.__name__:
element: Element = config.etree.Element( # type: ignore[attr-defined]
f"{self.ns}{self.__name__}"
f"{self.ns}{self.__name__}",
)
else:
raise NotImplementedError(
"Call of abstract base class, subclasses implement this!"
"Call of abstract base class, subclasses implement this!",
)
for mapping in self.kml_object_mapping:
mapping["to_kml"](self, element, **mapping)
Expand All @@ -98,7 +98,7 @@ def from_string(self, xml_string: str) -> None:
making it a classmethod.
"""
self.from_element(
cast(Element, config.etree.XML(xml_string)) # type: ignore[attr-defined]
cast(Element, config.etree.XML(xml_string)), # type: ignore[attr-defined]
)

def to_string(
Expand All @@ -125,7 +125,7 @@ def to_string(
return cast(
str,
config.etree.tostring( # type: ignore[attr-defined]
self.etree_element(), encoding="UTF-8"
self.etree_element(), encoding="UTF-8",
).decode("UTF-8"),
)

Expand Down Expand Up @@ -168,12 +168,15 @@ def class_from_string(
ns: Optional[str] = None,
strict: bool = True,
) -> "_XMLObject":
"""Creates a geometry object from a string.
"""
Creates a geometry object from a string.
Args:
----
string: String representation of the geometry object
Returns:
-------
Geometry object
"""
ns = cls._get_ns(ns)
Expand Down
22 changes: 12 additions & 10 deletions fastkml/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Add Custom Data
"""
Add Custom Data
https://developers.google.com/kml/documentation/extendeddata#example
"""
Expand All @@ -27,7 +28,7 @@
from typing import Tuple
from typing import Union

import fastkml.config as config
from fastkml import config
from fastkml.base import _BaseObject
from fastkml.base import _XMLObject
from fastkml.enums import DataType
Expand Down Expand Up @@ -135,13 +136,13 @@ def etree_element(
element.set("name", self.name)
for simple_field in self.simple_fields:
sf = config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}SimpleField"
element, f"{self.ns}SimpleField",
)
sf.set("type", simple_field.type.value)
sf.set("name", simple_field.name)
if simple_field.display_name:
dn = config.etree.SubElement( # type: ignore[attr-defined]
sf, f"{self.ns}displayName"
sf, f"{self.ns}displayName",
)
dn.text = simple_field.display_name
return element
Expand Down Expand Up @@ -179,7 +180,7 @@ def _get_kwargs(
kwargs = super()._get_kwargs(ns=ns, element=element, strict=strict)
kwargs["name"] = element.get("name")
kwargs["fields"] = cls._get_fields_kwargs_from_element(
ns=ns, element=element, strict=strict
ns=ns, element=element, strict=strict,
)
return kwargs

Expand Down Expand Up @@ -218,12 +219,12 @@ def etree_element(
element = super().etree_element(precision=precision, verbosity=verbosity)
element.set("name", self.name or "")
value = config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}value"
element, f"{self.ns}value",
)
value.text = self.value
if self.display_name:
display_name = config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}displayName"
element, f"{self.ns}displayName",
)
display_name.text = self.display_name
return element
Expand Down Expand Up @@ -308,7 +309,7 @@ def etree_element(
element.set("schemaUrl", self.schema_url)
for data in self.data:
sd = config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}SimpleData"
element, f"{self.ns}SimpleData",
)
sd.set("name", data.name)
sd.text = data.value
Expand All @@ -333,7 +334,8 @@ def _get_kwargs(


class ExtendedData(_XMLObject):
"""Represents a list of untyped name/value pairs. See docs:
"""
Represents a list of untyped name/value pairs. See docs:
-> 'Adding Untyped Name/Value Pairs'
https://developers.google.com/kml/documentation/extendeddata
Expand Down Expand Up @@ -384,7 +386,7 @@ def _get_kwargs(
typed_data = element.findall(f"{ns}SchemaData")
for sd in typed_data:
el_schema_data = SchemaData.class_from_element(
ns=ns, element=sd, strict=strict
ns=ns, element=sd, strict=strict,
)
elements.append(el_schema_data)
kwargs["elements"] = elements
Expand Down
37 changes: 21 additions & 16 deletions fastkml/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@

GeometryType = Union[geo.Polygon, geo.LineString, geo.LinearRing, geo.Point]
MultiGeometryType = Union[
geo.MultiPoint, geo.MultiLineString, geo.MultiPolygon, geo.GeometryCollection
geo.MultiPoint, geo.MultiLineString, geo.MultiPolygon, geo.GeometryCollection,
]
AnyGeometryType = Union[GeometryType, MultiGeometryType]


class _Geometry(_BaseObject):
"""Baseclass with common methods for all geometry objects.
"""
Baseclass with common methods for all geometry objects.
Attributes: extrude: boolean --> Specifies whether to connect the feature to
the ground with a line.
Expand All @@ -84,6 +85,7 @@ def __init__(
"""
Args:
----
ns: Namespace of the object
id: Id of the object
target_id: Target id of the object
Expand Down Expand Up @@ -159,7 +161,7 @@ def _etree_coordinates(
def _set_altitude_mode(self, element: Element) -> None:
if self.altitude_mode:
am_element = config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}altitudeMode"
element, f"{self.ns}altitudeMode",
)
am_element.text = self.altitude_mode.value

Expand All @@ -168,7 +170,7 @@ def _set_extrude(self, element: Element) -> None:
et_element = cast(
Element,
config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}extrude"
element, f"{self.ns}extrude",
),
)
et_element.text = str(int(self.extrude))
Expand All @@ -178,7 +180,7 @@ def _set_tessellate(self, element: Element) -> None:
t_element = cast(
Element,
config.etree.SubElement( # type: ignore[attr-defined]
element, f"{self.ns}tessellate"
element, f"{self.ns}tessellate",
),
)
t_element.text = str(int(self.tessellate))
Expand All @@ -197,7 +199,7 @@ def etree_element(

@classmethod
def _get_coordinates(
cls, *, ns: str, element: Element, strict: bool
cls, *, ns: str, element: Element, strict: bool,
) -> List[PointType]:
"""
Get coordinates from element.
Expand Down Expand Up @@ -279,7 +281,7 @@ def _get_geometry_kwargs(
"extrude": cls._get_extrude(ns=ns, element=element, strict=strict),
"tessellate": cls._get_tessellate(ns=ns, element=element, strict=strict),
"altitude_mode": cls._get_altitude_mode(
ns=ns, element=element, strict=strict
ns=ns, element=element, strict=strict,
),
}

Expand All @@ -304,7 +306,7 @@ def _get_kwargs(
kwargs = super()._get_kwargs(ns=ns, element=element, strict=strict)
kwargs.update(cls._get_geometry_kwargs(ns=ns, element=element, strict=strict))
kwargs.update(
{"geometry": cls._get_geometry(ns=ns, element=element, strict=strict)}
{"geometry": cls._get_geometry(ns=ns, element=element, strict=strict)},
)
return kwargs

Expand Down Expand Up @@ -496,8 +498,8 @@ def etree_element(
)
outer_boundary.append(
linear_ring(geometry=self.geometry.exterior).etree_element(
precision=precision, verbosity=verbosity
)
precision=precision, verbosity=verbosity,
),
)
for interior in self.geometry.interiors:
inner_boundary = cast(
Expand All @@ -509,8 +511,8 @@ def etree_element(
)
inner_boundary.append(
linear_ring(geometry=interior).etree_element(
precision=precision, verbosity=verbosity
)
precision=precision, verbosity=verbosity,
),
)
return element

Expand Down Expand Up @@ -541,20 +543,23 @@ def _get_geometry(cls, *, ns: str, element: Element, strict: bool) -> geo.Polygo
).decode("UTF-8")
raise KMLParseError(f"Missing LinearRing in {error}")
interiors.append(
LinearRing._get_geometry(ns=ns, element=inner_ring, strict=strict)
LinearRing._get_geometry(ns=ns, element=inner_ring, strict=strict),
)
return geo.Polygon.from_linear_rings(exterior, *interiors)


def create_multigeometry(
geometries: Sequence[AnyGeometryType],
) -> Optional[MultiGeometryType]:
"""Create a MultiGeometry from a sequence of geometries.
"""
Create a MultiGeometry from a sequence of geometries.
Args:
----
geometries: Sequence of geometries.
Returns:
-------
MultiGeometry
"""
Expand Down Expand Up @@ -633,13 +638,13 @@ def etree_element(
tessellate=None,
altitude_mode=None,
geometry=geometry, # type: ignore[arg-type]
).etree_element(precision=precision, verbosity=verbosity)
).etree_element(precision=precision, verbosity=verbosity),
)
return element

@classmethod
def _get_geometry(
cls, *, ns: str, element: Element, strict: bool
cls, *, ns: str, element: Element, strict: bool,
) -> Optional[MultiGeometryType]:
geometries = []
allowed_geometries = (cls,) + tuple(cls.map_to_kml.values())
Expand Down

0 comments on commit a1794ff

Please sign in to comment.