Skip to content

Commit

Permalink
add return_back_azimuth: bool = True to fwd()
Browse files Browse the repository at this point in the history
  • Loading branch information
idanmiara committed Oct 25, 2022
1 parent 12b7f19 commit 479811d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
8 changes: 7 additions & 1 deletion pyproj/_geod.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ class Geod:
def __reduce__(self) -> Tuple[Type["Geod"], str]: ...
def __repr__(self) -> str: ...
def _fwd(
self, lons: Any, lats: Any, az: Any, dist: Any, radians: bool = False
self,
lons: Any,
lats: Any,
az: Any,
dist: Any,
radians: bool = False,
return_back_azimuth: bool = True,
) -> None: ...
def _inv(
self, lons1: Any, lats1: Any, lons2: Any, lats2: Any, radians: bool = False
Expand Down
11 changes: 7 additions & 4 deletions pyproj/_geod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ cdef class Geod:
object az,
object dist,
bint radians=False,
bint return_back_azimuth=True,
):
"""
forward transformation - determine longitude, latitude and back azimuth
of a terminus point given an initial point longitude and latitude, plus
forward azimuth and distance.
if radians=True, lons/lats are radians instead of degrees.
if return_back_azimuth=False, the return azimuth will be the forward azimuth instead of the forward azimuth.
"""
cdef PyBuffWriteManager lonbuff = PyBuffWriteManager(lons)
cdef PyBuffWriteManager latbuff = PyBuffWriteManager(lats)
Expand Down Expand Up @@ -133,10 +135,11 @@ cdef class Geod:
)
# back azimuth needs to be flipped 180 degrees
# to match what PROJ geod utility produces.
if pazi2 > 0:
pazi2 = pazi2 - 180.
elif pazi2 <= 0:
pazi2 = pazi2 + 180.
if return_back_azimuth:
if pazi2 > 0:
pazi2 = pazi2 - 180.
elif pazi2 <= 0:
pazi2 = pazi2 + 180.
if not radians:
lonbuff.data[iii] = plon2
latbuff.data[iii] = plat2
Expand Down
19 changes: 16 additions & 3 deletions pyproj/geod.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,17 @@ def __init__(self, initstring: Optional[str] = None, **kwargs) -> None:
)

def fwd( # pylint: disable=invalid-name
self, lons: Any, lats: Any, az: Any, dist: Any, radians: bool = False
self,
lons: Any,
lats: Any,
az: Any,
dist: Any,
radians: bool = False,
return_back_azimuth: bool = True,
) -> Tuple[Any, Any, Any]:
"""
.. versionadded:: 3.5.0 return_back_azimuth
Forward transformation
Determine longitudes, latitudes and back azimuths of terminus
Expand Down Expand Up @@ -276,6 +284,9 @@ def fwd( # pylint: disable=invalid-name
radians: bool, default=False
If True, the input data is assumed to be in radians.
Otherwise, the data is assumed to be in degrees.
return_back_azimuth: bool, default=True
if True, the third return value will be the back azimuth,
Otherwise, it will be the forward azimuth.
Returns
-------
Expand All @@ -284,14 +295,16 @@ def fwd( # pylint: disable=invalid-name
scalar or array:
Latitude(s) of terminus point(s)
scalar or array:
Back azimuth(s)
Back azimuth(s) or Forward azimuth(s)
"""
# process inputs, making copies that support buffer API.
inx, x_data_type = _copytobuffer(lons)
iny, y_data_type = _copytobuffer(lats)
inz, z_data_type = _copytobuffer(az)
ind = _copytobuffer(dist)[0]
self._fwd(inx, iny, inz, ind, radians=radians)
self._fwd(
inx, iny, inz, ind, radians=radians, return_back_azimuth=return_back_azimuth
)
# if inputs were lists, tuples or floats, convert back.
outx = _convertback(x_data_type, inx)
outy = _convertback(y_data_type, iny)
Expand Down
25 changes: 17 additions & 8 deletions test/test_proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pyproj
from pyproj import Geod, Proj, pj_ellps, pj_list, transform
from pyproj.exceptions import CRSError, ProjError
from pyproj.utils import reverse_azimuth
from test.conftest import proj_network_env


Expand Down Expand Up @@ -300,14 +301,22 @@ def test_fwd_radians(self):
)

# Calculate Portland's lon/lat from bearing and distance in radians
endlon_r, endlat_r, backaz_r = self.g.fwd(
self.boston_r[0], self.boston_r[1], math.radians(az12_d), dist, radians=True
)

# Check they are equal
self.assertAlmostEqual(endlon_d, math.degrees(endlon_r))
self.assertAlmostEqual(endlat_d, math.degrees(endlat_r))
self.assertAlmostEqual(backaz_d, math.degrees(backaz_r))
for return_back_azimuth in [False, True]:
endlon_r, endlat_r, backaz_r = self.g.fwd(
self.boston_r[0],
self.boston_r[1],
math.radians(az12_d),
dist,
radians=True,
return_back_azimuth=return_back_azimuth,
)
if not return_back_azimuth:
backaz_r = reverse_azimuth(backaz_r)

# Check they are equal
self.assertAlmostEqual(endlon_d, math.degrees(endlon_r))
self.assertAlmostEqual(endlat_d, math.degrees(endlat_r))
self.assertAlmostEqual(backaz_d, math.degrees(backaz_r))

# Check to make sure we're back in Portland
self.assertAlmostEqual(endlon_d, self.portland_d[0])
Expand Down

0 comments on commit 479811d

Please sign in to comment.