1

In R we have a function, distCosine, within a package Geosphere, here

I am current;y looking to find a similar function in Python, but all I can find is SciPy's implementation here. This does not include the radius.

Is there a Python based working of this R function? Else I have found some to work write out one myself

redrubia
  • 2,256
  • 6
  • 33
  • 47
  • The linked scipy function is not the same as the Geosphere one – Dave Jan 23 '14 at 21:11
  • 2
    c.f. this question and answers (http://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points) – Dave Jan 23 '14 at 21:13
  • I know it wasn't the same - its the only cosine distance I could find in Python. Thanks for the link. Similar solution to the one above, which is o create your own function. – redrubia Jan 23 '14 at 21:14

1 Answers1

2

You can see the source code for the R distCosine function... just write one in python:

from math import acos, sin, cos, pi

def distCosine(p1, p2, r=6378137):
    p1 = [p * pi / 180 for p in p1]
    p2 = [p * pi / 180 for p in p2]

    out = acos(sin(p1[1]) * sin(p2[1]) +\
               cos(p1[1]) * cos(p2[1]) *\
               cos(p1[0] - p2[0])) * r
    return out

distCosine([0, 0], [90, 90])
#  10018754.171394622

You might want to add some error checking of sorts, but this works on the example provided by the R package...

Justin
  • 42,475
  • 9
  • 93
  • 111