Skip to content

Commit

Permalink
Fix some deprecation warnings (#175)
Browse files Browse the repository at this point in the history
* Fix use of deprecated tostring method

* Fix use of deprecated pyproj.transform

* Use explicit value for about to be changed approx arg
  • Loading branch information
TimoRoth committed Jul 13, 2020
1 parent bdf3c4d commit 865a511
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
20 changes: 17 additions & 3 deletions salem/gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,14 @@ def proj_is_same(p1, p2):
return p1 == p2


def _transform_internal(p1, p2, x, y, **kwargs):
if hasattr(pyproj, 'Transformer'):
trf = pyproj.Transformer.from_proj(p1, p2, **kwargs)
return trf.transform(x, y)
else:
return pyproj.transform(p1, p2, x, y, **kwargs)


def transform_proj(p1, p2, x, y, nocopy=False):
"""Wrapper around the pyproj.transform function.
Expand All @@ -1297,16 +1305,16 @@ def transform_proj(p1, p2, x, y, nocopy=False):

try:
# This always makes a copy, even if projections are equivalent
return pyproj.transform(p1, p2, x, y,
skip_equivalent=True, always_xy=True)
return _transform_internal(p1, p2, x, y,
skip_equivalent=True, always_xy=True)
except TypeError:
if proj_is_same(p1, p2):
if nocopy:
return x, y
else:
return copy.deepcopy(x), copy.deepcopy(y)

return pyproj.transform(p1, p2, x, y)
return _transform_internal(p1, p2, x, y)


def transform_geometry(geom, crs=wgs84, to_crs=wgs84):
Expand Down Expand Up @@ -1472,6 +1480,7 @@ def proj_to_cartopy(proj):
if k == 'proj':
if v == 'tmerc':
cl = ccrs.TransverseMercator
kw_proj['approx'] = True
if v == 'lcc':
cl = ccrs.LambertConformal
if v == 'merc':
Expand Down Expand Up @@ -1513,6 +1522,11 @@ def proj_to_cartopy(proj):
else:
kw_proj.pop('latitude_true_scale', None)

try:
return cl(globe=globe, **kw_proj)
except TypeError:
del kw_proj['approx']

return cl(globe=globe, **kw_proj)


Expand Down
7 changes: 5 additions & 2 deletions salem/sio.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# latest xarray dropped python2 support, so we can safely assume py3 here
basestring = str

# Locals
from salem import transform_proj


def read_shapefile(fpath, cached=False):
"""Reads a shapefile using geopandas.
Expand Down Expand Up @@ -372,7 +375,7 @@ def netcdf_time(ncobj, monthbegin=False):
except AttributeError:
stimes = ncobj.variables['Times'][:]
for t in stimes:
time.append(pd.to_datetime(t.tostring().decode(),
time.append(pd.to_datetime(t.tobytes().decode(),
errors='raise',
format='%Y-%m-%d_%H:%M:%S'))
elif vt is not None:
Expand Down Expand Up @@ -1029,7 +1032,7 @@ def is_rotated_proj_working():
p1 = pyproj.Proj(srs)
p2 = wgs84

return np.isclose(pyproj.transform(p1, p2, -20, -9),
return np.isclose(transform_proj(p1, p2, -20, -9),
[-22.243473889042903, -0.06328365194179102],
atol=1e-5).all()

Expand Down
10 changes: 0 additions & 10 deletions salem/tests/test_gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,11 +1020,6 @@ def test_pyproj_trafo(self):
x = np.random.randn(int(1e6)) * 60
y = np.random.randn(int(1e6)) * 60

for i in np.arange(3):
xx, yy = pyproj.transform(wgs84, wgs84, x, y)
assert_allclose(xx, x)
assert_allclose(yy, y)

for i in np.arange(3):
xx, yy = gis.transform_proj(wgs84, wgs84, x, y)
assert_allclose(xx, x)
Expand All @@ -1035,11 +1030,6 @@ def test_pyproj_trafo(self):
assert_allclose(xx, x)
assert_allclose(yy, y)

xx, yy = pyproj.transform(gis.check_crs('epsg:26915'),
gis.check_crs('epsg:26915'), x, y)
assert_allclose(xx, x, atol=1e-3)
assert_allclose(yy, y, atol=1e-3)

xx, yy = gis.transform_proj(gis.check_crs('epsg:26915'),
gis.check_crs('epsg:26915'), x, y)
assert_allclose(xx, x)
Expand Down
2 changes: 1 addition & 1 deletion salem/wrftools.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _factor(self):
time = []
stimes = vars['Times'][0:2]
for t in stimes:
time.append(to_datetime(t.tostring().decode(),
time.append(to_datetime(t.tobytes().decode(),
errors='raise',
format='%Y-%m-%d_%H:%M:%S'))
dt_minutes = time[1] - time[0]
Expand Down

0 comments on commit 865a511

Please sign in to comment.