Created
December 19, 2016 07:52
-
-
Save pavlov99/bd265be244f8a84e291e96c5656ceb5c to your computer and use it in GitHub Desktop.
Spherical distance calcualtion based on latitude and longitude with Apache Spark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on following links: | |
// http://andrew.hedges.name/experiments/haversine/ | |
// http://www.movable-type.co.uk/scripts/latlong.html | |
df | |
.withColumn("a", pow(sin(toRadians($"destination_latitude" - $"origin_latitude") / 2), 2) + cos(toRadians($"origin_latitude")) * cos(toRadians($"destination_latitude")) * pow(sin(toRadians($"destination_longitude" - $"origin_longitude") / 2), 2)) | |
.withColumn("distance", atan2(sqrt($"a"), sqrt(-$"a" + 1)) * 2 * 6371) | |
>>> | |
+--------------+-------------------+-------------+----------------+---------------+----------------+--------------------+---------------------+--------------------+------------------+ | |
|origin_airport|destination_airport| origin_city|destination_city|origin_latitude|origin_longitude|destination_latitude|destination_longitude| a| distance| | |
+--------------+-------------------+-------------+----------------+---------------+----------------+--------------------+---------------------+--------------------+------------------+ | |
| HKG| SYD| Hong Kong| Sydney| 22.308919| 113.914603| -33.946111| 151.177222| 0.3005838068886348|7393.8837884771565| | |
| YYZ| HKG| Toronto| Hong Kong| 43.677223| -79.630556| 22.308919| 113.914603| 0.6941733892671567|12548.533187172497| | |
+--------------+-------------------+-------------+----------------+---------------+----------------+--------------------+---------------------+--------------------+------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love this thread of people sharing different implementations for different needs ❤️
Also saved me some time, so thanks all!
Here's my contribution: