Skip to content

Commit

Permalink
Add more area plotting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Aug 27, 2024
1 parent 2e7fb76 commit 979e6b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
8 changes: 6 additions & 2 deletions pyresample/_formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _icon(icon_name):

def plot_area_def(area: Union['geom.AreaDefinition', 'geom.SwathDefinition'], # noqa F821
fmt: Optional[Literal["svg", "png", None]] = None,
features: Iterable[str] = ("ocean", "land", "coastline", "borders"),
features: Optional[Iterable[str]] = None,
) -> Union[str, None]:
"""Plot area.
Expand All @@ -80,7 +80,8 @@ def plot_area_def(area: Union['geom.AreaDefinition', 'geom.SwathDefinition'], #
features: Series of string names of cartopy features to add to the plot.
Can be lowercase or uppercase names of the features, for example,
"land", "coastline", "borders", or any other feature available from
``cartopy.feature``.
``cartopy.feature``. If None (default), then land, coastline, borders,
and ocean are used.
Returns:
svg or png image as string.
Expand Down Expand Up @@ -108,6 +109,9 @@ def plot_area_def(area: Union['geom.AreaDefinition', 'geom.SwathDefinition'], #
else:
raise NotImplementedError("Only AreaDefinition and SwathDefinition objects can be plotted")

if features is None:
features = ("ocean", "land", "coastline", "borders")

for feat_name in features:
feat_obj = getattr(cartopy.feature, feat_name.upper())
ax.add_feature(feat_obj)
Expand Down
4 changes: 2 additions & 2 deletions pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2121,8 +2121,8 @@ def update_hash(self, existing_hash: Optional[_Hash] = None) -> _Hash:
if existing_hash is None:
existing_hash = hashlib.sha1() # nosec: B324
existing_hash.update(self.crs_wkt.encode('utf-8'))
existing_hash.update(np.array(self.shape)) # type: ignore[arg-type]
existing_hash.update(np.array(self.area_extent)) # type: ignore[arg-type]
existing_hash.update(np.array(self.shape))
existing_hash.update(np.array(self.area_extent))
return existing_hash

@daskify_2in_2out
Expand Down
38 changes: 22 additions & 16 deletions pyresample/test/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import unittest.mock as mock
from unittest.mock import ANY

import pytest

import pyresample
from pyresample._formatting_html import (
area_repr,
Expand All @@ -29,25 +31,21 @@
from .test_geometry.test_swath import _gen_swath_def_numpy, _gen_swath_def_xarray_dask


def test_plot_area_def_w_area_def(area_def_stere_source): # noqa F811
"""Test AreaDefinition plotting as svg/png."""
area = area_def_stere_source

with mock.patch('matplotlib.pyplot.savefig') as mock_savefig:
plot_area_def(area, fmt="svg")
mock_savefig.asser_called_with(ANY, format="svg", bbox_inches="tight")
mock_savefig.reset_mock()
plot_area_def(area, fmt="png")
mock_savefig.assert_called_with(ANY, format="png", bbox_inches="tight")


def test_plot_area_def_w_area_def_show(area_def_stere_source): # noqa F811
@pytest.mark.parametrize("format", ["svg", "png", None])
@pytest.mark.parametrize("features", [None, ("coastline",)])
def test_plot_area_def_w_area_def(area_def_stere_source, format, features): # noqa F811
"""Test AreaDefinition plotting as svg/png."""
area = area_def_stere_source

with mock.patch('matplotlib.pyplot.show') as mock_show_plot:
plot_area_def(area)
mock_show_plot.assert_called_once()
with mock.patch('matplotlib.pyplot.savefig') as mock_savefig, \
mock.patch('matplotlib.pyplot.show') as mock_show_plot:
plot_area_def(area, fmt=format)
if format is None:
mock_show_plot.assert_called_once()
mock_savefig.assert_not_called()
else:
mock_show_plot.assert_not_called()
mock_savefig.asser_called_with(ANY, format=format, bbox_inches="tight")


def test_plot_area_def_w_swath_def(create_test_swath):
Expand All @@ -74,6 +72,14 @@ def test_area_def_cartopy_installed(area_def_stere_source): # noqa F811
assert "Note: If cartopy is installed a display of the area can be seen here" not in area._repr_html_()


def test_area_repr_custom_map(area_def_stere_source): # noqa F811
"""Test custom map section of area repr."""
area = area_def_stere_source
res = area_repr(area, include_header=False, include_static_files=False,
map_content="TEST")
assert "TEST" in res


def test_area_repr_w_static_files(area_def_stere_source): # noqa F811
"""Test area representation with static files (css/icons) included."""
area_def = area_def_stere_source
Expand Down

0 comments on commit 979e6b2

Please sign in to comment.