Skip to content

Fix typo breaking st_as_sfc.pq_geometry() on every input - #2626

Merged
edzer merged 1 commit into
r-spatial:mainfrom
guslipkin:fix/pq-geometry-spatialite-typo
Sep 17, 2026
Merged

edzer merged 1 commit into
r-spatial:mainfrom
guslipkin:fix/pq-geometry-spatialite-typo

Conversation

@guslipkin

@guslipkin guslipkin commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #2625.

Summary

st_as_sfc.pq_geometry() (R/db.R) forwards to st_as_sfc.WKB() using the argument name spatiallite, which doesn't match st_as_sfc.WKB()'s actual spatialite parameter. The unmatched named argument falls through st_as_sfc.WKB()'s own ... and rides along into its final st_sfc(ret, ...) call, where sfc_unique_sfg_dims_and_types() trips over it and fails with a misleading "object(s) should be of class 'sfg'" — unrelated to the actual geometry being converted.

This means st_as_sfc() has been broken for every pq_geometry input — i.e. every PostGIS geometry column read via RPostgres::dbGetQuery(), the exact workflow #1195 introduced this method for — since that method was added. The typo was already present in the fix suggested in that issue and appears to have been merged verbatim.

Why this was dormant until 1.1-3

The typo itself has been there since #1195 (2019), but it was harmless for years: in 1.1-2, st_as_sfc.WKB()'s final calls did not forward ... into st_sfc() at all —

st_sfc(ret, crs = crs)
...
st_sfc(ret) # leave attr srid in place: PostGIS srid that is not an EPSG code

— so any stray/unmatched argument (including the mistyped spatiallite) was silently dropped and never reached st_sfc().

1.1-3 changed this in R/wkb.R to forward ... through:

st_sfc(ret, crs = crs, ...)
...
st_sfc(ret, ...) # leave attr srid in place: PostGIS srid that is not an EPSG code

That's commit a264e6f ("1.1-3 CRAN submission"), landed alongside #2622's precision= round-trip work for st_coordinates() — almost certainly to let callers pass st_sfc()-level args like precision through st_as_sfc(). Reasonable on its own, but it's what first gave the six-year-old typo an actual path to break something: now the stray spatiallite = FALSE rides along in ... straight into st_sfc()'s sfg-class validation.

Confirmed directly against the CRAN source tarballs for both versions (R/wkb.R diff, 1.1-2 → 1.1-3 is exactly the two lines above) and reproduced the crash appearing/disappearing across a clean reinstall of each release.

Fix

One-character rename, spatiallitespatialite, so the argument actually reaches st_as_sfc.WKB()'s matching formal instead of leaking through ....

Test plan

  • Added a DB-free regression test in tests/testthat/test-wkb.R reusing the existing WKB hex fixture from that file, just re-classed as "pq_geometry".
  • Verified against the actual compiled package (devtools::load_all() + testthat::test_file()):
    • With the typo (pre-fix): the new test fails with the exact reported error, full backtrace through st_as_sfc.pq_geometryst_as_sfc.WKBst_sfcsfc_unique_sfg_dims_and_types.
    • With the fix: test-wkb.R passes in full.
  • Also verified against clean CRAN installs: reproduces on a from-source 1.1-3 install, does not reproduce on a from-source 1.1-2 install — consistent with the R/wkb.R diff above.

Related

st_as_sfc.pq_geometry() forwards to st_as_sfc.WKB() using the argument
name "spatiallite", which doesn't match st_as_sfc.WKB()'s actual
"spatialite" parameter. The unmatched named argument falls through
st_as_sfc.WKB()'s own `...` and rides along into its final
st_sfc(ret, ...) call, where sfc_unique_sfg_dims_and_types() trips over
it and fails with a misleading "object(s) should be of class 'sfg'" --
unrelated to the actual geometry being converted.

This means st_as_sfc() has been broken for every pq_geometry input
(i.e. every PostGIS geometry column read via RPostgres::dbGetQuery(),
the exact workflow r-spatial#1195 introduced this method for) since that method
was added -- the typo was already present in the fix suggested there.

Fixes r-spatial#2625.
@edzer
edzer merged commit 51fc6f6 into r-spatial:main Sep 17, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

st_as_sfc.pq_geometry() crashes with "object(s) should be of class 'sfg'" due to spatiallite/spatialite typo

2 participants