I'm working on a data frame of latitudes and longitudes values to get the corresponding address using the coordinates2politics
function available in the data science toolkit. The function works well for a single lat&long value, but when I parsing an array in the body of a POST
call it doesn't work.
Code snippet:
x <- data.frame(lat= c(35.18713,37.86058),long= c(-117.88536,-122.29673))
data <- paste0("[",paste(paste0("\"",x$lat,"%2c",x$long,"\""),collapse=","),"]")
url <- "http://www.datasciencetoolkit.org/coordinates2politics/"
response <- POST(url,body=data)
response
json <- fromJSON(content(response,type="text"))
json
The resulted places are for a different lat and long values, and I don't know why? Here is the result for the first input, you can observe that lat = 51, long=53 !!
json
[[1]]
[[1]]$politics
[[1]]$politics[[1]]
[[1]]$politics[[1]]$type
[1] "admin2"
[[1]]$politics[[1]]$friendly_type
[1] "country"
[[1]]$politics[[1]]$name
[1] "Kazakhstan"
[[1]]$politics[[1]]$code
[1] "kaz"
[[1]]$politics[[2]]
[[1]]$politics[[2]]$type
[1] "admin4"
[[1]]$politics[[2]]$friendly_type
[1] "state"
[[1]]$politics[[2]]$name
[1] "West Kazakhstan"
[[1]]$politics[[2]]$code
[1] "kz07"
[[1]]$location
[[1]]$location$latitude
[1] 51
[[1]]$location$longitude
[1] 53
好了,我找到了解决方案。问题在于定义data
对象,因为该函数需要两个键值组成的数组,每个键值对应于[lat,long]
对,因此数据对象应定义为data <- paste0("[",paste(paste0("[",x$lat,",",x$long,"]"),collapse=","),
“]”)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.