[R-sig-Geo] readOGR and PostGIS: "Error getting field 0"

Roger Bivand Roger.Bivand at nhh.no
Fri Nov 30 16:39:35 CET 2012


On Fri, 30 Nov 2012, Mathieu Basille wrote:

> Dear R users,
>
> I have a working PostGIS database that I routinely access through R, using 
> readOGR or readGDAL. I have this point layer, however, that I can't read in 
> R:
>
>> 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 2 fields
> Error in readOGR("PG:dbname=florida host=localhost user=pguser", layer = 
> "gis.world_ctrdcoast") :
>  Error getting field 0
>
> The layer is basically a table with just 2 fields: geom (a point feature) and 
> gid (a serial primary key). I can export this layer without any problem using 
> ogr2ogr, and the layer looks fine to me (it displays fine in QGIS):
>
> ogr2ogr -f "ESRI Shapefile" ctrd.shp PG:"dbname=florida host=localhost 
> user=pguser" "gis.world_ctrdcoast"
>
> Any idea where this is going wrong? Thanks in advance for any suggestion.
> Mathieu.

The error message is from ogrReadColumn(), line 221 in src/ogrsource.cpp, 
for example here:

https://r-forge.r-project.org/scm/viewvc.php/pkg/src/ogrsource.cpp?view=markup&root=rgdal

What does ogrInfo() report? Does it make a difference if you add an extra 
field? I'm wondering whether in this case the GID is not being seen as a 
field as such, but the FID of the point. Possibly ogr2ogr steps round 
this. I don't have a PostGIS system running, so may need help debugging. 
If you debug(readOGR), what happens here:

         if (drop_unsupported_fields) {
              iflds <- as.integer((1:ogr_info$nitems)-1)
              iflds <- iflds[keep]
              fldnms <- ogr_info$iteminfo$name[keep]
              if (any(!keep)) warning(paste("Fields dropped:",
                  paste(ogr_info$iteminfo$name[!keep], collapse=" ")))
         } else {
              if (any(!keep)) stop(paste("Unsupported field type:",
                  paste(ogr_info$iteminfo$typeName[!keep], collapse=" ")))
              iflds <- as.integer((1:ogr_info$nitems)-1)
              fldnms <- ogr_info$iteminfo$name
         }

I think ogr_info$nitems is 1, so we get 0, but if ogr_info$nitems is 0, 
(0, -1).

Roger




>
>
>> sessionInfo()
> R version 2.15.1 (2012-06-22)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
> [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C LC_TIME=fr_FR.UTF-8
> [4] LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8 
> LC_MESSAGES=fr_FR.UTF-8
> [7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C 
> [10] LC_TELEPHONE=C             LC_MEASUREMENT=fr_FR.UTF-8 
> LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] raster_2.0-31 rgdal_0.7-22  sp_1.0-2
>
> loaded via a namespace (and not attached):
> [1] compiler_2.15.1 grid_2.15.1     lattice_0.20-6  tools_2.15.1
>
>
> # SELECT PostGIS_Full_Version();
>
>    postgis_full_version 
> -------------------------------------------------------------------------
> POSTGIS="2.1.0SVN r10597" GEOS="3.3.3-CAPI-1.7.4" PROJ="Rel. 4.7.1, 23 
> September 2009" GDAL="GDAL 2.0dev, released 2011/12/29" LIBXML="2.8.0" 
> LIBJSON="UNKNOWN" TOPOLOGY RASTER
>
>
>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list