Created
April 21, 2016 08:29
-
-
Save mbannert/5847ac0a47958f470689b8010da77dba to your computer and use it in GitHub Desktop.
PostgreSQL Postgis R GeoJSON example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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