API request with R

Viv*_*ien 0 api geocoding r

I try to do geocoding of French addresses. I'd like to use the following website : http://adresse.data.gouv.fr/

There is an example on this website on how is working the API but I think it's some Linux code and I'd like to translate in R code. The aim is to give a csv file with addresses and the result should be geo coordinates.

Linux code (example give on the website)

http --timeout 600 -f POST http://api-adresse.data.gouv.fr/search/csv/ data@path/to/file.csv 
Run Code Online (Sandbox Code Playgroud)

I tried to "translate" this in R with the following code

library(httr)
library(RCurl)
queryResults=POST("http://api-adresse.data.gouv.fr/search/csv/",body=list(data=fileUpload("file.csv"))) 
result_geocodage=content(queryResults)
Run Code Online (Sandbox Code Playgroud)

But unfortunately I have a bad request error.

有人知道我在R的翻译中缺少什么吗?

谢谢!

luk*_*keA 5

这是一个例子。首先,一些示例数据加上请求:

library(httr)
df <- data.frame(c("13 Boulevard Chanzy", "Gloucester St"), 
                 c("93100 Montreuil", "Jersey"))
write.csv2(df, tf <- tempfile(fileext = ".csv"))
res <- POST("http://api-adresse.data.gouv.fr/search/csv/", 
            timeout(600), 
            body = list(data = upload_file(tf)))
Run Code Online (Sandbox Code Playgroud)

然后,结果:

content(res, sep = ";", row.names = 1)
# c..13.Boulevard.Chanzy....Gloucester.St.. c..93100.Montreuil....Jersey.. latitude longitude
# 1                       13 Boulevard Chanzy                93100 Montreuil 48.85825  2.434462
# 2                             Gloucester St                         Jersey 49.46712  1.145554
# result_label result_score result_type                result_id result_housenumber
# 1                13 Boulevard Chanzy 93100 Montreuil         0.88 housenumber ADRNIVX_0000000268334929                 13
# 2 2 Résidence le Jersey 76160 Saint-Martin-du-Vivier         0.24 housenumber ADRNIVX_0000000311480901                  2
# result_name result_street result_postcode            result_city                       result_context result_citycode
# 1    Boulevard Chanzy            NA           93100              Montreuil 93, Seine-Saint-Denis, Île-de-France           93048
# 2 Résidence le Jersey            NA           76160 Saint-Martin-du-Vivier  76, Seine-Maritime, Haute-Normandie           76617
Run Code Online (Sandbox Code Playgroud)

或者,只是坐标:

subset(content(res, sep = ";", row.names = 1, check.names = FALSE), select = c("latitude", "longitude"))
# latitude longitude
# 1 48.85825  2.434462
# 2 49.46712  1.145554
Run Code Online (Sandbox Code Playgroud)