我有一个DATAFRAME:
+----------+----------+
| longitude| latitude|
+----------+----------+
|-7.1732833|32.0414966|
|-7.1732844|32.0414406|
|-7.1732833|32.0414966|
|-7.1732833|32.0414966|
|-7.1732833|32.0414966|
|-7.1732833|32.0414966|
我想计算一个点与后继点之间的距离,例如:
distance between (-7.1732833,32.0414966) et (-7.1732844,32.0414406)
我完成的代码:
def haversine_distance(longitude1 : Double,latitude1 : Double,longitude2 : Double,latitude2 : Double) : Double= {
val R = 6372.8;
val dlat = math.toRadians(latitude2 - latitude1);
val dlog = math.toRadians(longitude2 - longitude1);
val a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(math.toRadians(latitude1)) * math.cos(math.toRadians(latitude2)) * math.sin(dlog / 2) * math.sin(dlog / 2)
val c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
val distance = R * c;
return distance
}
我的问题是:如何浏览数据框,以便我可以使用数据框的经度和纬度坐标调用函数hasrsine_distance。 谢谢。
答案 0 :(得分:1)
尝试调查该问题-Spark DataFrames: Combining Two Consecutive Rows
您不能直接将udf
应用于多行,因此必须使用窗口函数来合并随后的行,从而获得DF:
+----------+----------+----------+----------+
| long1 | lat1 | long2 | lat2 |
+----------+----------+----------+----------+
|-7.1732833|32.0414966|-7.1732844|32.0414406|
|-7.1732844|32.0414406|-7.1732833|32.0414966|
|-7.1732833|32.0414966|-7.1732833|32.0414966|
然后您可以应用您所描述的udf
。