R - Link 使用输入数据对 Open Street Map 进行地理编码的结果

R - Link the result of Open Street Map geocoding with input data

我想使用 Open Street Map(nominatim 包)对一系列位置进行地理编码。

当您的位置未转换为 longitude/latitude 时会出现问题。确实,没有办法link输入向量和输出数据框。

这是一个例子:

library(nominatim)

Location <- c("Washington", "Seattle", "Fzoieozepfvfmd", "Houston")
LonLat <- osm_geocode(Location, key = "enter your own OSM key")

View(LonLat)
# 3 observations only while there is 4 locations in the input vector
# no key to join the output data frame with the input vector

输出数据框LonLat

structure(list(place_id = c("2661769953", "151183715", "2691789858"
), licence = c("Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright", 
"Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright", 
"Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright"
), osm_type = c("relation", "relation", "relation"), osm_id = c("5396194", 
"237385", "2688911"), lat = c(38.8949549, 47.6038321, 29.7589382
), lon = c(-77.0366456, -122.3300624, -95.3676974), display_name = c("Washington, District of Columbia, United States of America", 
"Seattle, King County, Washington, United States of America", 
"Houston, Harris County, Texas, United States of America"), class = c("place", 
"place", "place"), type = c("city", "city", "city"), importance = c(0.82665678197628, 
0.80154398538761, 0.80088985079359), icon = c("http://ip-10-98-161-100.mq-us-east-1.ec2.aolcloud.net:8000/nominatim/v1/images/mapicons/poi_place_city.p.20.png", 
"http://ip-10-98-174-147.mq-us-east-1.ec2.aolcloud.net:8000/nominatim/v1/images/mapicons/poi_place_city.p.20.png", 
"http://ip-10-98-183-183.mq-us-east-1.ec2.aolcloud.net:8000/nominatim/v1/images/mapicons/poi_place_city.p.20.png"
), bbox_left = c(38.7916303, 47.4810022, 29.5370705), bbox_top = c(38.9958524, 
47.7341357, 30.1103506), bbox_right = c(-77.1197662, -122.4596959, 
-95.9097418), bbox_bottom = c(-76.9093659, -122.2244329, -95.0120524
)), .Names = c("place_id", "licence", "osm_type", "osm_id", "lat", 
"lon", "display_name", "class", "type", "importance", "icon", 
"bbox_left", "bbox_top", "bbox_right", "bbox_bottom"), row.names = c(NA, 
-3L), class = "data.frame")

您可以尝试使用 lapply() 遍历这些位置,并从 osm_geocode() 返回的 tibble 中获取 latlon 值:

LonLat <- data.table::rbindlist(
  lapply(Location, function(x, key) {

    lon_lat <- osm_geocode(x, key = key)

    lat <- ifelse(is.null(lon_lat$lat), NA, lon_lat$lat)
    lon <- ifelse(is.null(lon_lat$lon), NA, lon_lat$lon)  

    return(list(Location = x, Lat = lat, Lon = lon))

  } , key = "enter your key"))

这个returns:

         Location Lat Lon
1:     Washington  NA  NA
2:        Seattle  NA  NA
3: Fzoieozepfvfmd  NA  NA
4:        Houston  NA  NA

注意:存在 NA 是因为我没有指定正确的 API 密钥。