我尝试使用spark_apply
函数来计算R中的一些经度和纬度坐标之间的一些距离。我可以以R为基数来计算它们,但我想使用spark_apply()
函数来应用相同的计算。
如何在spark_apply
函数中复制distm(latLong, distanceFrom)
计算?
数据:
library(data.table)
library(sparklyr)
library(geosphere)
library(tidyverse)
# setup
conf <- spark_config()
conf$spark.dynamicAllocation.enabled <- "true"
sc <- spark_connect(master = "local", version = "2.3.0")
# create data
df <- data_frame(
place=c("Finland", "Canada", "Tanzania", "Bolivia", "France"),
longitude=c(27.472918, -90.476303, 34.679950, -65.691146, 4.533465),
latitude=c(63.293001, 54.239631, -2.855123, -13.795272, 48.603949),
crs="+proj=longlat +datum=WGS84")
# compute distance from the "distanceFrom" data
latLong <- df %>%
dplyr::select(c(longitude, latitude))
distanceFrom <- rbind(c(34.20, -3.67), c(30.56, -2.50))
distm(latLong, distanceFrom)
######################### Apply this in Spark
mySpark <- sdf_copy_to(sc, df, "my_tbl", overwrite = TRUE)
2条答案
按热度按时间wwtsj6pe1#
由于
sparklyr::spark_apply
在一个Spark Dataframe 上工作,一种策略是通过“交叉连接”将所有数据放到单个Spark Dataframe 上,然后,可以用geodist::geodist
计算距离。PS:考虑在spark上进行地理空间工作的geospark包。
zbq4xfa02#
下面是没有geodist库的spark_apply函数。
做与上面答案相同的事情,但首先定义下面的函数。
更改函数中的代码地理位置(x$long1、x$lat1、x$long2、x$lat2)以反映列名