r-spatial/sf

Generic for `st_drop_geometry()`

henningte opened this issue · 1 comments

I wanted to ask if you think it would make sense to implement st_drop_geometry() as a generic function.

A potential use case where this would be useful is when someone wants to create a subclass to the sf class, as e.g. in the sftime package.

The problem with the current implementation for st_drop_geometry() is that it will drop the geometry column and class attribute, but not the subclass attribute. For example, for sftime objects:

library(sf)
#> Warning: package 'sf' was built under R version 4.0.5
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(sftime)

# example sfc object
x_sfc <- 
  sf::st_sfc(
    sf::st_point(1:2), 
    sf::st_point(c(1,3)), 
    sf::st_point(2:3), 
    sf::st_point(c(2,1))
  )

# create an sftime object directly from x_sfc
x_sftime1 <- sftime::st_sftime(a = 1:4, x_sfc, time = Sys.time()- 0:3 * 3600 * 24)

# trying to drop the geometry column returns an error (when printing):
st_drop_geometry(x_sftime1)
#> Error in st_agr.default(x): all(is.na(x)) is not TRUE

# ... because only the sf class is removed, but not the sftime class:
class(st_drop_geometry(x_sftime1))
#> [1] "sftime"     "data.frame"

Created on 2022-03-04 by the reprex package (v2.0.1)

It would be easier to handle this with a method than creating a function with exactly the same name.

edzer commented

Thanks, good idea!