0

I'm looking for a way to calculate haversine distance between two geographical points (lat and lon) in Athena! In presto, we have something like great_circle_distance but Athena doesn't have it. Any suggestion?

3 Answers 3

3

This is now available in Athena engine version 2: https://docs.aws.amazon.com/athena/latest/ug/geospatial-functions-list-v2.html

enter image description here

1

This is c# but the algo is there, just port logic to an athena query, from my reading a ton of people end up implementing haversine themselves in athena

static double haversine(double lat1, double lon1, 
                        double lat2, double lon2) 
{ 
    // distance between latitudes and longitudes 
    double dLat = (Math.PI / 180) * (lat2 - lat1); 
    double dLon = (Math.PI / 180) * (lon2 - lon1); 

    // convert to radians 
    lat1 = (Math.PI / 180) * (lat1); 
    lat2 = (Math.PI / 180) * (lat2); 

    // apply formulae 
    double a = Math.Pow(Math.Sin(dLat / 2), 2) +  
               Math.Pow(Math.Sin(dLon / 2), 2) *  
               Math.Cos(lat1) * Math.Cos(lat2); 
    double rad = 6371; 
    double c = 2 * Math.Asin(Math.Sqrt(a)); 
    return rad * c; 
} 
0

Athena has a large number of geospatial functions, including one called ST_DISTANCE that returns the distance between two points in degrees, I'm no GIS expert, but I assume it should be possible to convert that to a distance in kilometers.

1
  • 1
    Because degrees of latitude and degrees of longitude are different units, and because the conversion of a degree of longitude to meters changes depending on one's latitude, it is not valid to measure distances in degrees. Evidently the person who implemented this function in Athena also was not a GIS expert. Jun 26, 2020 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.