Dear List,

I was hoping someone could give me some pointers please for trying to read
postGIS polygon geometries into R spatialPolygons.

I unfortunately am not sure how to give a toy example with a postGIS
database, but what I want to do is:

>From R, I want to query a postGIS database in order to access one of many
polygons in a table in the database based on the value of its ID.

For example, this SQL statement:

select geom from sa1kgrid where id=10

This works fine when I access the database through phpPGAdmin and enter it
directly as a SQL statement.

I also think I got it to work in R last night, but I can't seem to repeat
it, using this:

library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname = "mydb", user = "me", password = "mypw")
sql <- "SELECT geom from sa1kgrid where id=9"
kmlGeom <- dbGetQuery(con, sql)
(But now I am getting this error:
In postgresqlExecStatement(conn, statement, ...) :
  RS-DBI driver warning: (unrecognized PostgreSQL field type geometry
(id:16504) in column 0))

I have tried the readOGR approach, but I am not getting that right and am
not sure how to even get the entire table in, let alone one record from
it.

library(rgdal)
mydf <- readOGR("PG:dbname='mydf' user='me' password='mypw'",
layer="sa1kgrid")
(I have also tried various permutations of the dbname, quotes, etc., but
all get me this:

Error in ogrInfo(dsn = dsn, layer = layer, input_field_name_encoding =
input_field_name_encoding)

ogrDrivers() gives me this:

> ogrDrivers()
             name write
1          AVCBin FALSE
2          AVCE00 FALSE
3             BNA  TRUE
4             CSV  TRUE
5             DGN  TRUE
6            DODS FALSE
7             DXF  TRUE
8  ESRI Shapefile  TRUE
9      Geoconcept  TRUE
10        GeoJSON  TRUE
11         GeoRSS  TRUE
12            GML  TRUE
13            GMT  TRUE
14  GPSTrackMaker  TRUE
15            GPX  TRUE
16     Interlis 1  TRUE
17     Interlis 2  TRUE
18            KML  TRUE
19   MapInfo File  TRUE
20         Memory  TRUE
21          MySQL  TRUE
22           ODBC  TRUE
23           OGDI FALSE
24         PCIDSK FALSE
25           PGeo FALSE
26     PostgreSQL  TRUE
27            REC FALSE
28            S57  TRUE
29           SDTS FALSE
30         SQLite  TRUE
31          TIGER  TRUE
32        UK .NTF FALSE
33            VFK FALSE
34            VRT FALSE
35         XPlane FALSE

Here's my sessionInfo()

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=C                 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=C                 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] rgdal_0.7-18      sp_0.9-99         RPostgreSQL_0.3-2 DBI_0.2-5

loaded via a namespace (and not attached):
[1] grid_2.15.1    lattice_0.20-6

Oh, one other thing that seems to be possibly relevant, when I try with
straight up ogr2ogr to access the postGIS database, I get this result:

This is simply from doing this:

> ogr2ogr
ogr2ogr: error while loading shared libraries: libjpeg.so.8: cannot open
shared object file: No such file or directory

But it is the same error when I try to run a full command to access my
database.  The version of gdal is 1.7.3 installed as an rpm from the EPEL
repository by our administrators.

Any pointers will be greatly appreciated, either how to get this read in
using rgdal, or using rpostGresql and a transformation of the geometry
values into something that I can then turn translate into a sp object.

Thanks, Lyndon

	[[alternative HTML version deleted]]

