#' @export getDensities
getDensities <- function(latitudes, longitudes, n = 200) {
require(RCurl)
require(rjson)
if(length(latitudes) != length(longitudes)) {
stop("Different length vectors.")
}
coord2stat <- function(x) {
output <- getURL(paste0("http://www.datasciencetoolkit.org/coordinates2statistics/",
toJSON(x), "?statistics=population_density"))
output <- fromJSON(output)
return(output)
}
all.pos <- mapply(c, round(latitudes, 3), round(longitudes, 3), SIMPLIFY = FALSE)
n <- ceiling(length(latitudes) / n)
chunk <- function(x,n) split(x, factor(sort(rank(x)%%n)))
chunks <- chunk(x = 1:length(all.pos), n = n)
all.output <- as.list(rep(NA, length(chunks)))
for(i in 1:length(chunks)) {
print(i)
current.pos <- all.pos[chunks[[i]]]
temp.output <- coord2stat(current.pos)
all.output[[i]] <- sapply(temp.output, function(x) x$statistics$population_density$value)
}
all.output <- do.call(c, all.output)
return(all.output)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.