Skip to content

Specifying Schema for PostGIS Layer with Simple Features

11 messages · Avipsa Roy, jds-linux, Edzer Pebesma +4 more

#
Hi All,

Getting going with Simple Features - awesome job Developers, and thanks for
your time!

Quick question - I'm working on bringing in PostGIS layers to work with in
R - I've got the db connection made using the RPostgreSQL connection and
have successfully imported data from the public schema. However, I'm not
figuring out how to bring in layers from other schema. Sorry if I'm just
missing something obvious.

I've tried a few different iterations, as detailed below, for table 'ipis'
in schema 'staging'

Try 1: specify table without naming schema
Error in postgresqlExecStatement(conn, statement, ...) :
  RS-DBI driver: (could not Retrieve the result : ERROR:  relation "ipis"
does not exist
LINE 1: select * from  ipis ;
                       ^
)
Error in .subset2(x, i, exact = exact) : subscript out of bounds


Try 2: specify schema with table as i would in postgres directly
Error in .subset2(x, i, exact = exact) : subscript out of bounds


Try 3: Specify schema as I would with RPostgreSQL operations (e.g.,
dbExistsTable)
Error in postgresqlExecStatement(conn, statement, ...) :
  RS-DBI driver: (could not Retrieve the result : ERROR:  relation
"staging" does not exist
LINE 1: select * from  staging ;
                       ^
)
Error in .subset2(x, i, exact = exact) : subscript out of bounds

I've verified R can see the table using:
[1] TRUE


Thanks in advance for any suggestions!
Best,
Mike T


PS - I was originally trying to use rgdal to read these layers in, but
found rgdal did not have the PostgreSQL/PostGIS driver with it on Windows -
if that's a simple fix too, I'm all ears.
#
Hi Michael,

You can check if the execute permission is granted to your user id on the schema you are trying to access?

Thanks & Regards,
Avipsa Roy

Michael Treglia wrote on 2016-12-09:

        
#
Dear Michael.

To add on Avipsa answer. You may use this query to grant your user 
(maybe not full secure)

GRANT ALL ON ALL TABLES IN SCHEMA public to MICHAEL with grant option;

To my knowledge Rpostgresql does not support postgis geometry. To 
retreive geometry in R, you should take a look at Rpostgis.

https://cran.r-project.org/web/packages/rpostgis/index.html

Regards.

Jean-Daniel
On 12/09/2016 03:34 PM, Avipsa Roy wrote:
#
On 09/12/16 21:02, Michael Treglia wrote:
thanks, this doesn't work indeed; the bug is fixed, but it may take a
while until this works on windows. A work-around for now may be:

test = st_read_db(conn, table = "ipis",
   query = "select * from 'staging.ipis'")
Switch to linux? Small step, these days.

  
    
#
Hi Avipsa,

Thanks for the quick response! The role of the username is Superuser, so I
should have all permissions, right? Or is there something else I will need
to check on?

Best,
Mike
On Fri, Dec 9, 2016 at 3:34 PM, Avipsa Roy <a_roy001 at uni-muenster.de> wrote:

            

  
  
#
Hi Michael,
Ideally if you are the superuser you should have access to all tables and schemas. Sometimes it happens that when you make an odbc connection from R you still need to explicitly grant access to non-public/user-specific tables. I would suggest to use the GRANT ALL ON <SCHEMA> TO <USER> , as Edzer mentioned in the previous email.
Thanks & Regards,
Avipsa Roy
Institute f?r Geoinformatics 
Westf?lische Wilhelms- Universit?t M?nster
#
Thanks so much, everybody.

Edzer - your primary fix works - tested on Windows and on an Ubuntu VM. (I
installed from GitHub on both - my Ubuntu VM was behind on things, so it
was a good excuse to upgrade gdal, etc. there :-)) Thanks for the quick fix!

just FYI, Your alternative suggestion threw an error though:
+    query = "select * from 'staging.ipis'")
Error in postgresqlExecStatement(conn, statement, ...) :
  RS-DBI driver: (could not Retrieve the result : ERROR:  syntax error at
or near "'staging.ipis'"
LINE 1: select * from 'staging.ipis'
                      ^
)
Error in CPL_hex_to_raw(sapply(x, skip0x, USE.NAMES = FALSE)) :
  not compatible with STRSXP




On Fri, Dec 9, 2016 at 4:04 PM, Edzer Pebesma <edzer.pebesma at uni-muenster.de
Definitely - I often use a Linux VM as mentioned above, but like to have
things running across envs when I can. (with sf working for me, given your
fix, I'm all set for now!)

Best,
mike

  
  
#
On Fri, Dec 9, 2016 at 6:06 PM, Michael Treglia <mtreglia at gmail.com> wrote:

            
A couple of years ago I wrote a function that can load PostGIS geometries
using RPostgreSQL instead of rgdal. It converts to WKT in SQL and then
converts the WKT to R spatial objects using the rgeos library.

Code below. Blogged here:
https://geospatial.commons.gc.cuny.edu/2014/01/14/load-postgis-geometries-in-r-without-rgdal/

```
library(RPostgreSQL)
library(rgeos)
library(sp)

# Load data from the PostGIS server
conn = dbConnect(
  dbDriver("PostgreSQL"), dbname=dbname, host=host, port=5432,
  user=user, password=password
  )

strSQL = "
  SELECT gid, ST_AsText(geom) AS wkt_geometry, attr1, attr2[, ...]
  FROM geo_layer"
dfTemp = dbGetQuery(conn, strSQL)
row.names(dfTemp) = dfTemp$gid

# Create spatial polygons
# To set the PROJ4 string, enter the EPSG SRID and uncomment the
# following two lines:
# EPSG = make_EPSG()
# p4s = EPSG[which(EPSG$code == SRID), "prj4"]
for (i in seq(nrow(dfTemp))) {
  if (i == 1) {
    spTemp = readWKT(dfTemp$wkt_geometry[i], dfTemp$gid[i])
    # If the PROJ4 string has been set, use the following instead
    # spTemp = readWKT(dfTemp$wkt_geometry[i], dfTemp$gid[i], p4s)
  }
  else {
    spTemp = rbind(
      spTemp, readWKT(dfTemp$wkt_geometry[i], dfTemp$gid[i])
      # If the PROJ4 string has been set, use the following instead
      # spTemp, readWKT(dfTemp$wkt_geometry[i], dfTemp$gid[i], p4s)
    )
  }
}

# Create SpatialPolygonsDataFrame, drop WKT field from attributes
spdfFinal = SpatialPolygonsDataFrame(spTemp, dfTemp[-2])
```
#
Oh, cool Lee! That's great to have around!

Thanks,
Mike

On Fri, Dec 9, 2016 at 7:47 PM, Lee Hachadoorian <
Lee.Hachadoorian+L at gmail.com> wrote:

            

  
  
#
Hi Michael,

As pointed out earlier in that thread by Jean-Daniel, if you're only
interested in loading PostGIS geometries in R, and not in Simple Features
themselves, you may want to give a look at rpostgis, which present a
standard and flexible solution (using rgeos and RPostgreSQL in the background):

https://cran.r-project.org/package=rpostgis

Mathieu.
On 12/10/2016 06:02 PM, Michael Treglia wrote:

  
    
1 day later
#
Thanks so much Matthieu! This looks like a great package!

Best,
Mike

On Sun, Dec 11, 2016 at 1:25 AM, Mathieu Basille <
basille.web at ase-research.org> wrote: