人口普查批量地理编码 API,R 中数据框(非 CSV)中的源地址
Census Batch Geocoding API with source addresses in data frame (not CSV) in R
我正在尝试将人口普查局的批量地理编码器 (http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf) 与 R 一起使用。输入地址在数据框中,而不是 CSV。由于地址是一个中间步骤,我不想将它们写入 CSV。
我已经阅读了一些关于 Whosebug 的帖子。 Hadley 的解决方案 (Posting to and Receiving data from API using httr in R) illustrates how to upload an existing CSV file. MrFlick's solution () 似乎接近我想要的,但使用的是字符串,而不是数据框。
下面是我的代码方式:
#generate data frame of test addresses for this example
a = c(1, 2, 3)
b = c("125 Worth Street", "258 Broadway", "8 Centre Street")
c = rep("New York", 3)
d = rep("NY", 3)
e = c("10013","10007","10007")
addresses = data.frame(a,b,c,d,e)
#names specified by API documentation
colnames(addresses) <- c("Unique ID","Street address","City","State","ZIP")
apiurl <- "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch"
req <- POST(apiurl, body=list(
addressFile = RCurl::fileUpload(
filename = "test.csv",
contents = addresses
),
benchmark = "Public_AR_Census2010",
vintage = "Census2010_Census2010"
),
encode="multipart"
)
stop_for_status(req)
提前致谢。
如果您愿意将数据写入临时文件...
library("httr")
a = c(1, 2, 3)
b = c("125 Worth Street", "258 Broadway", "8 Centre Street")
c = rep("New York", 3)
d = rep("NY", 3)
e = c("10013","10007","10007")
addresses = data.frame(a,b,c,d,e)
colnames(addresses) <- c("Unique_ID","Street address","City","State","ZIP")
apiurl <- "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch"
file <- tempfile(fileext = ".csv")
write.csv(addresses, file, row.names = FALSE)
req <- POST(apiurl, body=list(
addressFile = upload_file(file),
benchmark = "Public_AR_Census2010",
vintage = "Census2010_Census2010"
),
encode="multipart"
)
content(req, "text", encoding = "UTF-8")
#> [1] "\"3\",\"8 Centre Street, New York, NY, 10007\",\"Match\",\"Non_Exact\",\"8 Centre St, NEW YORK, NY, 10013\",\"-74.00442,40.712765\",\"59660429\",\"R\",\"36\",\"061\",\"002900\",\"4019\"\n\"2\",\"258 Broadway, New York, NY, 10007\",\"No_Match\"\n\"1\",\"125 Worth Street, New York, NY, 10013\",\"Match\",\"Exact\",\"125 Worth St, NEW YORK, NY, 10013\",\"-74.0027,40.715446\",\"59660405\",\"L\",\"36\",\"061\",\"003100\",\"1012\"\n\"Unique_ID\",\"Street address, City, State, ZIP\",\"No_Match\"\n"
我正在尝试将人口普查局的批量地理编码器 (http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf) 与 R 一起使用。输入地址在数据框中,而不是 CSV。由于地址是一个中间步骤,我不想将它们写入 CSV。
我已经阅读了一些关于 Whosebug 的帖子。 Hadley 的解决方案 (Posting to and Receiving data from API using httr in R) illustrates how to upload an existing CSV file. MrFlick's solution (
下面是我的代码方式:
#generate data frame of test addresses for this example
a = c(1, 2, 3)
b = c("125 Worth Street", "258 Broadway", "8 Centre Street")
c = rep("New York", 3)
d = rep("NY", 3)
e = c("10013","10007","10007")
addresses = data.frame(a,b,c,d,e)
#names specified by API documentation
colnames(addresses) <- c("Unique ID","Street address","City","State","ZIP")
apiurl <- "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch"
req <- POST(apiurl, body=list(
addressFile = RCurl::fileUpload(
filename = "test.csv",
contents = addresses
),
benchmark = "Public_AR_Census2010",
vintage = "Census2010_Census2010"
),
encode="multipart"
)
stop_for_status(req)
提前致谢。
如果您愿意将数据写入临时文件...
library("httr")
a = c(1, 2, 3)
b = c("125 Worth Street", "258 Broadway", "8 Centre Street")
c = rep("New York", 3)
d = rep("NY", 3)
e = c("10013","10007","10007")
addresses = data.frame(a,b,c,d,e)
colnames(addresses) <- c("Unique_ID","Street address","City","State","ZIP")
apiurl <- "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch"
file <- tempfile(fileext = ".csv")
write.csv(addresses, file, row.names = FALSE)
req <- POST(apiurl, body=list(
addressFile = upload_file(file),
benchmark = "Public_AR_Census2010",
vintage = "Census2010_Census2010"
),
encode="multipart"
)
content(req, "text", encoding = "UTF-8")
#> [1] "\"3\",\"8 Centre Street, New York, NY, 10007\",\"Match\",\"Non_Exact\",\"8 Centre St, NEW YORK, NY, 10013\",\"-74.00442,40.712765\",\"59660429\",\"R\",\"36\",\"061\",\"002900\",\"4019\"\n\"2\",\"258 Broadway, New York, NY, 10007\",\"No_Match\"\n\"1\",\"125 Worth Street, New York, NY, 10013\",\"Match\",\"Exact\",\"125 Worth St, NEW YORK, NY, 10013\",\"-74.0027,40.715446\",\"59660405\",\"L\",\"36\",\"061\",\"003100\",\"1012\"\n\"Unique_ID\",\"Street address, City, State, ZIP\",\"No_Match\"\n"