Skip to content
Prev 28860 / 29559 Next

Error reading spatialite table with sf::st_read

Loic,

I agree with Barry that there is a (logic) bug there, leading to a 
confusing error message. The problem seems the DBI driver failing to 
interpret a binary column ("blob") as wkb, which is needed when taking 
this path:


 > tb = dbReadTable(con, "olinda1")
 > head(tb)
   ogc_fid    id      cd_geocodi   tipo   cd_geocodb    nm_bair v014 
GEOMETRY
1       1 28801 260960005000001 URBANO 260960005020 Ouro Preto 1119 
blob[484 B]
2       2 28802 260960005000002 URBANO 260960005020 Ouro Preto 1267 
blob[532 B]
3       3 28803 260960005000003 URBANO 260960005020 Ouro Preto  557 
blob[276 B]
4       4 28804 260960005000004 URBANO 260960005020 Ouro Preto  709 
blob[468 B]
5       5 28805 260960005000005 URBANO 260960005020 Ouro Preto 1045 
blob[452 B]
6       6 28806 260960005000006 URBANO 260960005020 Ouro Preto  727 
blob[388 B]
 > st_as_sfc(tb$GEOMETRY)
wkbType: 30078980
Error in CPL_read_wkb(x, EWKB, spatialite) :
   unsupported wkbType dim in switch

so there seems to be something special with spatialite's WKB, which is 
not recognized by sf's WKB reader. Reading ?st_as_sfc:

 > st_as_sfc(tb$GEOMETRY, spatialite = TRUE)
Geometry set for 470 features
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -34.91692 ymin: -8.044467 xmax: -34.82779 ymax: 
-7.954672
CRS:           NA
First 5 geometries:
POLYGON ((-34.86406 -7.992488, -34.86397 -7.992...
POLYGON ((-34.86129 -7.989947, -34.86087 -7.989...
POLYGON ((-34.85856 -7.992407, -34.85899 -7.992...
POLYGON ((-34.85752 -7.994827, -34.85722 -7.994...
POLYGON ((-34.8582 -7.997543, -34.858 -7.996937...
Warning message:
In CPL_crs_from_input(x) :
   GDAL Error 1: PROJ: proj_create_from_database: crs not found

This can no doubt be made more automatic (e.g. by adding a spatialite 
argument to st_read.DBIObject), but the the question remains why, when

x = st_read('/tmp/olinda1.sqlite')

works OK, that couldn't be used instead?

Many regards,
On 03/03/2022 19:19, Barry Rowlingson wrote: