gpt4 book ai didi

RDSTK : Reverse geocode lat/lon to city (using coordinates2politics)

转载 作者:行者123 更新时间:2023-12-04 23:47:05 24 4
gpt4 key购买 nike

我正在为 R 开发一个 Shiny 应用程序,我正在尝试使用 RDSTK 包对经纬度对列表进行反向地理编码,并从 json 结果中获取 CITY 并将其保存到列表中。工作流程是:

  • SQLDF 选择日期范围内的所有记录。
  • 反向地理编码记录并将列添加到具有特定城市的数据框中。
  • 再次使用 SQLDF 按城市获取计数。

  • 我在理解如何获取 JSON 输出,将其转换为数据帧,然后将其 cbind 回原始数据帧时遇到了很多麻烦。任何帮助将非常感激!请参阅以下代码以供引用:

    数据框:
    df <- data.frame(lat=c(34.048381, 37.757836, 40.729855, 42.356391),
    lon=c(-118.266164, -122.441033, -73.987921, -71.062307))

    我能够从返回的 JSON 列表中提取城市,但我一辈子都无法弄清楚如何为更大的纬度/经度对列表多次执行此操作。通过stackoverflow搜索主要导致dstk在R之外。

    我理想的输出是:
    lat        lon           city
    34.048381 -118.266164 Los Angeles
    37.757836 -122.441033 San Francisco
    40.729855 -73.987921 New York
    42.356391 -71.062307 Boston

    我也试过这个例子: R: How to GeoCode a simple address using Data Science Toolbox虽然我似乎无法为坐标 2 政治重新设计它。

    有输入吗?

    最佳答案

    FWIW,这是使用 Google API 的一种简单替代方法:

    library(ggmap)
    res <- lapply(with(df, paste(lat, lon, sep = ",")), geocode, output = "more")
    transform(df, city = sapply(res, "[[", "locality"))
    # lat lon city
    # 1 34.04838 -118.26616 los angeles
    # 2 37.75784 -122.44103 san francisco
    # 3 40.72986 -73.98792 new york
    # 4 42.35639 -71.06231 boston

    关于RDSTK : Reverse geocode lat/lon to city (using coordinates2politics),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189241/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com