-1

我正在尝试为一批地址生成人口普查大地水准面。当我在 tigris 包中使用“append_geoid”函数时,r 返回“call_geolocator 中的错误(as.character(address$street[i]), as.character(address$city[i]), : Bad Request (HTTP 400) )”

我使用了 r 文档中给出的示例数据,它产生了相同的结果。代码如下。任何有关如何解决问题的帮助表示赞赏!

airports <- dplyr::data_frame(street = "700 Catalina Dr", city = "Daytona Beach", state = "FL")

append_geoid(airports, 'tr) # Populate Census Tract GEOID
4

1 回答 1

0

编辑:包的固定版本在 github 上:

remotes::install_github("walkerke/tigris")

然后再试一次

编辑2:

github 上的版本似乎仍然给出错误,尽管这次不同。HTTP 调用成功,但响应不包含他的函数所期望的内容。我会联系他或她。

我的初始帖子:

我收到了和你一样的信息。

我做了:debug( call_geolocator )

然后再次运行它,这次是单步执行。在几行代码之后,它创建了 url:https://geocoding.geo.census.gov/geocoder/geographies/address?street=700%20Catalina%20Dr&city=Daytona%20Beach&state=FL&benchmark=Public_AR_Census2010&vintage=Census2010_Census2010&layers=14&format=json

然后此网址失败。在浏览器中打开这个 url 也会出现错误,说无效的基准。

在这一点上,是时候给作者打电话,让他​​知道他的包不再工作了。

作为参考,这是调试会话在我的终端中的样子,直到我检查了创建的完整 url 并点击Q停止调试:

> debug( call_geolocator )
> append_geoid(airports, 'tract') # Populate Census Tract GEOID
debugging in: call_geolocator(as.character(address$street[i]), as.character(address$city[i]), 
    as.character(address$state[i]))
debug: {
    call_start <- "https://geocoding.geo.census.gov/geocoder/geographies/address?"
    if (is.na(zip)) {
        url <- paste0("street=", utils::URLencode(street), "&city=", 
            utils::URLencode(city), "&state=", state)
    }
    if (!is.na(zip)) {
        if (class(zip) == "character" & nchar(zip) == 5 & !grepl("\\D", 
            zip)) {
            url <- paste0("street=", utils::URLencode(street), 
                "&city=", utils::URLencode(city), "&state=", 
                state, "&zip=", zip)
        }
        else {
            message("'zip' (", paste0(zip), ") was not a 5-character-long string composed of :digits:. Using only street, city, state.")
            url <- paste0("street=", utils::URLencode(street), 
                "&city=", utils::URLencode(city), "&state=", 
                state)
        }
    }
    call_end <- "&benchmark=Public_AR_Census2010&vintage=Census2010_Census2010&layers=14&format=json"
    url_full <- paste0(call_start, url, call_end)
    r <- httr::GET(url_full)
    httr::stop_for_status(r)
    response <- httr::content(r)
    if (length(response$result$addressMatches) == 0) {
        message(paste0("Address (", street, " ", city, " ", state, 
            ") returned no address matches. An NA was returned."))
        return(NA_character_)
    }
    else {
        if (length(response$result$addressMatches) > 1) {
            message(paste0("Address (", street, " ", city, " ", 
                state, ") returned more than one address match. The first match was returned."))
        }
        return(response$result$addressMatches[[1]]$geographies$`Census Blocks`[[1]]$GEOID)
    }
}
Browse[2]> 
debug: call_start <- "https://geocoding.geo.census.gov/geocoder/geographies/address?"
Browse[2]> 
debug: if (is.na(zip)) {
    url <- paste0("street=", utils::URLencode(street), "&city=", 
        utils::URLencode(city), "&state=", state)
}
Browse[2]> 
debug: url <- paste0("street=", utils::URLencode(street), "&city=", 
    utils::URLencode(city), "&state=", state)
Browse[2]> 
debug: if (!is.na(zip)) {
    if (class(zip) == "character" & nchar(zip) == 5 & !grepl("\\D", 
        zip)) {
        url <- paste0("street=", utils::URLencode(street), "&city=", 
            utils::URLencode(city), "&state=", state, "&zip=", 
            zip)
    }
    else {
        message("'zip' (", paste0(zip), ") was not a 5-character-long string composed of :digits:. Using only street, city, state.")
        url <- paste0("street=", utils::URLencode(street), "&city=", 
            utils::URLencode(city), "&state=", state)
    }
}
Browse[2]> 
debug: call_end <- "&benchmark=Public_AR_Census2010&vintage=Census2010_Census2010&layers=14&format=json"
Browse[2]> 
debug: url_full <- paste0(call_start, url, call_end)
Browse[2]> 
debug: r <- httr::GET(url_full)
Browse[2]> url_full
[1] "https://geocoding.geo.census.gov/geocoder/geographies/address?street=700%20Catalina%20Dr&city=Daytona%20Beach&state=FL&benchmark=Public_AR_Census2010&vintage=Census2010_Census2010&layers=14&format=json"
Browse[2]> Q

进入这个人机界面:https ://geocoding.geo.census.gov/geocoder/locations/address?form

它确实看起来像上面那个 url 中的基准在下拉选择框中不再可用。将其更改为Public_AR_Census2020会产生另一个错误,Invalid vintage in request. 在该字符串中将 2010 更改为 2020 会导致 HTTP 请求成功:https://geocoding.geo.census.gov/geocoder/geographies/address?street=700%20Catalina%20Dr&city=Daytona%20Beach&state=FL&benchmark=Public_AR_Census2020&vintage=Census2010_Census2010&layers= 14&格式=json

在这一点上,这对你并没有多大帮助,但至少你可以联系作者,说明问题可以解决,你可以给他一些信息来开始工作。

如果你很精明,你可以克隆他的包源并自己修复它,向他提供修复,但仍然使用你自己的固定包,直到他解决。

于 2021-03-08T23:36:31.863 回答