gpt4 book ai didi

R - 对于位置数据帧,从另一个位置数据帧中找到最近的成员

转载 作者:行者123 更新时间:2023-12-04 10:46:41 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Geographic / geospatial distance between 2 lists of lat/lon points (coordinates)

(3 个回答)


去年关闭。




我需要获取 DF1(3000 个位置)中的位置(Lat/Lon)并使用 R 找到到 DF2(1500 个位置)中最近位置的 Haversine 距离。

我可以通过对 DF2 中的位置进行硬编码或为 DF2 中的每个项目创建一列来使其工作,这非常麻烦。

关于如何有效地完成这项工作的任何想法?

Data structures
DF1
Lat Lon

DF2
Lat Lon

最佳答案

尝试这个:

# load packages
install.packages("tidyverse")
library(geosphere)
install.packages("rjson")
library(rjson)

# make a function to retrieve test data


get_latlon <- function(x){
url <- paste0("https://api3.geo.admin.ch/rest/services/api/SearchServer?searchText=",paste(x),"&type=locations")
result <- rjson::fromJSON(file = URLencode(url))


as_tibble(result$results[[1]]) %>%
mutate(attr_names = names(attrs)) %>%
spread(attr_names, attrs) %>%
unnest(cols = c(detail, featureId, geom_quadindex, geom_st_box2d, label, lat,
lon, num, origin, rank, x, y, zoomlevel)) %>%
select(detail,lat,lon)
}


# retrieve test data

cities1 <- c("spiez","zumikon","winterthur","neuenburg")
cities2 <- c("zurich","bern","lausanne")

cities1 %>% map(get_latlon) %>% bind_rows() -> DF1
cities2 %>% map(get_latlon) %>% bind_rows() -> DF2


# make a combined dataframe
names(DF1) <- paste0(names(DF1), ".a")
names(DF2) <- paste0(names(DF2), ".b")
crossing(DF1,DF2) -> data

# function to calculate the Harversine distance

haversine <- function(lon1, lat1, lon2, lat2, r = 6378137) {

if(!is.numeric(c(lon1, lat1, lon2, lat2)))
stop("Inpust must be numeric")

# Convert degrees to radians
lon1 <- lon1 * pi / 180
lat1 <- lat1 * pi / 180
lon2 <- lon2 * pi / 180
lat2 <- lat2 * pi / 180

delta.lon <- (lon2 - lon1)
delta.lat <- (lat2 - lat1)
a <- sin(delta.lat/2)^2 + cos(lat1) * cos(lat2) *
sin(delta.lon/2)^2
c <- 2 * asin(min(1,sqrt(a)))
distance <- r * c

return(distance) # Distance
}


# find the smallest distances for locations in DF1
data %>%
group_by(detail.a,detail.b) %>%
mutate(haversine=haversine(lat.a,lon.a,lat.b,lon.b)) %>%
group_by(detail.a) %>%
slice(which.min(haversine))

关于R - 对于位置数据帧,从另一个位置数据帧中找到最近的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59674971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com