这里有一种方法
data.table
,以及我为其重新编写的哈弗森公式
this question
这样它就能在里面工作
数据桌子
操作
在每个点上连接到每个点,但在连接内计算每对点之间的距离,并删除阈值之外的点。这是受到@Jaap的优秀作品的启发
answer here
哈弗森公式是
## Haversine formula
dt.haversine <- function(lat_from, lon_from, lat_to, lon_to, r = 6378137){
radians <- pi/180
lat_to <- lat_to * radians
lat_from <- lat_from * radians
lon_to <- lon_to * radians
lon_from <- lon_from * radians
dLat <- (lat_to - lat_from)
dLon <- (lon_to - lon_from)
a <- (sin(dLat/2)^2) + (cos(lat_from) * cos(lat_to)) * (sin(dLon/2)^2)
return(2 * atan2(sqrt(a), sqrt(1 - a)) * r)
}
googleway
这是墨尔本城市环线有轨电车上的车站
library(googleway)
head(tram_stops)
计算
参加
library(data.table)
## set the tram stop data as a data.table
dt1 <- as.data.table(tram_stops)
## add a column that will be used to do the join on
dt1[, joinKey := 1]
## find the dinstance between each point to every other point
## by joining the data to itself
dt2 <- dt1[
dt1
, {
idx = dt.haversine(stop_lat, stop_lon, i.stop_lat, i.stop_lon) < 500 ## in metres
.(stop_id = stop_id[idx],
near_stop_id = i.stop_id)
}
, on = "joinKey"
, by = .EACHI
]
后果
dt2 <- dt2[stop_id != near_stop_id]
情节
googleway
mapKey <- "your_api_key"
myStop <- 18048
dt_stops <- dt3[stop_id == myStop ]
dt_stops <- dt_stops[
dt1
, on = c(near_stop_id = "stop_id")
, nomatch = 0
]
google_map(key = mapKey) %>%
add_circles(data = dt1[stop_id == myStop], lat = "stop_lat", lon = "stop_lon", radius = 500) %>%
add_markers(dt_stops, lat = "stop_lat", lon = "stop_lon")
笔记
join应该相当有效,但显然我在这里使用的数据只有51行;你必须让我知道这个方法对你的数据的扩展性有多好