Skip to content

Instantly share code, notes, and snippets.

@mbannert
Created April 21, 2016 08:29
Show Gist options
  • Select an option

  • Save mbannert/5847ac0a47958f470689b8010da77dba to your computer and use it in GitHub Desktop.

Select an option

Save mbannert/5847ac0a47958f470689b8010da77dba to your computer and use it in GitHub Desktop.
PostgreSQL Postgis R GeoJSON example
# Postgis data type in my case is WSG84: geom | geometry(Point,4326)
library(jsonlite)
library(RPostgreSQL)
source("dbConnect.R") # create some connection object called con
p <- dbGetQuery(con,
"SELECT id,
postal_code,
st_asGeoJSON(geom)
FROM sometable
ORDER BY geom ASC")
p$lng <- sapply(p$st_asgeojson,function(x){
tryCatch({fromJSON(x)$coordinates[1]},
error = function(e) NA)
})
p$lat <- sapply(p$st_asgeojson,function(x){
tryCatch({fromJSON(x)$coordinates[2]},
error = function(e) NA)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment