gpt4 book ai didi

r - 如何使用 R 找到到单对纬度/经度坐标的最近道路?

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

我有一个包含不同位置的数百个纬度-经度坐标对的列表。我的目标是使用 R 估算从“家”位置到每个坐标对的行驶持续时间。

我在 R 中使用 googleway 包取得了一些成功,但(可以预见)在远离道路的位置遇到问题,例如如果坐标是山顶。在这些情况下,我想估计到每个有问题的坐标对的最近道路的行驶时间。

为了说明,假设我家的位置是;

home <- "Edinburgh, UK"

...以及我想找到可能到达的行车时间的位置示例数据框;

location <- c("place_a", "place_b", "place_c") 
latitude <- c("56.87034", "57.69380", "57.36243")
longitude <- c("-4.199001", "-5.128715", "-5.104728")

df <- data.frame(location, latitude, longitude)

我可以获得homeplace_a之间,homeplace_b之间的距离/时长等> 使用类似的东西;

(注意。您需要自己的 Google Maps api key 才能复制此部分...)

library(googleway)
api_key <- [insert your Google Maps api key here!]

results <- google_distance(origins = home,
destinations = list(c("56.87034,-4.199001"),
("57.69380,-5.128715")),
mode = "driving",
key = api_key,
units = "imperial")

我得到了我想要使用的所有数据:

results$rows[[1]]

然而,当我们为 place_c 尝试相同的坐标时遇到了麻烦,它返回 ZERO_RESULTS

results2 <- google_distance(origins = home,
destinations = ("57.36243,-5.104728"),
mode = "driving",
key = api_key,
units = "imperial")

这里我认为问题是坐标是半山腰,所以在这种情况下我想找到离坐标最近的路。我希望在 googlewaynearest_road 功能上有一些好运,但似乎无法让它工作,例如这样的事情不起作用;

df_points <- read.table(text = "lat lon
57.36243 -5.104728", header = T)

nearest_road <- google_nearestRoads(df_points, key = api_key)

谁能告诉我这里的问题是什么?或者完全提出更好的解决方案?!

非常感谢。

最佳答案

我正在研究一个即将在 github 上可用的包(称为 spaceheater)中的这个问题。与此同时:

我会从 geofabrik 下载与您合作的国家/地区的 Open Street Maps shapefile。例如尼日利亚:http://download.geofabrik.de/africa/nigeria.html

根据 monkeytennis 的建议进行编辑(谢谢!):

library(sp)
library(rgdal)
library(raster)
library(googleway)
library(geosphere)
library(foreach)
###I did Nigeria because I have it in my file downloaded, you would use UK###
roadshp <- readOGR(dsn="nigeria-latest-free.shp",
layer="gis.osm_roads_free_1")
#Isolate primary roads (or secondary and tertiary) if you wish#
roads <- roadshp[roadshp$fclass %in% c("primary", "secondary", "tertiary"),]
#Use SpatialPoints for your gps coords
location <- c("place_a", "place_b", "place_c")
latitude <- c(8.641, 10.892, 11.797)
longitude <- c(6.0046, 11.146, 5.477)
df <- data.frame(location, latitude, longitude)
coordinates(df)=~longitude+latitude
sp1 <- SpatialPoints(df)
proj4string(sp1)=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
+towgs84=0,0,0")
clodist <- dist2Line(sp1, roads)
df <- as.data.frame(df)
df$clodist <- clodist[,c("distance")]
df$lat <- clodist[,c("lat")]
df$lon <- clodist[,c("lon")]
iters <- nrow(df)
origin <- as.character("9.056, 7.497")
gc1 <- data.frame(round(df[,c("lat")],3), round(df[,c("lon")],3))
colnames(gc1) <- c("lat","lon")
df$lat <- as.character(gc1$lat)
df$lon <- as.character(gc1$lon)
gt2 <- paste(df[,c("lat")], df[,c("lon")], sep=",")
results <- google_distance(origins =origin, destinations= gt2,
mode="driving",
key="Your API Key Here")
results <-unlist(results)
results <- as.data.frame(results)
ttt <- head(results,-1)
ttt <- ttt[-c(iters+1), ]
m1 <- matrix(ttt, ncol=iters, byrow=TRUE)
distance <- as.data.frame(m1)
rownames(distance) <- c("Address", "DistanceKM",
"DistanceM","TimeTextLow","TimeSecondsLow","TimeTextHigh","TimeSecondsHigh",
"Status")

关于r - 如何使用 R 找到到单对纬度/经度坐标的最近道路?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48175907/

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