[Superseded]

With sf package version >= 1.0-6 st_set_geometry and st_geometry<- have become handy tools for renaming the active geometry columns of sf objects. st_set_geometry now offers the same functionality as st_rename_geometry (s. examples), and it is faster, which is advantageous when working on large lists of sf objects.

st_rename_geometry(obj, geometry_name)

Arguments

obj

object of class sf

geometry_name

a single character string renaming the active list-column with simple feature geometries

Value

a sf object with renamed geometry list-column.

Details

st_rename_geometry() is inspired by code found on gis.stackexchange and particularly useful to homogenize sf objects with differently named geometry columns or lists of such before binding them to a single sf object by

  • rbind (bind)

  • do.call(rbind, <list_of_sf>)

  • sf::st_as_sf(data.table::rbindlist(<list_of_sf>))

Examples

# current active list-column with simple feature geometries:
attr(poly_1, "sf_column")
#> [1] "geometry"
st_rename_geometry(poly_1, "renamed_geometry")
#> Simple feature collection with 4 features and 3 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 2600000 ymin: 1200000 xmax: 2601000 ymax: 1201000
#> Projected CRS: CH1903+ / LV95
#>    A B    C               renamed_geometry
#> 1 Pq 1 1983 POLYGON ((2600000 1200000, ...
#> 2 Qr 2 1984 POLYGON ((2600500 1200000, ...
#> 3 Rs 3 1985 POLYGON ((2600000 1200500, ...
#> 4 St 4 1986 POLYGON ((2600500 1200500, ...

library(sf)
if(packageVersion("sf") >= '1.0.6'){
  all.equal(
    st_rename_geometry(poly_1, "renamed_geometry"),
    st_set_geometry(poly_1, "renamed_geometry")
  )
}
#> [1] TRUE