Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # set connection
- conn <- dbConnect("PostgreSQL",user="postgres",password="pass",port=5432,dbname="postgis_user")
- # set query and reproject to EPSG:4326 for GeoJSON creation
- # (the real query is more involved with a bounding box etc)
- qry <- "SELECT ST_Transform(table.geom, 4326) as geom FROM table"
- # i then read as an sf object
- pols <- st_read_db(conn, query=qry, geom="geom")
- # and convert to GeoJSON
- pols.js <- geojsonio::geojson_json(pols)
- qry1 <- "SELECT ST_AsGeoJSON(ST_Transform(geom,4326)) FROM table"
- pols1 <- dbGetQuery(conn,qry1)
- ## no error messages but no display in Leaflet (incorrectly constructed geoJSON i assume, returned as a data.frame)
- qry2 <- "SELECT json_build_object(
- 'type', 'FeatureCollection',
- 'features', json_agg(
- json_build_object(
- 'type', 'Feature',
- 'id', gid,
- 'geometry', ST_AsGeoJSON(ST_ForceRHR(st_transform(geom,4326)))::json,
- 'properties', jsonb_set(row_to_json(table)::jsonb,'{geom}','0',false)
- )
- )
- )
- FROM table"
- pols2 <- dbGetQuery(conn,qry2)
- ## hangs and errors: "DETAIL: Cannot enlarge string buffer containing 1073740688 bytes by 3167 more bytes."
- qry3 <- "SELECT row_to_json(f) As feature
- FROM (SELECT 'Feature' As type,
- ST_AsGeoJSON(ST_Transform(l.geom, 4326))::json As geometry,
- row_to_json((SELECT l FROM (SELECT id, 'l' AS type) As l))
- As properties
- FROM table As l) As f"
- pols3 <- dbGetQuery(conn,qry3)
- ## this gives a similar output to attempt 1 above but gives a warning "RS-DBI driver
- ## warning: (unrecognized PostgreSQL field type json (id:114) in column 0)", which
- ## i'm guessing is bcos i've got the syntax wrong.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement