R - 将开放街道地图地理编码的结果与输入数据相关联

时间:2017-11-06 09:33:07

标签: r openstreetmap nominatim

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

如果您的位置未转换为经度/纬度,则会出现问题。实际上,没有办法链接输入向量和输出数据帧。

以下是一个例子:

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

1 个答案:

答案 0 :(得分:2)

您可以尝试使用lapply()浏览这些位置,并从lat返回的lon获取tibbleosm_geocode()值:

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

返回:

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

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