Skip to content

Commit

Permalink
Merge pull request #1 from snowman2/return_back_azimuth
Browse files Browse the repository at this point in the history
REF: Use cython for reverse_azimuth
  • Loading branch information
idanmiara authored Dec 19, 2022
2 parents 272ea8c + 980f5bc commit b58272c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 51 deletions.
34 changes: 22 additions & 12 deletions pyproj/_geod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from libc.math cimport ceil, isnan, round

from pyproj._compat cimport cstrencode, empty_array

import math
from collections import namedtuple

from pyproj.enums import GeodIntermediateFlag
Expand Down Expand Up @@ -65,6 +66,24 @@ cdef int GEOD_INTER_FLAG_AZIS_DISCARD = GeodIntermediateFlag.AZIS_DISCARD
cdef int GEOD_INTER_FLAG_AZIS_KEEP = GeodIntermediateFlag.AZIS_KEEP


cdef double _reverse_azimuth(double azi, double factor) nogil:
if azi > 0:
azi = azi - factor
else:
azi = azi + factor
return azi

def reverse_azimuth(object azi, bint radians=False):
cdef PyBuffWriteManager azibuff = PyBuffWriteManager(azi)
cdef Py_ssize_t iii
cdef double factor = 180
if radians:
factor = math.pi
with nogil:
for iii in range(azibuff.len):
azibuff.data[iii] = _reverse_azimuth(azibuff.data[iii], factor=factor)


cdef class Geod:
def __init__(self, double a, double f, bint sphere, double b, double es):
geod_init(&self._geod_geodesic, <double> a, <double> f)
Expand Down Expand Up @@ -137,10 +156,7 @@ cdef class Geod:
# forward azimuth needs to be flipped 180 degrees
# to match the (back azimuth) output of PROJ geod utilities.
if return_back_azimuth:
if pazi2 > 0:
pazi2 = pazi2 - 180.
else:
pazi2 = pazi2 + 180.
pazi2 = _reverse_azimuth(pazi2, factor=180)
if not radians:
lonbuff.data[iii] = plon2
latbuff.data[iii] = plat2
Expand Down Expand Up @@ -201,10 +217,7 @@ cdef class Geod:
# forward azimuth needs to be flipped 180 degrees
# to match the (back azimuth) output of PROJ geod utilities.
if return_back_azimuth:
if pazi2 > 0:
pazi2 = pazi2 - 180.
else:
pazi2 = pazi2 + 180.
pazi2 = _reverse_azimuth(pazi2, factor=180)
if radians:
lon1buff.data[iii] = _DG2RAD * pazi1
lat1buff.data[iii] = _DG2RAD * pazi2
Expand Down Expand Up @@ -327,10 +340,7 @@ cdef class Geod:
# forward azimuth needs to be flipped 180 degrees
# to match the (back azimuth) output of PROJ geod utilities.
if return_back_azimuth:
if pazi2 > 0:
pazi2 = pazi2 - 180.
else:
pazi2 = pazi2 + 180.
pazi2 =_reverse_azimuth(pazi2, factor=180)
azis_buff.data[iii] = pazi2

return GeodIntermediateReturn(
Expand Down
70 changes: 31 additions & 39 deletions pyproj/geod.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from pyproj._geod import Geod as _Geod
from pyproj._geod import GeodIntermediateReturn, geodesic_version_str
from pyproj._geod import reverse_azimuth as _reverse_azimuth
from pyproj.enums import GeodIntermediateFlag
from pyproj.exceptions import GeodError
from pyproj.list import get_ellps_map
Expand Down Expand Up @@ -1117,47 +1118,38 @@ def __eq__(self, other: Any) -> bool:
return self.__repr__() == other.__repr__()


try:
import numpy as np # type: ignore # pylint: disable=import-outside-toplevel

def reverse_azimuth(azi: Any, radians: bool = False) -> Any:
"""
Reverses the given azimuth (forward <-> backwards)
Parameters
----------
azi: float or array, :class:`numpy.ndarray`
azimuth
radians: bool, default=False
If True, the input data is assumed to be in radians.
Otherwise, the data is assumed to be in degrees.
def reverse_azimuth(azi: Any, radians: bool = False) -> Any:
"""
Reverses the given azimuth (forward <-> backwards)
Returns
-------
float or array, :class:`numpy.ndarray`
the reversed azimuth (forward <-> backwards)
"""
factor = math.pi if radians else 180.0
return azi - np.copysign(factor, azi)
.. versionadded:: 3.5.0
except ImportError:
Accepted numeric scalar or array:
def reverse_azimuth(azi: Any, radians: bool = False) -> Any:
"""
Reverses the given azimuth (forward <-> backwards)
- :class:`int`
- :class:`float`
- :class:`numpy.floating`
- :class:`numpy.integer`
- :class:`list`
- :class:`tuple`
- :class:`array.array`
- :class:`numpy.ndarray`
- :class:`xarray.DataArray`
- :class:`pandas.Series`
Parameters
----------
azi: float
azimuth
radians: bool, default=False
If True, the input data is assumed to be in radians.
Otherwise, the data is assumed to be in degrees.
Parameters
----------
azi: scalar or array
The azimuth.
radians: bool, default=False
If True, the input data is assumed to be in radians.
Otherwise, the data is assumed to be in degrees.
Returns
-------
float
the reversed azimuth (forward <-> backwards)
"""
factor = math.pi if radians else 180.0
return azi - math.copysign(factor, azi)
Returns
-------
scalar or array:
The reversed azimuth (forward <-> backwards)
"""
inazi, azi_data_type = _copytobuffer(azi)
_reverse_azimuth(inazi, radians=radians)
return _convertback(azi_data_type, inazi)

0 comments on commit b58272c

Please sign in to comment.