Skip to content

Commit 8410224

Browse files
committed
to_raster: detect default extent via autoscale
Cartopy can set projection limits while autoscale stays on, so the old (0,1)-only check missed default extents. Using autoscale plus a global PlateCarree extent keeps the warning firing when users never set an extent.
1 parent a689170 commit 8410224

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

uxarray/core/dataarray.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,23 @@ def to_raster(
404404
)
405405
pixel_mapping = np.asarray(pixel_mapping, dtype=INT_DTYPE)
406406
else:
407-
xlim, ylim = ax.get_xlim(), ax.get_ylim()
408-
if np.allclose(xlim, (0.0, 1.0)) and np.allclose(ylim, (0.0, 1.0)):
407+
408+
def _is_default_extent() -> bool:
409+
# Default extents can be (0, 1) or projection limits while autoscale stays on.
410+
if not ax.get_autoscale_on():
411+
return False
412+
try:
413+
import cartopy.crs as ccrs
414+
415+
extent = ax.get_extent(ccrs.PlateCarree())
416+
except Exception:
417+
xlim, ylim = ax.get_xlim(), ax.get_ylim()
418+
return np.allclose(xlim, (0.0, 1.0)) and np.allclose(
419+
ylim, (0.0, 1.0)
420+
)
421+
return np.allclose(extent, (-180.0, 180.0, -90.0, 90.0), atol=1.0)
422+
423+
if _is_default_extent():
409424
try:
410425
import cartopy.crs as ccrs
411426

0 commit comments

Comments
 (0)