# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

import math
import tensorflow as tf


def distance(origin, destination):
    """
    Calculate the Haversine distance.

    Parameters
    ----------
    origin : tuple of float
        (lat, long)
    destination : tuple of float
        (lat, long)

    Returns
    -------
    distance_in_km : float

    Examples
    --------
    >>> origin = (48.1372, 11.5756)  # Munich
    >>> destination = (52.5186, 13.4083)  # Berlin
    >>> round(distance(origin, destination), 1)
    504.2
    """
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371  # km

    dlat = math.radians(lat2 - lat1)
    dlon = math.radians(lon2 - lon1)
    print((dlat, dlon))
    a = (math.sin(dlat / 2) * math.sin(dlat / 2) +
         math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
         math.sin(dlon / 2) * math.sin(dlon / 2))
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    d = radius * c

    return d


origin = (48.1372, 11.5756)  # Munich
destination = (52.5186, 13.4083)  # Berlin
distance(origin, destination)

sess = tf.InteractiveSession()

lat1=tf.constant( 48.1372)
lon1=tf.constant(11.5756)
lat2=tf.constant(52.5186)
lon2=tf.constant(13.4083)
dlat = radians (lat2 - lat1) 
dlon = radians (lon2 - lon2)
sess.run([dlat, dlon])


# +
def radians(a):
    return a * math.pi / 180.0

def tf_haversine(lat1, lon1, lat2, lon2):
    
    radius = 6371.0
    dlat = radians (lat2 - lat1) 
    dlon = radians (lon2 - lon1)
    a = (tf.sin(dlat / 2.0) * tf.sin(dlat/2.0) +
         tf.cos(radians(lat1)) * tf.cos(radians(lat2)) *
         tf.sin(dlon / 2.0) * tf.sin(dlon / 2.0))
    c = 2.0 * tf.atan2(tf.sqrt(a), tf.sqrt(1.0 - a))
    return radius * c


# -

lat1=tf.constant( 48.1372)
lon1=tf.constant(11.5756)
lat2=tf.constant(52.5186)
lon2=tf.constant(13.4083)

sess.run(tf_haversine(lat1, lon1, lat2, lon2))



StarcoderdataJupyterScriptsDedupFiltered