Skip to content

$classes attribute can be outdated #2567

@DavisVaughan

Description

@DavisVaughan

I noticed that in some cases the classes attribute that is on a sfc_GEOMETRY column can be outdated in some cases. In particular, when [<- is used and the sfc_GEOMETRY becomes a uniform sfc_POINT due to the assignment, the classes attribute isn't updated (ideally in this case I think it should be removed? Because the vector is a uniform geometry now?)

library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE

# Mixed geometries
x <- st_sfc(st_point(), st_linestring())
x
#> Geometry set for 2 features  (with 2 geometries empty)
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> CRS:           NA
#> POINT EMPTY
#> LINESTRING EMPTY

# Abstract `sfc_GEOMETRY`
class(x)
#> [1] "sfc_GEOMETRY" "sfc"

# Note that `classes` tracks this
attributes(x)$classes
#> [1] "POINT"      "LINESTRING"

# Make `x` a uniform vector of sfc_POINTs
x[2] <- st_point()

# Looks like the overall geometry type updated
x
#> Geometry set for 2 features  (with 2 geometries empty)
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> CRS:           NA
#> POINT EMPTY
#> POINT EMPTY

# But `classes` hasn't been updated
attributes(x)$classes
#> [1] "POINT"      "LINESTRING"

# Ideally I think `classes` would be totally removed here,
# since `x` is now uniform and `classes` is not required.

Created on 2026-01-05 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions