【问题标题】:ggmap in R: How do I extract individual location features from geocoding?R中的ggmap:如何从地理编码中提取单个位置特征?
【发布时间】:2018-08-31 15:10:21
【问题描述】:

我正在尝试清理用户输入的地址,因此我认为使用 GGMAP 提取经度/纬度和使用的地址将是清理所有内容的一种方法。但是,它吐出的地址有时会在地址中包含俗名,因此很难解析出各个位置方面。

这是我正在使用的代码

for(i in 1:nrow(Raw_Address))
   {
     result <- try(geocode(Raw_Address$Address_Total[i], output = "more", source = "google"))
     Raw_Address$lon[i] <- as.numeric(result[1])
     Raw_Address$lat[i] <- as.numeric(result[2])
     Raw_Address$geoAddress[i] <- as.character(result[3])

   }

我尝试将“latlona”更改为“more”并查看结果数字,但只得到不同的经度/纬度。我在文档中没有看到任何显示结果向量的地方。

基本上,我想要街道名称、城市、州、邮编、经度和纬度。

编辑:这是数据示例

用户输入: 1651 SE TIFFANY AVE。圣港露西 FL

GGMAP 输出: martin health systems - tiffany ave., 1651 se tiffany ave, port st.露西,佛罗里达州 34952,美国

由于俗名,这很难解析。我可以使用 stringr 包来尝试解析,但它可能不会包含所有内容。但是当一些用户拼错“Tiffany”或拼写“Saint”而不是“St”时,它会返回一个不同的地址。

【问题讨论】:

  • 请提供一些example data 以供其他人帮助。如果您还可以提供您所看到的以及您想要的结果。
  • 谢谢!我添加了一个例子
  • 我可能不清楚。当我对该地址进行地理编码时,除了address 字段之外,它还返回一个字段拆分的数据帧,例如street_number 等。您可以只选择这些字段,然后根据需要进行组合。

标签: r ggmap street-address


【解决方案1】:

而不是使用for 循环,purrr::map_dfr 将遍历一个向量并将生成的数据帧 rbind 为一个,这在这里很方便。例如,

library(tidyverse)

libraries <- tribble(
    ~library,                      ~address,
    "Library of Congress",         "101 Independence Ave SE, Washington, DC 20540",
    "British Library",             "96 Euston Rd, London NW1 2DB, UK",
    "New York Public Library",     "476 5th Ave, New York, NY 10018", 
    "Library and Archives Canada", "395 Wellington St, Ottawa, ON K1A 0N4, Canada"
)

library_locations <- map_dfr(libraries$address, ggmap::geocode, 
                             output = "more", source = "dsk")

这会输出很多消息,其中一些会告诉你geocode 正在调用什么,例如

#> Information from URL : http://www.datasciencetoolkit.org/maps/api/geocode/json?address=101%20Independence%20Ave%20SE,%20Washington,%20DC%2020540&sensor=false

以及一些警告因素正在被强制影响角色:

#> Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector

它们应该是,所以你可以忽略它们。 (如果你真的想要,你可以编写更多的代码让它们消失,但你最终会得到同样的结果。)

组合生成的数据框,您将获得链接到原始数据集的所有位置数据:

full_join(libraries, library_locations)
#> Joining, by = "address"
#> # A tibble: 4 x 15
#>   library address      lon   lat type  loctype north south    east     west
#>   <chr>   <chr>      <dbl> <dbl> <chr> <chr>   <dbl> <dbl>   <dbl>    <dbl>
#> 1 Librar… 101 In…  -77.0    38.9 stre… rooftop  38.9  38.9 -77.0    -77.0  
#> 2 Britis… 96 Eus…   -0.125  51.5 stre… rooftop  51.5  51.5  -0.124   -0.126
#> 3 New Yo… 476 5t…  -74.0    40.8 stre… rooftop  40.8  40.8 -74.0    -74.0  
#> 4 Librar… 395 We… -114.     60.1 coun… approx…  83.1  41.7 -52.3   -141.   
#> # … with 5 more variables: street_number <chr>, route <chr>,
#> #   locality <chr>, administrative_area_level_1 <chr>, country <chr>

您可能会注意到,无论出于何种原因,Data Science Toolkit 完全无法对加拿大图书馆和档案馆进行地理编码 - 它被标记为国家/地区而不是地址。地理编码器有时会出现故障。从这里,子集出你不需要的任何东西。

如果您想要更多信息,您可以使用geocodeoutput = "all" 方法,但这会返回一个您需要解析的列表,这需要更多的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 2013-01-26
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多