Skip to content
Prev 16787 / 29559 Next

readOGR and PostGIS: "Error getting field 0"

Dear Roger,

Thanks a lot for your very fast answer! Here are the requested outputs:

Le 30/11/2012 10:39, Roger Bivand a ?crit :
$ ogrinfo "PG:dbname=florida host=localhost user=pguser" 
gis.world_ctrdcoast -summary
INFO: Open of `PG:dbname=florida host=localhost user=pguser'
       using driver `PostgreSQL' successful.

Layer name: gis.world_ctrdcoast
Geometry: Unknown (any)
Feature Count: 35398
Extent: (-179.916500, -59.083000) - (179.968500, 83.535000)
Layer SRS WKT:
(unknown)
FID Column = gid
Geometry Column = geom

I noticed there was a call to ogrInfo in the readOGR function. After this 
line, here is what's reported:

Browse[2]> ogr_info
Source: "PG:dbname=florida host=localhost user=pguser", layer: 
"gis.world_ctrdcoast"
Driver: PostgreSQL number of rows 35398
Feature type: wkbPoint with 2 dimensions
NA
Number of fields: 0
[1] name     type     length   typeName
<0 rows> (or 0-length row.names)
As a matter of fact, it does! If I add a serial field named 'bla', it works 
perfectly:

 > ctrdcoast <- readOGR("PG:dbname=florida host=localhost user=pguser", 
layer = "gis.world_ctrdcoast")
OGR data source with driver: PostgreSQL
Source: "PG:dbname=florida host=localhost user=pguser", layer: 
"gis.world_ctrdcoast"
with 35398 features and 1 fields
Feature type: wkbPoint with 2 dimensions

 > summary(ctrdcoast)
Object of class SpatialPointsDataFrame
Coordinates:
                 min      max
coords.x1 -179.9165 179.9685
coords.x2  -59.0830  83.5350
Is projected: NA
proj4string : [NA]
Number of points: 35398
Data attributes:
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
       1    8850   17700   17700   26550   35400

 > head(ctrdcoast at data)
   bla
1   1
2   2
3   3
4   4
5   5
6   6
It looks like a correct guess, given the output of ogrinfo. In addition, 
the exported shapefile from ogr2ogr present one field (named FID), while if 
I load the table into QGIS, I can see the field with its correct name 
(gid). Admittedly, while I'm pretty sure that QGIS uses GDAL to load 
tables, I have no idea how it deals with it.
Browse[2]> ogr_info$nitems
[1] 0
Browse[2]> iflds
[1]  0 -1
Browse[2]> fldnms
character(0)
Does that mean that we necessarily need a field in the table (in addition 
to geom + gid)? It's not hard to 'fix' by adding a nonsensical field, but I 
find it quite counterintuitive.

Thanks again for the help!
Mathieu.