[R-sig-Geo] Error reading spatialite table with sf::st_read

Barry Rowlingson b@row||ng@on @end|ng |rom gm@||@com
Thu Mar 3 19:19:53 CET 2022

Looks like that error might be generated about here:

where the code does:

try(sfc <- st_as_sfc(tbl[[i]]), silent = TRUE)
if (!inherits(sfc, "try-error")) {
tbl[[i]] = sfc
success = TRUE

If the first "try" fails then `sfc` doesn't get created and so the error
happens on `if(!inherits(sfc,....)`.

The code is looping over columns trying to find a valid geometry column,
but not trapping this error properly. A bug, I think. Feel free to report
as an issue.

However I don't think that gets us out of the woods - that code only talks
about PostGIS databases, but it gets called for any object of class
DBIObject, which your SQLite connection object is:

 > inherits(con,"DBIObject")
 [1] TRUE

Perhaps that method should be something like st_read.PGConnection if it
only works on PostGIS databases? Experimenting with bits of that function
don't get me the geometry from my test spatialite DB anyway, and I think
its due to it being PostGIS-specific. I kept getting empty geometries.

But why not read the Spatialite directly without going through DBI?  Here's
a test I made of some points:

> d = st_read("/tmp/olinda1.sqlite","pts")
Reading layer `pts' from data source `/tmp/olinda1.sqlite' using driver
Simple feature collection with 1000 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 234250 ymin: -198750 xmax: 909750 ymax: 502750
CRS:           unknown

THe CRS is printed as unknown but the WKT is there. Maybe this is a funny
CRS I was using for testing or something:

> st_crs(d)
Coordinate Reference System:
  User input: unknown

and you can add SQL queries to `st_read` if that;s part of your motivation
for using a spatial file database:

> d = st_read("/tmp/olinda1_no.sqlite", query="select * from pts where
Reading query `select * from pts where pampa_srtm>300' from data source
`/tmp/olinda1_no.sqlite' using driver `SQLite'
Simple feature collection with 189 features and 1 field


On Thu, Mar 3, 2022 at 4:48 PM DUTRIEUX Loic <Loic.DUTRIEUX using ec.europa.eu>

> Hi everyone,
> When trying to read data contained in a sqlite table with the full
> spatialite options (see what FORMAT=SPATIALITE implies here -->
> https://gdal.org/drivers/vector/sqlite.html), I get a
> wkbType: 30078980 Error in inherits(sfc, "try-error") : object 'sfc' not
> found.
> See the example below:
> # In bash
> ogr2ogr -F SQLITE -dsco SPATIALITE=YES /tmp/olinda1.sqlite
> ~/R/x86_64-pc-linux-gnu-library/4.0/sf/shape/olinda1.shp
> # In R
> library(sf)
> library(DBI)
> con <- dbConnect(RSQLite::SQLite(), '/tmp/olinda1.sqlite')
> st_read(con, query='select * from olinda1;')
> If I create the database without the SPATIALITE=YES creation option
> (geometries encoded as WKB), reading with sf partly works (no CRS) but then
> I lose the spatialite functionalities. Any suggestion?
> Kind regards,
> Loïc
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

	[[alternative HTML version deleted]]

More information about the R-sig-Geo mailing list