Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created March 31, 2014 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/9901224 to your computer and use it in GitHub Desktop.
Save ramnathv/9901224 to your computer and use it in GitHub Desktop.
to_geo_json <- function(x, ...){
UseMethod('to_geo_json')
}
to_geo_json.data.frame <- function(x, lat = "latitude", lon = "longitude"){
geo_json = vector('list', NROW(x))
y = lapply(1:NROW(x), function(i){
if (is.null(x[i,lat]) || is.null(x[i,lon])){
return(NULL)
} else {
list(
type = "Feature",
geometry = list(
type = "Point",
coordinates = as.numeric(c(x[i,lon], x[i,lat])),
properties = x[i,!(names(x) %in% c(lat, lon))]
)
)
}
setNames(Filter(function(z) !is.null(z), y), NULL)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment