R: How to identify a road type using GPS?

五迷三道 提交于 2020-01-01 18:23:07

问题


I have a GPS coordinates of several points and I want to know if they are on a highway, or trunk road, or minor road, and it would be even greater if I could identify a road name. I'm using R leaflet to draw maps and I can see with OpenStreetMap that different types of roads are colored differently, and I wonder how I can extract this information. It's not a problem to use Google maps instead if it will solve my problem.

I would appreciate any help.


回答1:


You can use revgeocode() from ggmap:

library(ggmap)
gc <- c(-73.596706, 45.485501)
revgeocode(gc)

Which gives:

#[1] "4333 Rue Sherbrooke O, Westmount, QC H3Z 1E2, Canada"

Note: As per mentioned in the comments, this method uses Google Maps API, not OpenStreetMap. You have a limit of 2500 queries per day. You can always check how many queries you have left using geocodeQueryCheck()

From the package documentation:

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.


Update

If you need more detailed information, use output = "all" and extract the components you need:

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]])

Which gives:

#$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"



回答2:


Using Google's API it's not possible to identify the type of road (yet - they may introduce that capability in the future).

But you can use their Roads API to get the road details for a given set of coordinates.

I've written the googleway package that accesses the roads API through the functions google_snapToRoads() and google_nearestRoads(), and if you have a premium account you can use google_speedLimits()

In all calls to Google's API you need a Google API key enabled on each API you are using.

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

In these results, the originalIndex value tells you which of the orignal df_points the value is refering to (where 0 == the first row of df_points, 1 == the second row of df_points)

The placeId value is Google's unique key that identifies each place in their database. So you can then use Google's Places API to get the information about those places

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"


来源:https://stackoverflow.com/questions/33318880/r-how-to-identify-a-road-type-using-gps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!