Cidree / rpostgis

rpostgis: R Interface to a 'PostGIS' Database
http://cidree.github.io/rpostgis/
77 stars 14 forks source link

Error when using a single colum apart from geometry to get spatial object back #8

Closed Rekyt closed 7 years ago

Rekyt commented 7 years ago

Hi, First thank you for building this package which is exactly what I was looking for as most database packages don't handle spatial objects.

However, when I want to extract a geometry with a single other column, there's problem because the returned data object is not a data.frame and I get this error:

 Error in validObject(.Object) : 
  invalid class “SpatialPointsDataFrame” object: invalid object for slot "data" in class "SpatialPointsDataFrame": got class "integer", should be or extend class "data.frame" 

I couldn't make a reproducible example but here is a conceptual one.

my_table

x (numeric) y (numeric) coord (ST_Point)
1 10.1 ST_POINT(x1, y1)
2 11.1 ST_POINT(x2, y2)
3 12.1 ST_POINT(x3, y3)

If i want to retrieve all coords it works with:

spatial_table = pgGetGeom(local_db, "my_table", "coord")
# or
spatial_table = pgGetGeom(local_db, "my_table", "coord", other.cols = c("x", "y"))

but with a single column it breaks

# Error above
spatial_table = pgGetGeom(local_db, "my_table", "coord", other.cols = c("x"))
# Error in validObject(.Object) : 
#   invalid class “SpatialPointsDataFrame” object: invalid object for slot "data" in class "SpatialPointsDataFrame": got class "integer", should be or extend class "data.frame" 

Is there a way to fix that ? (for the moment I query two columns and get rid of one)

dnbucklin commented 7 years ago

@Rekyt

Thanks for using the package and letting us know about this. It was indeed a bug for all one-column imports, and thankfully an easy fix (ebcdfbdc74096bbbf380aae9642dbfced7beb47f)

It's now fixed in the master branch, and in the next CRAN version (to be released soon). Install with:

devtools::install_github("mablab/rpostgis")

Rekyt commented 7 years ago

Woah, that was fast, thanks ;) By the way, I was considering using the sfpackage because it implements an easy API for spatial objects and spatial database. You could maybe show how rpostgis is different from it.

dnbucklin commented 7 years ago

sf is indeed a really nice package, and the current functional difference for (e.g.) sf::st_read_db and rpostgis::pgGetGeom is that sf returns sf (new R vector standard) and rpostgis returns Spatial* (old R vector standard) objects. I haven't tested extensively but sf::st_read_db will also offer a performance benefit most notable in large read/write operations. So pgGetGeom/pgInsert are likely to adopt these methods in the future.

About how rpostgis is different, we that that was a good idea too, and we're now in the process of preparing a paper/vignette(s) that will get at where we think it is most useful, and how it fits in among spatial and database R packages, so stay tuned!