Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R CMD check with R_SF_ST_READ_USE_STREAM=true #2296

Open
rsbivand opened this issue Dec 18, 2023 · 4 comments · May be fixed by #2369
Open

R CMD check with R_SF_ST_READ_USE_STREAM=true #2296

rsbivand opened this issue Dec 18, 2023 · 4 comments · May be fixed by #2369

Comments

@rsbivand
Copy link
Member

Running CMD check for GDAL 3.8.2RC1, I set R_SF_ST_READ_USE_STREAM=true, and hit a problem in tests/empty.R, lines 91-92:
00check.log

> y = st_read("empty.gpkg", quiet = TRUE)
> all.equal(x, y)
Error in UseMethod("st_geometry") : 
  no applicable method for 'st_geometry' applied to an object of class "c('double', 'numeric')"
> str(x)
Classes 'sf' and 'data.frame':	2 obs. of  2 variables:
 $ a   : int  2 1
 $ geom:sfc_LINESTRING of length 2; first list element:  'XY' num[0 , 1:2] LINESTRING EMPTY
 - attr(*, "sf_column")= chr "geom"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA
  ..- attr(*, "names")= chr "a"
> str(y)
Classes 'sf' and 'data.frame':	2 obs. of  2 variables:
 $ a   : int  2 1
 $ geom:sfc_MULTILINESTRING of length 2; first list element:  list()
  ..- attr(*, "class")= chr [1:3] "XY" "MULTILINESTRING" "sfg"
 - attr(*, "sf_column")= chr "geom"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA
  ..- attr(*, "names")= chr "a"

I guess this is not new for GDAL 3.8.2, will check when this train gets to Bergen.

@rsbivand
Copy link
Member Author

Same error with released GDAL 3.8.1:
00check.log

@edzer
Copy link
Member

edzer commented Feb 10, 2024

Confirmed; the issue is that empty.gpkg (zipped: empty.zip) is written with LINESTRING geometries but read with MULTILINESTRING geometries when using the stream interface. @paleolimbot can you see whether this is caused by sf code or by GDAL code?

@paleolimbot
Copy link
Contributor

I will dig deeper after the kids are in bed, but I think the culprit might be the promote multi option when converting from wkb to sfc. That's implemented in wk at the moment (because sf's built in wkb parser doesn't do promote to multi if I remember correctly)

@paleolimbot
Copy link
Contributor

It seems like the two interpretations of "promote to multi" are slightly different: wk seems to do this unconditionally, whereas sf seems to do this only when required. I can't find a PR where this changed...I forget how deeply I poked this when writing implementing this in wk::sfc_writer() but the sf version is definitely the desired behaviour.

library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.3, PROJ 9.3.1; sf_use_s2() is TRUE

tf <- tempfile(fileext = ".gpkg")
data.frame(
  a = 2:1,
  geom = sf::st_as_sfc(c("LINESTRING EMPTY", "LINESTRING (1 2, 3 4)"))
) |>
  sf::write_sf(tf)
#> writing: substituting ENGCRS["Undefined Cartesian SRS with unknown unit"] for missing CRS

sf::read_sf(tf, use_stream = FALSE, promote_to_multi = TRUE)
#> Simple feature collection with 2 features and 1 field (with 1 geometry empty)
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> Projected CRS: Undefined Cartesian SRS with unknown unit
#> # A tibble: 2 x 2
#>       a         geom
#>   <int> <LINESTRING>
#> 1     2        EMPTY
#> 2     1   (1 2, 3 4)
sf::read_sf(tf, use_stream = FALSE, promote_to_multi = FALSE)
#> Simple feature collection with 2 features and 1 field (with 1 geometry empty)
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> Projected CRS: Undefined Cartesian SRS with unknown unit
#> # A tibble: 2 x 2
#>       a         geom
#>   <int> <LINESTRING>
#> 1     2        EMPTY
#> 2     1   (1 2, 3 4)

sf::read_sf(tf, use_stream = TRUE, promote_to_multi = TRUE)
#> Simple feature collection with 2 features and 1 field (with 1 geometry empty)
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> Projected CRS: Undefined Cartesian SRS with unknown unit
#> # A tibble: 2 x 2
#>       a              geom
#>   <int> <MULTILINESTRING>
#> 1     2             EMPTY
#> 2     1      ((1 2, 3 4))
sf::read_sf(tf, use_stream = TRUE, promote_to_multi = FALSE)
#> Simple feature collection with 2 features and 1 field (with 1 geometry empty)
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> Projected CRS: Undefined Cartesian SRS with unknown unit
#> # A tibble: 2 x 2
#>       a         geom
#>   <int> <LINESTRING>
#> 1     2        EMPTY
#> 2     1   (1 2, 3 4)

Created on 2024-02-12 with reprex v2.1.0

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 a pull request may close this issue.

3 participants