Source code for Exercises.essentials.functions.great_circle.great_circle

"""

The shortest distance between two points on the globe, assuming it is perfectly spherical, is the length of the  *great circle* path.  If you are given two locations in latitude and longitude, then the `Haversine Formula <http://en.wikipedia.org/wiki/Haversine_formula>`_ gives this distance in a
numerically stable way. Here are the steps to computing :math:`d`, the
distance:

.. math::

  a &= \\sin^{2}\\frac{(\\phi_{1} - \\phi_{2})}{2} + \\cos(\\phi_{1})\\cos(\\phi_{2})\\sin^{2}\\frac{(\\lambda_{1} - \\lambda_{2})}{2}

  c &=  2\\; \\arcsin(\sqrt{a})

  d & = rc

Where :math:`\phi_{1},\;\lambda_{1}` is the latitude, longitude of
point 1 and :math:`\phi_{2},\;\lambda_{2}` is the latitude,longitude of point 2, and :math:`r` is the radius of the globe.

Question 1
----------

Write a function called `haversine_formula` that takes as inputs a `radius` and two points specified by tuples of `(latitude, longitude)` and returns the distance between the points along a great circle.  These are the math functions
needed to implement above formula::

   from math import sin, cos, asin, radians, sqrt    

The functions `asin` is :math:`\\arcsin` and the function
`radians` is used  to convert degrees to radians.  You will
need this since lat/longs are degrees, and the `math` functions
:math:`\\sin` and :math:`\\cos` will only give the expected
answers if their arguments are in radians.

Test your function using these values::

    >>> r_earth = 6371.0 # km
    >>> austin = (30.2500, -97.7500)
    >>> cambridge = (52.2050, 0.1190)

Your answer should be roughly 7,895 km::

    >>> print haversine_formula(r_earth, austin, cambridge)
    7894.56962773

Question 2
----------

Here is the list of cities from the "Flight Distances" exercise, along their latitude and longitudes::


    cities = {
        'Atlanta': (33.7569444444, -84.3902777778),
        'Austin': (30.3, -97.7333333333),
        'Boston': (42.3577777778, -71.0616666667),
        'Chicago': (41.9, -87.65),
        'Dallas': (32.7825, -96.7975),
        'Denver': (39.7391666667, -104.984722222),
        'Houston': (29.7627777778, -95.3830555556),
        'Los Angeles': (34.05, -118.25),
        'Miami': (25.7833333333, -80.2166666667),
        'New York': (40.67, -73.94),
        'San Francisco': (37.7666666667, -122.433333333),
        'Seattle': (47.6, -122.316666667),
    }


write a function named `city_distance` that, given a dictionary of city names and locations, plus the names of two cities, returns the great circle distance between the two cities.  The result should be rounded to the nearest 10 km.

.. hint::

   The built-in `round()` function takes an optional second argument for the number of digits of precision.  This argument can be negative.

Your function should work like this::

    >>> dist = city_distance(cities, "Austin", "San Francisco")
    >>> print dist


Question 3
----------

Write a function that, given a set of cities returns a dictionary whose keys are pairs of cities and whose values are the distances between them.  You should use an appropriate data structure for the keys, and round distances to the nearest 10 km.

    def compute_distances(cities):
        ...

Bonus points if you compute the distance for a given pair of cities only once.

.. hint::

   Have a look at the `flight_distances` exercise in the Frozenset lecture for a possible data structure.

"""
from math import sin, cos, asin, radians, pi

[docs]def haversine_formula(radius, latlong1, latlong2): """ You will need to convert your latlongs to radians to use the haversine formula. Call the `deg2radian` function defined below to do that. """ pass # your code goes here
def city_distance(cities, city_1, city_2): pass # Your code goes here def compute_distances(cities): pass # Your code goes here def deg2radian (deg): return (pi * deg)/180 if __name__ == '__main__': cities = { 'Atlanta': (33.7569444444, -84.3902777778), 'Austin': (30.3, -97.7333333333), 'Boston': (42.3577777778, -71.0616666667), 'Chicago': (41.9, -87.65), 'Dallas': (32.7825, -96.7975), 'Denver': (39.7391666667, -104.984722222), 'Houston': (29.7627777778, -95.3830555556), 'Los Angeles': (34.05, -118.25), 'Miami': (25.7833333333, -80.2166666667), 'New York': (40.67, -73.94), 'San Francisco': (37.7666666667, -122.433333333), 'Seattle': (47.6, -122.316666667), } #Copyright 2008-2016, Enthought, Inc. #Use only permitted under license. Copying, sharing, redistributing or other unauthorized use strictly prohibited. #http://www.enthought.com