-
Notifications
You must be signed in to change notification settings - Fork 79
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
Request to add PostGIS support #86
Comments
You are right about the speed, and I know there's a lot of options here - it's just that communicating remotely like this makes it hard to elucidate many things. I don't have functional DBs to summon, I should sort that out but it's just another thing. Please try, after this "res <- " line, in R res <- dbSendQuery(con, "SELECT * FROM buildings")
## please try
g <- wkb::readWKB(res$geom)
class(g) and report details. There are other options / details / points to note: rgdal::readOGR (via GDAL, with the right drivers built in) can read natively from Postgres or PostGIS, there are gdal.org pages about the details wkb::readWKB or rgeos::readWKT can re-compose Spatial geometry lists from geometry columns, but the author of postGIStools reported that readWKB was slower than the internals of the workflow used here: SESYNC-ci/postGIStools#3 |
Thanks for input. I believe you can't use a query with Regarding the wkb perhaps I'm missing something but I get the following: res <- dbSendQuery(con, "SELECT * FROM buildings")
# Warning: Unknown field type (16400) in column geom
dat <- dbFetch(res)
g <- wkb::readWKB(res$geom)
#Error in res$geom : $ operator not defined for this S4 class
g <- wkb::readWKB(dat$geom)
#Error in wkb::readWKB(dat$geom) : wkb must be a list In postGIStools: he uses a variation on: res <- dbSendQuery(con, "SELECT ST_AsText(geom) geom FROM buildings")
dat <- dbFetch(res)
final<-rgeos::readWKT(dat$geom)
# [1] "SpatialPolygons"
# attr(,"package")
# [1] "sp" |
It's not that there isn't a windows driver, it's that they (rgdal) don't want to bundle the additional OGR drivers in the CRAN package build. If you build the package against a different OGR then it can work, there are quite a few OGR/GDAL drivers in this boat. Yes I can confirm that rgdal does not support queries through ogr. Pull requests(Patches) to add query support to readOGR is an option (has been mentioned on r-sig-geo several times), if anyone wants to contribute it. My workaround is that I create Views, then use readOGR on the view... |
As discussed in the linked ticket, I think the main reason readWKB is slower than readWKT at the moment is the R vs. C implementation. On the PostgreSQL side, one additional complication is that neither ST_AsText nor ST_AsBinary import the projection information, so another term must be added to the query: ST_SRID (geom). |
For reference, here is what I use to get the proj4 SELECT proj4text FROM spatial_ref_sys WHERE srid = (SELECT ST_SRID(geom) FROM the_table); |
That's what I am using most of the time... |
For those coming across this now, we've recently released on CRAN the
We also found that the @zross , for large point tables (single point per record) in particular, simply extracting the X and Y coordinates in the SQL query (e.g., |
This is exciting, thanks for letting me know! |
Related: #114 |
Closing in favor of https://github.com/r-dbi/DBI/issues/203. |
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary. |
I'm following up on a tweet exchange with Hadley. It would be spectacular RPostgres offered PostGIS support (support for PostgreSQL tables that have a geom).
I can't give a perfectly reproducible example since it will depend on your versions of PostGIS etc, but here is an attempt
Create a spatial DB from the command line
createdb --host localhost --port 5433 --username postgres --template postgis_21_sample mypostgis
Create a new table, add a geometry column and fill with one polygon (use psql or pgAdmin)
In R show what happens with
RPostgres
You can read in the non-spatial fields just fine, but you get a warning and you get the raw geom data when your query includes the spatial variable (geom). Ideally, if you query against a spatial file it would return some kind of
sp
object like, in this case, aSpatialPolygonsDataFrame
.The
postGIStools
package offers some supportWith the postGIStools package you can read in PostGIS data
This is perfect except I found it to be slow on a medium size table. With 400k points, it took a pretty long time.
Speed
The image below shows that it takes almost 5 minutes to read in 400k points as a spatial file, but it can be read in faster if I write to shapefile first and then use
readOGR
. To me this suggests that there is likely a faster way to read this data directly into R.Additional discussion on this
I'm not sure what the speediest way to bring in spatial data from PostGIS but I think bringing in a table of just 400k points should take less than 5 minutes as it seems to with postGIStools. And, given the example above where I write to shapefile and read in in half the time, it's clear that there should be better alternatives. There is some discussion about this at the postGIStools GitHub site.
The text was updated successfully, but these errors were encountered: