Skip to content

Commit a09e0fb

Browse files
Add warning when constructing a PolyCollection/LineCollection with a projection (#1351)
* add warning * add warning for projection * add test case * update phrasing * more detailed warning * update test warning --------- Co-authored-by: Orhan Eroglu <[email protected]>
1 parent 7c9fa21 commit a09e0fb

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

test/test_dataset.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
from uxarray import UxDataset
77
import pytest
88

9-
try:
10-
import constants
11-
except ImportError:
12-
from . import constants
9+
import numpy as np
10+
from . import constants
1311

1412
current_path = Path(os.path.dirname(os.path.realpath(__file__)))
1513

@@ -19,6 +17,16 @@
1917
dsfile_v1_geoflow = current_path / "meshfiles" / "ugrid" / "geoflow-small" / "v1.nc"
2018
mpas_ds_path = current_path / 'meshfiles' / "mpas" / "QU" / 'mesh.QU.1920km.151026.nc'
2119

20+
21+
22+
23+
@pytest.fixture()
24+
def healpix_sample_ds():
25+
uxgrid = ux.Grid.from_healpix(zoom=1)
26+
fc_var = ux.UxDataArray(data=np.ones((3, uxgrid.n_face)), dims=['time', 'n_face'], uxgrid=uxgrid)
27+
nc_var = ux.UxDataArray(data=np.ones((3, uxgrid.n_node)), dims=['time', 'n_node'], uxgrid=uxgrid)
28+
return ux.UxDataset({"fc": fc_var, "nc": nc_var}, uxgrid=uxgrid)
29+
2230
def test_uxgrid_setget():
2331
"""Load a dataset with its grid topology file using uxarray's
2432
open_dataset call and check its grid object."""
@@ -59,15 +67,3 @@ def test_get_dual():
5967

6068
assert isinstance(dual, UxDataset)
6169
assert len(uxds.data_vars) == len(dual.data_vars)
62-
63-
# Uncomment the following test if you want to include it, ensuring you handle potential failures.
64-
# def test_read_from_https():
65-
# """Tests reading a dataset from a HTTPS link."""
66-
# import requests
67-
#
68-
# small_file_480km = requests.get(
69-
# "https://web.lcrc.anl.gov/public/e3sm/inputdata/share/meshes/mpas/ocean/oQU480.230422.nc"
70-
# ).content
71-
#
72-
# ds_small_480km = ux.open_dataset(small_file_480km, small_file_480km)
73-
# assert isinstance(ds_small_480km, ux.core.dataset.UxDataset)

test/test_plot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,12 @@ def test_to_raster():
110110
raster = uxds['bottomDepth'].to_raster(ax=ax)
111111

112112
assert isinstance(raster, np.ndarray)
113+
114+
115+
def test_collections_projection_kwarg():
116+
import cartopy.crs as ccrs
117+
uxgrid = ux.open_grid(gridfile_ne30)
118+
119+
with pytest.warns(FutureWarning):
120+
pc = uxgrid.to_polycollection(projection=ccrs.PlateCarree())
121+
lc = uxgrid.to_linecollection(projection=ccrs.PlateCarree())

uxarray/grid/grid.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@ def to_polycollection(
22082208
**kwargs,
22092209
):
22102210
"""Constructs a ``matplotlib.collections.PolyCollection``` consisting
2211-
of polygons representing the faces of the current ``Grid``
2211+
of polygons representing the faces of the unstructured grid.
22122212
22132213
Parameters
22142214
----------
@@ -2217,6 +2217,21 @@ def to_polycollection(
22172217
"""
22182218
import cartopy.crs as ccrs
22192219

2220+
if "projection" in kwargs:
2221+
proj = kwargs.pop("projection")
2222+
warn(
2223+
(
2224+
"'projection' is not a supported argument and will be ignored. "
2225+
"Define the desired projection on the GeoAxes that this collection will be added to. "
2226+
"Example:\n"
2227+
" fig, ax = plt.subplots(subplot_kw={'projection': ccrs.Robinson()})\n"
2228+
" ax.add_collection(poly_collection)\n"
2229+
f"(received projection={proj!r})"
2230+
),
2231+
category=FutureWarning,
2232+
stacklevel=2,
2233+
)
2234+
22202235
if self._cached_poly_collection:
22212236
return copy.deepcopy(self._cached_poly_collection)
22222237

@@ -2237,15 +2252,29 @@ def to_linecollection(
22372252
**kwargs,
22382253
):
22392254
"""Constructs a ``matplotlib.collections.LineCollection``` consisting
2240-
of lines representing the edges of the current ``Grid``
2255+
of lines representing the edges of the unstructured grid.
22412256
22422257
Parameters
22432258
----------
22442259
**kwargs: dict
2245-
Key word arguments to pass into the construction of a PolyCollection
2260+
Key word arguments to pass into the construction of a LineCollection
22462261
"""
22472262
import cartopy.crs as ccrs
22482263

2264+
if "projection" in kwargs:
2265+
proj = kwargs.pop("projection")
2266+
warn(
2267+
(
2268+
"'projection' is not a supported argument and will be ignored. "
2269+
"Define the desired projection on the GeoAxes that this collection will be added to. "
2270+
"Example:\n"
2271+
" fig, ax = plt.subplots(subplot_kw={'projection': ccrs.Robinson()})\n"
2272+
" ax.add_collection(line_collection)\n"
2273+
f"(received projection={proj!r})"
2274+
),
2275+
category=FutureWarning,
2276+
stacklevel=2,
2277+
)
22492278
if self._cached_line_collection:
22502279
return copy.deepcopy(self._cached_line_collection)
22512280

0 commit comments

Comments
 (0)