Skip to content

Commit

Permalink
BUG: fix Geod.npts NaN handling (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 authored May 19, 2023
1 parent 3b417c4 commit bb237b3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Latest
- ENH: Added allow_superseded kwargs to :class:`pyproj.transformer.TransformerGroup` (pull #1269)
- ENH: Added :meth:`CRS.to_2d` to demote 3D CRS to 2D (issue #1266)
- ENH: Added parameter `output_axis_rule` to :meth:`CRS.to_wkt` (pull #1287)
- BUG: fix Geod.npts NaN handling (issue #1282)

3.5.0
------
Expand Down
3 changes: 2 additions & 1 deletion pyproj/_geod.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Geod:
lon1: float,
lat1: float,
lon2_or_azi1: float,
lat2_or_nan: float,
lat2: float,
npts: int,
del_s: float,
radians: bool,
Expand All @@ -74,6 +74,7 @@ class Geod:
out_lats: Any,
out_azis: Any,
return_back_azimuth: bool,
is_fwd: bool,
) -> GeodIntermediateReturn: ...
def _line_length(self, lons: Any, lats: Any, radians: bool = False) -> float: ...
def _polygon_area_perimeter(
Expand Down
8 changes: 4 additions & 4 deletions pyproj/_geod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ cdef class Geod:
double lon1,
double lat1,
double lon2_or_azi1,
double lat2_or_nan,
double lat2,
int npts,
double del_s,
bint radians,
Expand All @@ -376,6 +376,7 @@ cdef class Geod:
object out_lats,
object out_azis,
bint return_back_azimuth,
bint is_fwd,
) -> GeodIntermediateReturn:
"""
.. versionadded:: 3.1.0
Expand All @@ -397,7 +398,6 @@ cdef class Geod:
PyBuffWriteManager lons_buff
PyBuffWriteManager lats_buff
PyBuffWriteManager azis_buff
bint is_fwd = isnan(lat2_or_nan)

if not is_fwd and (del_s == 0) == (npts == 0):
raise GeodError("inv_intermediate: "
Expand All @@ -409,7 +409,7 @@ cdef class Geod:
lat1 *= _RAD2DG
lon2_or_azi1 *= _RAD2DG
if not is_fwd:
lat2_or_nan *= _RAD2DG
lat2 *= _RAD2DG

if is_fwd:
# do fwd computation to set azimuths, distance.
Expand All @@ -418,7 +418,7 @@ cdef class Geod:
else:
# do inverse computation to set azimuths, distance.
geod_inverseline(&line, &self._geod_geodesic, lat1, lon1,
lat2_or_nan, lon2_or_azi1, 0u)
lat2, lon2_or_azi1, 0u)

if npts == 0:
# calc the number of required points by the distance increment
Expand Down
9 changes: 6 additions & 3 deletions pyproj/geod.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def npts(
lon1=lon1,
lat1=lat1,
lon2_or_azi1=lon2,
lat2_or_nan=lat2,
lat2=lat2,
npts=npts,
del_s=0,
radians=radians,
Expand All @@ -535,6 +535,7 @@ def npts(
out_lats=None,
out_azis=None,
return_back_azimuth=False,
is_fwd=False,
)
return list(zip(res.lons, res.lats))

Expand Down Expand Up @@ -688,7 +689,7 @@ def inv_intermediate(
lon1=lon1,
lat1=lat1,
lon2_or_azi1=lon2,
lat2_or_nan=lat2,
lat2=lat2,
npts=npts,
del_s=del_s,
radians=radians,
Expand All @@ -699,6 +700,7 @@ def inv_intermediate(
out_lats=out_lats,
out_azis=out_azis,
return_back_azimuth=return_back_azimuth,
is_fwd=False,
)

def fwd_intermediate(
Expand Down Expand Up @@ -835,7 +837,7 @@ def fwd_intermediate(
lon1=lon1,
lat1=lat1,
lon2_or_azi1=azi1,
lat2_or_nan=math.nan,
lat2=math.nan,
npts=npts,
del_s=del_s,
radians=radians,
Expand All @@ -846,6 +848,7 @@ def fwd_intermediate(
out_lats=out_lats,
out_azis=out_azis,
return_back_azimuth=return_back_azimuth,
is_fwd=True,
)

def line_length(self, lons: Any, lats: Any, radians: bool = False) -> float:
Expand Down
17 changes: 16 additions & 1 deletion test/test_geod.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy
import pytest
from numpy.testing import assert_almost_equal
from numpy.testing import assert_almost_equal, assert_array_equal

from pyproj import Geod
from pyproj.geod import GeodIntermediateFlag, reverse_azimuth
Expand Down Expand Up @@ -443,6 +443,21 @@ def test_geodesic_npts(include_initial, include_terminus):
assert_almost_equal(lonlats, expected_lonlats, decimal=3)


@pytest.mark.parametrize(
"input_data",
[
[1.0, 2.0, 3.0, math.nan],
[1.0, 2.0, math.nan, 4.0],
[1.0, math.nan, 3.0, 4.0],
[math.nan, 2.0, 3.0, 4.0],
[math.nan, math.nan, math.nan, math.nan],
],
)
def test_geodesic_npts__nan(input_data):
geod = Geod(ellps="WGS84")
assert_array_equal(geod.npts(*input_data, npts=1), [(math.nan, math.nan)])


@pytest.mark.parametrize(
"ellipsoid,expected_azi12,expected_az21,expected_dist",
[("clrk66", -66.531, 75.654, 4164192.708), ("WGS84", -66.530, 75.654, 4164074.239)],
Expand Down

0 comments on commit bb237b3

Please sign in to comment.