1

我正在使用这个googleway包,我有一堆地址,我需要解析出嵌套列表中地址的各个组成部分。循环(不鼓励)和应用函数似乎都令人困惑,我不确定是否有一个整洁的解决方案。我发现这个map函数(特别pluck是它在后端列表上调用的函数)可以实现我的目标,所以我将分享我的解决方案。

问题:我需要提取有关白宫的某些信息,例如

  • 纬度
  • 经度

您需要使用 设置您的 Google Cloud API 密钥googleway::set_key(API_KEY),但这只是嵌套列表的一个示例,我希望使用此包的人会看到。

# Address for the White House and the Lincoln Memorial

address_vec <- c(
  "1600 Pennsylvania Ave NW, Washington, DC 20006",
  "2 Lincoln Memorial Cir NW, Washington, DC 20002"
)

address_vec <- pmap(list(address_vec), googleway::google_geocode) 

输出

[[1]]
[[1]]$results
                                                                                                                                                                                                                                                                                                                             address_components
1 1600, Pennsylvania Avenue Northwest, Northwest Washington, Washington, District of Columbia, United States, 20500, 1600, Pennsylvania Avenue NW, Northwest Washington, Washington, DC, US, 20500, street_number, route, neighborhood, political, locality, political, administrative_area_level_1, political, country, political, postal_code
                                       formatted_address geometry.bounds.northeast.lat
1 1600 Pennsylvania Avenue NW, Washington, DC 20500, USA                       38.8979
  geometry.bounds.northeast.lng geometry.bounds.southwest.lat geometry.bounds.southwest.lng geometry.location.lat
1                     -77.03551                      38.89731                     -77.03796              38.89766
  geometry.location.lng geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng
1             -77.03657                ROOFTOP                        38.89895                       -77.03539
  geometry.viewport.southwest.lat geometry.viewport.southwest.lng                    place_id
1                        38.89626                       -77.03808 ChIJGVtI4by3t4kRr51d_Qm_x58
                                      types
1 establishment, point_of_interest, premise

[[1]]$status
[1] "OK"


[[2]]
[[2]]$results
                                                                                                                                                                                                                                                                                                                            address_components
1 2, Lincoln Memorial Circle Northwest, Southwest Washington, Washington, District of Columbia, United States, 20037, 2, Lincoln Memorial Cir NW, Southwest Washington, Washington, DC, US, 20037, street_number, route, neighborhood, political, locality, political, administrative_area_level_1, political, country, political, postal_code
                                     formatted_address geometry.location.lat geometry.location.lng
1 2 Lincoln Memorial Cir NW, Washington, DC 20037, USA              38.88927             -77.05018
  geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng
1                ROOFTOP                        38.89062                       -77.04883
  geometry.viewport.southwest.lat geometry.viewport.southwest.lng                    place_id
1                        38.88792                       -77.05152 ChIJgRuEham3t4kRFju4R6De__g
      plus_code.compound_code plus_code.global_code          types
1 VWQX+PW Washington, DC, USA           87C4VWQX+PW street_address

[[2]]$status
[1] "OK"
4

2 回答 2

1

我想出的解决方案是一个自定义函数,可以访问列表的任何部分:

geocode_accessor <- function(df, accessor, ...) {
  unlist(map(df, list(accessor, ...)))
}

这需要理解三个重要部分:

  1. map函数正在为我们调用该pluck函数(它取代了 的使用[[)。你可以在这里阅读更多关于正在发生的事情,但只要知道这让我们可以通过名称访问事物。
  2. 函数定义和列表中的“...”允许我们访问多个级别。同样,list()在 pluck 文档中解释了使用 来访问列表中的更多级别
  3. 使用 unlist 将列表转换为向量(我在我的实例中想要的)

综上所述,我们可以得到白宫和林肯纪念堂的纬度:

geocode_accessor(address_vec, "results", "geometry", "location", "lat")

[1] 38.89766 38.88927

于 2021-01-08T23:04:41.060 回答
1

这是我从Googleway Vignette获得的一些代码:

df <- google_geocode(address = "Flinders Street Station",
                     key = key,
                     simplify = TRUE)


geocode_coordinates(df)
#         lat      lng
# 1 -37.81827 144.9671

看起来您需要做的是:

df <- google_geocode("1600 Pennsylvania Ave")
geocode_coordinates(df)
于 2021-01-08T22:57:06.693 回答