Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. # set connection
  2. conn <- dbConnect("PostgreSQL",user="postgres",password="pass",port=5432,dbname="postgis_user")
  3.  
  4. # set query and reproject to EPSG:4326 for GeoJSON creation
  5. # (the real query is more involved with a bounding box etc)
  6. qry <- "SELECT ST_Transform(table.geom, 4326) as geom FROM table"
  7.  
  8. # i then read as an sf object
  9. pols <- st_read_db(conn, query=qry, geom="geom")
  10.  
  11. # and convert to GeoJSON
  12. pols.js <- geojsonio::geojson_json(pols)
  13.  
  14. qry1 <- "SELECT ST_AsGeoJSON(ST_Transform(geom,4326)) FROM table"
  15. pols1 <- dbGetQuery(conn,qry1)
  16. ## no error messages but no display in Leaflet (incorrectly constructed geoJSON i assume, returned as a data.frame)
  17.  
  18. qry2 <- "SELECT json_build_object(
  19. 'type', 'FeatureCollection',
  20.  
  21. 'features', json_agg(
  22. json_build_object(
  23. 'type', 'Feature',
  24. 'id', gid,
  25. 'geometry', ST_AsGeoJSON(ST_ForceRHR(st_transform(geom,4326)))::json,
  26. 'properties', jsonb_set(row_to_json(table)::jsonb,'{geom}','0',false)
  27. )
  28. )
  29. )
  30. FROM table"
  31. pols2 <- dbGetQuery(conn,qry2)
  32. ## hangs and errors: "DETAIL: Cannot enlarge string buffer containing 1073740688 bytes by 3167 more bytes."
  33.  
  34. qry3 <- "SELECT row_to_json(f) As feature
  35. FROM (SELECT 'Feature' As type,
  36. ST_AsGeoJSON(ST_Transform(l.geom, 4326))::json As geometry,
  37. row_to_json((SELECT l FROM (SELECT id, 'l' AS type) As l))
  38. As properties
  39. FROM table As l) As f"
  40. pols3 <- dbGetQuery(conn,qry3)
  41. ## this gives a similar output to attempt 1 above but gives a warning "RS-DBI driver
  42. ## warning: (unrecognized PostgreSQL field type json (id:114) in column 0)", which
  43. ## i'm guessing is bcos i've got the syntax wrong.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement