Ejemplo n.º 1
0
def test_angle_to_name():
    expect(angle_to_name(0)) == 'North'
    expect(angle_to_name(360)) == 'North'
    expect(angle_to_name(45)) == 'North-east'
    expect(angle_to_name(292)) == 'West'
    expect(angle_to_name(293)) == 'North-west'
    expect(angle_to_name(0, 4)) == 'North'
    expect(angle_to_name(360, 16)) == 'North'
    expect(angle_to_name(45, 4, True)) == 'NE'
    expect(angle_to_name(292, 16, True)) == 'WNW'
Ejemplo n.º 2
0
    def bearing(self, other, format='numeric'):
        """Calculate the initial bearing from self to other.

        .. note::
           Applying common plane Euclidean trigonometry to bearing calculations
           suggests to us that the bearing between point A to point B is equal
           to the inverse of the bearing from Point B to Point A, whereas
           spherical trigonometry is much more fun.  If the ``bearing`` method
           doesn't make sense to you when calculating return bearings there are
           plenty of resources on the web that explain spherical geometry.

        .. todo:: Add Rhumb line calculation

        :param Point other: Location to calculate bearing to
        :param str format: Format of the bearing string to return
        :rtype: ``float``
        :return: Initial bearing from self to other in degrees
        :raise ValueError: Unknown value for ``format``
        """
        longitude_difference = other.rad_longitude - self.rad_longitude

        y = math.sin(longitude_difference) * math.cos(other.rad_latitude)
        x = math.cos(self.rad_latitude) * math.sin(other.rad_latitude) - \
            math.sin(self.rad_latitude) * math.cos(other.rad_latitude) * \
            math.cos(longitude_difference)
        bearing = math.degrees(math.atan2(y, x))
        # Always return positive North-aligned bearing
        bearing = (bearing + 360) % 360
        if format == 'numeric':
            return bearing
        elif format == 'string':
            return utils.angle_to_name(bearing)
        else:
            raise ValueError('Unknown format type %r' % format)
Ejemplo n.º 3
0
    def final_bearing(self, other, format='numeric'):
        """Calculate the final bearing from self to other.

        .. seealso::

           :meth:`bearing`

        :param Point other: Location to calculate final bearing to
        :param str format: Format of the bearing string to return
        :rtype: ``float``
        :return: Final bearing from self to other in degrees
        :raise ValueError: Unknown value for ``format``
        """
        final_bearing = (other.bearing(self) + 180) % 360
        if format == 'numeric':
            return final_bearing
        elif format == 'string':
            return utils.angle_to_name(final_bearing)
        else:
            raise ValueError('Unknown format type %r' % format)
Ejemplo n.º 4
0
def test_angle_to_name(args, result):
    assert angle_to_name(*args) == result