1

I have a Postgis Database from which I query data. I know that I can use the rgdal::readOGR() function with the PG driver to get geoms from the database into R.

However, I am also querying lots of non-spatial data from the same database using DBI. From a programming point of view it seems not clean to have two different methods to connect to the same database in the same script.

What I do now is to query the column with SELECT ST_AsBinary(the_geom) ... using DBI and then read the result with wkb::readWKB(). This works (only uses DBI for connection) but is quite slow.

Is there any way to read a geom string as returned from Postgis with rgdal when the string is already available as variable?

asked Jul 5, 2019 at 7:06
1
  • Can you show some example code with timings to demonstrate how slow using DBI and converting is compared to reading with the OGR PG driver? Commented Jul 5, 2019 at 10:53

1 Answer 1

0

I always find it difficult, but now there is existing solutions, and even an R package: rpostgis (https://journal.r-project.org/archive/2018/RJ-2018-025/RJ-2018-025.pdf)

There is functions to read/write from/to postgis and even to store and read R data.frame (dbWriteDataFrame)

The connection its uses is RPostGreSQL, so you can use it simply for normal tables.

answered Jul 19, 2019 at 13:26
1
  • That looks like a really useful package, thank you! Commented Jul 22, 2019 at 6:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.