Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid a deprecation warning from numpy 1.25 in crs._safe_pj_transform #2194

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Avoid a deprecation warning from numpy 1.25 in crs._safe_pj_transform
  • Loading branch information
neutrinoceros committed Jun 21, 2023
commit 0fb0ee84157b88374f3e2a5d408ed590667a2bcd
17 changes: 16 additions & 1 deletion lib/cartopy/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@ def _safe_pj_transform(src_crs, tgt_crs, x, y, z=None, trap=True):
if z is None:
z = np.zeros_like(x)

return transformer.transform(x, y, z, errcheck=trap)
def _sanitize_scalars(a):
# future compatibility with numpy:
# pyproj implicitly converts size-1 arrays to scalars, which is
# deprecated in numpy 1.25
# see https://github.com/numpy/numpy/pull/10615
if isinstance(a, np.ndarray) and a.size == 1:
return a.item()
else:
return a

return transformer.transform(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about putting a context manager around a filterwarnings call here instead? The warning is sort of unfortunate and pyproj already has the workarounds for handling the single-item numpy arrays, so I guess I'd say we can just sweep the warning under the rug here because it is downstream and already taken care of.

warnings.filterwarnings("ignore", message="Conversion of an array with ndim > 0")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try it

_sanitize_scalars(x),
_sanitize_scalars(y),
_sanitize_scalars(z),
errcheck=trap,
)


class Globe:
Expand Down
17 changes: 17 additions & 0 deletions lib/cartopy/tests/crs/test_transform_point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright Cartopy Contributors
#
# This file is part of Cartopy and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
import warnings

import cartopy.crs as ccrs


def test_transform_point():
neutrinoceros marked this conversation as resolved.
Show resolved Hide resolved
# see https://github.com/SciTools/cartopy/pull/2194
p = ccrs.PlateCarree()
p2 = ccrs.Mercator()
with warnings.catch_warnings():
warnings.simplefilter("error")
p2.transform_point(1, 2, p)