-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable database connection for st_read and st_write #558
Conversation
R/db.R
Outdated
#' @details st_write_db was written with help of Josh London, see \url{https://github.com/r-spatial/sf/issues/285} | ||
st_write_db = function(conn = NULL, obj, table = deparse(substitute(obj)), geom_name = "wkb_geometry", | ||
#' @details database reading was written with help of Josh London, see \url{https://github.com/r-spatial/sf/issues/285} | ||
st_write.PostgreSQLConnection = function(conn = NULL, obj, table = deparse(substitute(obj)), geom_name = "wkb_geometry", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the arguments before ... in the generic need to have identical names to that of the generic, which is: obj, dsn, layer
; similar for st_read
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that if we put the connection (dsn
) in second, then we'll need to use S4 generic. Otherwise we'll only catch st_write.sf()
. It also seems more common to see the connection as the first argument. S4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to branch in st_write.sf
- then I'm pragmatic and would like to avoid S4.
For write_xxx
function it's more common to see the object to write as first argument, I'd say.
R/deprecated.R
Outdated
st_read(...) | ||
} | ||
|
||
st_write_db <- function(...) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here, the argument order needs to change: st_write_db(conn, obj, layer)
should become st_write(obj, conn, layer, ...)
but then with correct names obj, dsn, layer
.
There is something I can't explain yet when reading the crs from the database.It might be a problem of version, but on my personal computer EPSG 28992 is different than from postgis for some decimals of the # crs from the database
st_crs(st_read("PG:dbname=postgis", "meuse"))
#> Coordinate Reference System:
#> EPSG: 28992
#> proj4string: "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079
#> +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.4171,50.3319,465.5524,
#> -0.398957,0.343988,-1.87740,4.0725 +units=m +no_defs"
# local crs
st_crs(st_read(pg, "meuse"))
#> Coordinate Reference System:
#> EPSG: 28992
#> proj4string: "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079
#> +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.2369,50.0087,465.658,
#> -0.406857,0.350733,-1.87035,4.0812 +units=m +no_defs" Where |
Yes, this indicates changes to the EPSG database, present in different proj.4 versions; this particular one is well known. National geodesy institutes update this when they improve the mapping from their national system to (in this case) the global datum WGS84. |
c410cce
to
0dac8dc
Compare
To clarify the PR:
To do:
Probably other things. Happy to discuss any of these and modify the pull request before it gets merged. |
Cool that RPostgres is out! Let me know when you have travis running here; looking good. Release sf 0.5-6 planned early Jan. |
Err, sorry to barge in here, but #592 might be a problem? Or would that be solved with the UPDATE: Sorry, I should have read
|
@dpprdan, I'll add explicit test for logical type. It's the best way to convince everyone :) |
b14431e
to
618edf2
Compare
Essentially add `PostgreSQLConnection` (RPostgreSQL) and deprecates `st_read_db()` and `st_write_db()`
* use `st_write` instead (or `dbWriteTable`) * cleanup
618edf2
to
1f6f904
Compare
This PR is ready to merge 🎉 ! however the commits are messy. @edzer, would you like me to clean up the commits before merging? |
When testing the RPostgres tests, I see > pg <- DBI::dbConnect(RPostgres::Postgres(), host = "localhost", dbname = "postgis")
Error in connection_create(names(opts), as.vector(opts)) :
fe_sendauth: no password supplied any idea? the |
When I remove the |
Check your I thought I could do without the |
Thanks, that works now. Using |
It seems to be driver dependent. For instance, ODBC was less demanding. By the way, ODBC is not fully supported, there are still some issues, but it works for most cases. So the test is just skipped for now. |
suppressWarnings(could_schema <- DBI::dbGetQuery(pg, q) %>% nrow() > 0) | ||
|
||
skip_if_not(could_schema, "Could not create schema (might need to run 'GRANT CREATE ON DATABASE postgis TO <user>')") | ||
expect_error(st_write(pts, pg, Id(schema = "public", table = "sf_meuse__")), "exists") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting
Error: `st_write(pts, pg, Id(schema = "public", table = "sf_meuse__"))` threw an error with unexpected message.
Expected match: "exists"
Actual message: "unable to find an inherited method for function ‘dbWriteTable’ for signature ‘\"PqConnection\", \"SQL\", \"missing\"’"
here -- any idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure you are running the latest DBI
and RPostgres
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did, but that didn't resolve it; sessionInfo below; postgresql 9.6.8.
When I outcomment these test, next thing I get stuck with is
> st_write(pts, pg, Id(schema = "CAP__", table = "Meuse_tbl"))
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘dbWriteTable’ for signature ‘"PqConnection", "SQL", "missing"’
What am I missing?
> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 17.10
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] sp_1.2-7 testthat_2.0.0 DBI_0.8
loaded via a namespace (and not attached):
[1] bit_1.1-12 compiler_3.4.3 magrittr_1.5 R6_2.2.2
[5] hms_0.4.2 tools_3.4.3 Rcpp_0.12.16 bit64_0.9-7
[9] grid_3.4.3 blob_1.1.1 pkgconfig_2.0.1 rlang_0.2.0
[13] RPostgres_1.1.0 lattice_0.20-35
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error is because the test uses the new way of naming tables in DBI
. It uses DBI::Id()
(prior was DBI::SQL()
). Also Rpostgres
was just recently updated (r-dbi/RPostgres#179) could you try devtools::install_github("r-dbi/rpostgres")
. I was surprised that travis ran the test without complaining.
Looking at the travis logs (devtools::session_info()
)
DBI 0.8 2018-03-02 cran (@0.8)
Can't find RPostgres
, but it must be 1.1.0. (I'm using RPostgres 1.1.0 2018-04-05 from Github (r-dbi/RPostgres@ef2ab88)
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not causing this, but in general, shouldn't RPostgres
be in Suggests
in DESCRIPTION
, (preferably with minimal version number)? Or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I don't know. I think the whole point of using DBI is to only require DBI, but it's true that we sould still suggest RPostgres, but then should we suggest other drivers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are tests for them, they ought to be added, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For our own education, I searched a bit for other examples and I found that dplyr
uses data.table
in tests, but it's nowhere in the DESCRIPTION
. I still think it's a good idea to suggest it, though. Maybe @krlmlr knows the reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We moved the data.table backend to dtplyr a while ago. The tests are gone now: https://github.com/tidyverse/dplyr/blob/master/tests/testthat/test-group-by.r.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks (I got confused in the commits)! Makes sense. So if I understand correctly we should Import
DBI
and Suggest
all drivers we explicitly test.
Cool -- now merged in master!! I had to install DBI from github to get the thing going, in the end, and remove one |
Yay! Writing the code was nothing compared to having the tests run successfully in another environment :) |
This now also requires DBI and RPostgres versions from gitnub to pass travis checking. @etiennebr could you please restore code coverage here with either adding tests or nocovs? And could you pls complete the NEWS entries? Also: would you be tempted to draft an r-spatial.org blog post on all the new capabilities? I'd be happy to help here. |
@edzer yes and yes! |
When is the next release? |
A month from now (2 months between releases is required by CRAN), or perhaps earlier if 3.5.0 burns the whole thing down before that (see here). |
May I humbly suggest that two minor but significant details be mentioned in the blog post?
|
I think 2. was caused by #718. Can that be a problem? We could, in principle, move it to last before writing. |
Thanks @dpprdan, indeed, |
* `sf::` is now a data source * `st_example` allows to load files (fuzzy match) * Use these sources for examples and vignettes related: r-spatial#558
* `sf::` is now a data source * `st_example` allows to load files (fuzzy match) * Use these sources for examples and vignettes related: r-spatial#558
This PR enables database (i.e. postgis) connection for
st_read
andst_write
. It also updates tests and documentation.I've moved
st_*_db()
to a newdeprecated.R
. I didn't make any effort to reconcile the arguments forst_read.character
andst_read.PostgreSQLConnection)
, which might be a little sloppy. I'm open to trying to reconcile arguments as much as possible since this is an opportunity, if you have any preference, please let me know.