Skip to content

Commit

Permalink
Merge pull request #46 from agrenott/dev
Browse files Browse the repository at this point in the history
Code clean-up and linter checks
  • Loading branch information
agrenott committed Jan 6, 2024
2 parents 4a9e258 + a43b0b3 commit 02ce676
Show file tree
Hide file tree
Showing 27 changed files with 876 additions and 531 deletions.
27 changes: 16 additions & 11 deletions pyhgtmap/contour.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from typing import List, Optional, Tuple, cast
from __future__ import annotations

from typing import TYPE_CHECKING, List, cast

import contourpy
import numpy
import numpy.typing
from pybind11_rdp import rdp

from pyhgtmap.hgt import TransformFunType
if TYPE_CHECKING:
from pyhgtmap.hgt import TransformFunType


def simplify_path(
input_path: numpy.ndarray, rdp_epsilon: Optional[float] = None
input_path: numpy.ndarray,
rdp_epsilon: float | None = None,
) -> numpy.ndarray:
"""Simplifies a path using the Ramer-Douglas-Peucker (RDP) algorithm.
Expand All @@ -29,19 +33,19 @@ def simplify_path(
return deduped_path


class ContoursGenerator(object):
class ContoursGenerator:
def __init__(
self,
cntr: contourpy.ContourGenerator,
max_nodes_per_way,
transform: Optional[TransformFunType],
transform: TransformFunType | None,
polygon=None,
rdp_epsilon=None,
) -> None:
self.cntr: contourpy.ContourGenerator = cntr
self.max_nodes_per_way = max_nodes_per_way
self.polygon = polygon
self.transform: Optional[TransformFunType] = transform
self.transform: TransformFunType | None = transform
self.rdp_epsilon = rdp_epsilon

def _cutBeginning(self, p):
Expand All @@ -60,7 +64,7 @@ def _cutBeginning(self, p):
else:
return self._cutBeginning(p[1:])

def splitList(self, input_list) -> Tuple[List[numpy.ndarray], int, int]:
def splitList(self, input_list) -> tuple[list[numpy.ndarray], int, int]:
"""splits a path to contain not more than self.maxNodesPerWay nodes.
A list of paths containing at least 2 (or, with closed paths, 3) nodes
Expand Down Expand Up @@ -112,15 +116,16 @@ def splitList(self, input_list) -> Tuple[List[numpy.ndarray], int, int]:
# Actually returns Tuple[List[numpy.typing.ArrayLike[numpy.typing.ArrayLike[numpy.float64]]], int, int]
# But can't be typed correctly yet...
# https://stackoverflow.com/questions/66657117/type-hint-2d-numpy-array
def trace(self, elevation: int) -> Tuple[List[numpy.ndarray], int, int]:
def trace(self, elevation: int) -> tuple[list[numpy.ndarray], int, int]:
"""this emulates matplotlib.cntr.Cntr's trace method.
The difference is that this method returns already split paths,
along with the number of nodes and paths as expected in the OSM
XML output. Also, consecutive identical nodes are removed.
"""
# Keep only the first element of the tuple, ignoring matplot line code
rawPaths: List[numpy.ndarray] = cast(
List[numpy.ndarray], self.cntr.create_contour(elevation)[0]
rawPaths: list[numpy.ndarray] = cast(
List[numpy.ndarray],
self.cntr.create_contour(elevation)[0],
)
numOfPaths, numOfNodes = 0, 0
resultPaths = []
Expand All @@ -138,7 +143,7 @@ def build_contours(
y: numpy.typing.ArrayLike,
z: numpy.typing.ArrayLike,
max_nodes_per_way: int,
transform: Optional[TransformFunType],
transform: TransformFunType | None,
polygon,
rdp_epsilon,
) -> ContoursGenerator:
Expand Down
17 changes: 10 additions & 7 deletions pyhgtmap/hgt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
from typing import Callable, Iterable, Optional, Tuple
from __future__ import annotations

from typing import Callable, Iterable, Tuple

# Coordinates transformation function prototype
TransformFunType = Callable[
[Iterable[Tuple[float, float]]], Iterable[Tuple[float, float]]
[Iterable[Tuple[float, float]]],
Iterable[Tuple[float, float]],
]


def makeBBoxString(bbox: Tuple[float, float, float, float]) -> str:
return "{{0:s}}lon{0[0]:.2f}_{0[2]:.2f}lat{0[1]:.2f}_{0[3]:.2f}".format(bbox)
def makeBBoxString(bbox: tuple[float, float, float, float]) -> str:
return f"{{0:s}}lon{bbox[0]:.2f}_{bbox[2]:.2f}lat{bbox[1]:.2f}_{bbox[3]:.2f}"


def transformLonLats(
minLon: float,
minLat: float,
maxLon: float,
maxLat: float,
transform: Optional[TransformFunType],
) -> Tuple[float, float, float, float]:
transform: TransformFunType | None,
) -> tuple[float, float, float, float]:
if transform is None:
return minLon, minLat, maxLon, maxLat
else:
(lon1, lat1), (lon2, lat2), (lon3, lat3), (lon4, lat4) = transform(
[(minLon, minLat), (maxLon, maxLat), (minLon, maxLat), (maxLon, maxLat)]
[(minLon, minLat), (maxLon, maxLat), (minLon, maxLat), (maxLon, maxLat)],
)
minLon = min([lon1, lon2, lon3, lon4])
maxLon = max([lon1, lon2, lon3, lon4])
Expand Down
Loading

0 comments on commit 02ce676

Please sign in to comment.