R: 如何使用 GPS 识别道路类型?
R: How to identify a road type using GPS?
我有几个点的GPS坐标,我想知道它们是在高速公路上,还是主干道上,还是小路上,如果我能识别出路名就更好了。我正在使用 R leaflet 绘制地图,我可以通过 OpenStreetMap 看到不同类型的道路颜色不同,我想知道如何提取这些信息。如果可以解决我的问题,使用 Google 地图不是问题。
如有任何帮助,我将不胜感激。
您可以使用 revgeocode()
来自 ggmap
:
library(ggmap)
gc <- c(-73.596706, 45.485501)
revgeocode(gc)
给出:
#[1] "4333 Rue Sherbrooke O, Westmount, QC H3Z 1E2, Canada"
注意:正如评论中提到的,此方法使用 Google 地图 API,而不是 OpenStreetMap。您每天有 2500 个查询的限制。您可以随时使用 geocodeQueryCheck()
检查您还剩多少查询
来自包文档:
reverse geocodes a longitude/latitude location using Google Maps. Note
that in most cases by using this function you are agreeing to the
Google Maps API Terms of Service at
https://developers.google.com/maps/terms.
更新
如果您需要更详细的信息,请使用 output = "all"
并提取您需要的组件:
lst <- list(
g1 = c(-73.681069, 41.433155),
g2 = c(-73.643196, 41.416240),
g3 = c(-73.653324, 41.464168)
)
res <- lapply(lst, function(x) revgeocode(x, output = "all")[[1]][[1]][[1]][[2]])
给出:
#$g1
#$g1$long_name
#[1] "Highway 52"
#
#$g1$short_name
#[1] "NY-52"
#
#$g1$types
#[1] "route"
#
#
#$g2
#$g2$long_name
#[1] "Carmel Avenue"
#
#$g2$short_name
#[1] "US-6"
#
#$g2$types
#[1] "route"
#
#
#$g3
#$g3$long_name
#[1] "Wakefield Road"
#
#$g3$short_name
#[1] "Wakefield Rd"
#
#$g3$types
#[1] "route"
使用 Google 的 API 无法识别道路类型(但是 - 他们将来可能会引入该功能)。
但是您可以使用他们的 Roads API 来获取给定坐标集的道路详细信息。
我已经编写了通过函数 google_snapToRoads()
和 google_nearestRoads()
访问道路 API 的 googleway 程序包,如果您有高级帐户,则可以使用 google_speedLimits()
在对 Google 的 API 的所有调用中,您需要在您使用的每个 API 上启用 Google API key。
library(googleway)
df_points <- data.frame(lat = c(60.1707, 60.172, 60.192),
lon = c(24.9426, 24.86, 24.89))
## plot the points on a map
google_map(key = map_key) %>%
add_markers(df_points)
nearRoads <- google_nearestRoads(df_points, key = api_key)
nearRoads
# $snappedPoints
# location.latitude location.longitude originalIndex placeId
# 1 60.17070 24.94272 0 ChIJNX9BrM0LkkYRIM-cQg265e8
# 2 60.17229 24.86028 1 ChIJpf7azXMKkkYRsk5L-U5W4ZQ
# 3 60.17229 24.86028 1 ChIJpf7azXMKkkYRs05L-U5W4ZQ
# 4 60.19165 24.88997 2 ChIJN1s1vhwKkkYRKGm4l5KmISI
# 5 60.19165 24.88997 2 ChIJN1s1vhwKkkYRKWm4l5KmISI
在这些结果中,originalIndex
值告诉您该值指的是哪个原始 df_points
(其中 0 == df_points
的第一行,1 ==第二行df_points
)
placeId
值是 Google 的唯一键,用于标识其数据库中的每个位置。因此,您可以使用 Google 的地点 API 获取有关这些地点的信息
roadDetails <- lapply(nearRoads$snappedPoints$placeId, function(x){
google_place_details(place_id = x, key = api_key)
})
## road address
lapply(roadDetails, function(x){
x[['result']][['formatted_address']]
})
# [[1]]
# [1] "Rautatientori, 00100 Helsinki, Finland"
#
# [[2]]
# [1] "Svedjeplogsstigen 7-9, 00340 Helsingfors, Finland"
#
# [[3]]
# [1] "Svedjeplogsstigen 18-10, 00340 Helsingfors, Finland"
#
# [[4]]
# [1] "Meilahdentie, 00250 Helsinki, Finland"
#
# [[5]]
# [1] "Meilahdentie, 00250 Helsinki, Finland"
我有几个点的GPS坐标,我想知道它们是在高速公路上,还是主干道上,还是小路上,如果我能识别出路名就更好了。我正在使用 R leaflet 绘制地图,我可以通过 OpenStreetMap 看到不同类型的道路颜色不同,我想知道如何提取这些信息。如果可以解决我的问题,使用 Google 地图不是问题。
如有任何帮助,我将不胜感激。
您可以使用 revgeocode()
来自 ggmap
:
library(ggmap)
gc <- c(-73.596706, 45.485501)
revgeocode(gc)
给出:
#[1] "4333 Rue Sherbrooke O, Westmount, QC H3Z 1E2, Canada"
注意:正如评论中提到的,此方法使用 Google 地图 API,而不是 OpenStreetMap。您每天有 2500 个查询的限制。您可以随时使用 geocodeQueryCheck()
来自包文档:
reverse geocodes a longitude/latitude location using Google Maps. Note that in most cases by using this function you are agreeing to the Google Maps API Terms of Service at https://developers.google.com/maps/terms.
更新
如果您需要更详细的信息,请使用 output = "all"
并提取您需要的组件:
lst <- list(
g1 = c(-73.681069, 41.433155),
g2 = c(-73.643196, 41.416240),
g3 = c(-73.653324, 41.464168)
)
res <- lapply(lst, function(x) revgeocode(x, output = "all")[[1]][[1]][[1]][[2]])
给出:
#$g1
#$g1$long_name
#[1] "Highway 52"
#
#$g1$short_name
#[1] "NY-52"
#
#$g1$types
#[1] "route"
#
#
#$g2
#$g2$long_name
#[1] "Carmel Avenue"
#
#$g2$short_name
#[1] "US-6"
#
#$g2$types
#[1] "route"
#
#
#$g3
#$g3$long_name
#[1] "Wakefield Road"
#
#$g3$short_name
#[1] "Wakefield Rd"
#
#$g3$types
#[1] "route"
使用 Google 的 API 无法识别道路类型(但是 - 他们将来可能会引入该功能)。
但是您可以使用他们的 Roads API 来获取给定坐标集的道路详细信息。
我已经编写了通过函数 google_snapToRoads()
和 google_nearestRoads()
访问道路 API 的 googleway 程序包,如果您有高级帐户,则可以使用 google_speedLimits()
在对 Google 的 API 的所有调用中,您需要在您使用的每个 API 上启用 Google API key。
library(googleway)
df_points <- data.frame(lat = c(60.1707, 60.172, 60.192),
lon = c(24.9426, 24.86, 24.89))
## plot the points on a map
google_map(key = map_key) %>%
add_markers(df_points)
nearRoads <- google_nearestRoads(df_points, key = api_key)
nearRoads
# $snappedPoints
# location.latitude location.longitude originalIndex placeId
# 1 60.17070 24.94272 0 ChIJNX9BrM0LkkYRIM-cQg265e8
# 2 60.17229 24.86028 1 ChIJpf7azXMKkkYRsk5L-U5W4ZQ
# 3 60.17229 24.86028 1 ChIJpf7azXMKkkYRs05L-U5W4ZQ
# 4 60.19165 24.88997 2 ChIJN1s1vhwKkkYRKGm4l5KmISI
# 5 60.19165 24.88997 2 ChIJN1s1vhwKkkYRKWm4l5KmISI
在这些结果中,originalIndex
值告诉您该值指的是哪个原始 df_points
(其中 0 == df_points
的第一行,1 ==第二行df_points
)
placeId
值是 Google 的唯一键,用于标识其数据库中的每个位置。因此,您可以使用 Google 的地点 API 获取有关这些地点的信息
roadDetails <- lapply(nearRoads$snappedPoints$placeId, function(x){
google_place_details(place_id = x, key = api_key)
})
## road address
lapply(roadDetails, function(x){
x[['result']][['formatted_address']]
})
# [[1]]
# [1] "Rautatientori, 00100 Helsinki, Finland"
#
# [[2]]
# [1] "Svedjeplogsstigen 7-9, 00340 Helsingfors, Finland"
#
# [[3]]
# [1] "Svedjeplogsstigen 18-10, 00340 Helsingfors, Finland"
#
# [[4]]
# [1] "Meilahdentie, 00250 Helsinki, Finland"
#
# [[5]]
# [1] "Meilahdentie, 00250 Helsinki, Finland"