Skip to content

Commit

Permalink
Properly transform coordinates for geotiff contours
Browse files Browse the repository at this point in the history
Fix #47
  • Loading branch information
agrenott committed Jan 10, 2024
1 parent 9a2321c commit bee94cf
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
2 changes: 2 additions & 0 deletions pyhgtmap/hgt/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def trace(self, elevation: int) -> tuple[list[numpy.ndarray], int, int]:
numOfPaths, numOfNodes = 0, 0
resultPaths = []
for path in rawPaths:
if self.transform:
path = numpy.array(self.transform(path))
path = simplify_path(path, self.rdp_epsilon)
splitPaths, numOfNodesAdd, numOfPathsAdd = self.splitList(path)
resultPaths.extend(splitPaths)
Expand Down
Binary file modified tests/data/toulon_ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/toulon_ref_smoothed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/toulon_ref_transformed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions tests/hgt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import contextlib
import importlib.util
from typing import Generator


@contextlib.contextmanager
def handle_optional_geotiff_support() -> Generator[None, None, None]:
"""
Context manager handling the cases where optional GeoTiff support has an impact.
Cases should run fully if geotiff dependencies are available, else specific exception is
expected.
"""
try:
# Execute test case
yield
except ImportError as ex:
if importlib.util.find_spec("osgeo") is not None:
# GDAL module is available, do NOT ignore the exception
raise
# GDAL not available, ensure the proper errror message is raised
assert ( # noqa: PT017 # Test is more complex
ex.msg
== "GeoTiff optional support not enabled; please install with 'pip install pyhgtmap[geotiff]'"
)
26 changes: 2 additions & 24 deletions tests/hgt/test_file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

import contextlib
import importlib.util
import os
from typing import TYPE_CHECKING, Generator
from typing import TYPE_CHECKING

import numpy
import pytest
Expand All @@ -18,6 +16,7 @@
polygon_mask,
)
from tests import TEST_DATA_PATH
from tests.hgt import handle_optional_geotiff_support

if TYPE_CHECKING:
from pyhgtmap import BBox
Expand Down Expand Up @@ -69,27 +68,6 @@ def toulon_tiles_smoothed() -> list[HgtTile]:
)


@contextlib.contextmanager
def handle_optional_geotiff_support() -> Generator[None, None, None]:
"""
Context manager handling the cases where optional GeoTiff support has an impact.
Cases should run fully if geotiff dependencies are available, else specific exception is
expected.
"""
try:
# Execute test case
yield
except ImportError as ex:
if importlib.util.find_spec("osgeo") is not None:
# GDAL module is available, do NOT ignore the exception
raise
# GDAL not available, ensure the proper errror message is raised
assert ( # noqa: PT017 # Test is more complex
ex.msg
== "GeoTiff optional support not enabled; please install with 'pip install pyhgtmap[geotiff]'"
)


class TestHgtFile:
@staticmethod
def test_make_tiles_chopped() -> None:
Expand Down
39 changes: 24 additions & 15 deletions tests/hgt/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def toulon_tiles_smoothed() -> list[HgtTile]:
@pytest.fixture()
def toulon_tiles_transformed() -> list[HgtTile]:
"""Toulon tiles in 3857 transformed coordinates."""
# Skip any test using this fixture if GDAL is not installed
pytest.importorskip("osgeo")
return toulon_tiles(smooth_ratio=1, file_name="N43E006_3857.tiff")


Expand Down Expand Up @@ -159,19 +161,20 @@ def test_draw_contours_Toulon(
"""
return TestHgtTile._test_draw_contours(toulon_tiles_raw, rdp_epsilon)

# @staticmethod
# @pytest.mark.mpl_image_compare(
# baseline_dir=TEST_DATA_PATH,
# filename="toulon_ref.png",
# )
# def test_draw_contours_Toulon_transform(
# toulon_tiles_transformed: list[HgtTile],
# ) -> plt.Figure: # type: ignore[reportPrivateImportUsage] # not supported by pylance
# """Rather an end-to-end test.
# Print contours in Toulon's area to assert overall result, even if contours are not exactly the same (eg. algo evolution).
# To compare output, run `pytest --mpl`
# """
# return TestHgtTile._test_draw_contours(toulon_tiles_transformed, None)
@staticmethod
@pytest.mark.mpl_image_compare(
baseline_dir=TEST_DATA_PATH,
# Transformed tiff result is slightly different from original HGT one
filename="toulon_ref_transformed.png",
)
def test_draw_contours_Toulon_transform(
toulon_tiles_transformed: list[HgtTile],
) -> plt.Figure: # type: ignore[reportPrivateImportUsage] # not supported by pylance
"""Rather an end-to-end test.
Print contours in Toulon's area to assert overall result, even if contours are not exactly the same (eg. algo evolution).
To compare output, run `pytest --mpl`
"""
return TestHgtTile._test_draw_contours(toulon_tiles_transformed, None)

@staticmethod
@pytest.mark.mpl_image_compare(
Expand All @@ -193,12 +196,18 @@ def _test_draw_contours(
rdp_epsilon: float | None,
) -> plt.Figure: # type: ignore[reportPrivateImportUsage] # not supported by pylance
"""Internal contour testing method."""
elevations, contour_data = tiles[0].contourLines(rdpEpsilon=rdp_epsilon)
tile = tiles[0]
elevations, contour_data = tile.contourLines(rdpEpsilon=rdp_epsilon)
dpi = 100
# Get graph space close to original data size
out_size = HGT_SIZE
fig = plt.figure(figsize=(out_size / dpi, out_size / dpi), dpi=dpi)
plt.axis("off")
plt.axis("on")
# Fix the axes limits to the bbox
tile_bbox = tile.bbox()
plt.xlim(tile_bbox.min_lon, tile_bbox.max_lon)
plt.ylim(tile_bbox.min_lat, tile_bbox.max_lat)
plt.ticklabel_format(useOffset=False)
plt.tight_layout(pad=0)
for elev in range(0, 500, 100):
for contour in contour_data.trace(elev)[0]:
Expand Down

0 comments on commit bee94cf

Please sign in to comment.