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

[feature] function to get sf_column attribute #2435

Closed
JosiahParry opened this issue Sep 5, 2024 · 3 comments
Closed

[feature] function to get sf_column attribute #2435

JosiahParry opened this issue Sep 5, 2024 · 3 comments

Comments

@JosiahParry
Copy link
Contributor

As a package developer I often want to grab the geometry column from an sf object. However, it is not safe to assume that the column is called geometry. For this reason I grab the sf_column attribute. This is done via attr(x, "sf_column"). It would be great if there were a helper function to get this. For example:

sf_column <- function(x) {
  if (!inherits(x, "sf")) {
    stop("Expected an `sf` object")
  }
  attr(x, "sf_column")
}

x <- sf::st_sf(
  x = 1, foo = sf::st_sfc(sf::st_point(c(0, 0)))
)

sf_column(x)
#> [1] "foo"

sf_column(1L)
#> Error in sf_column(1L): Expected an `sf` object

sf_column(list())
#> Error in sf_column(list()): Expected an `sf` object
@edzer
Copy link
Member

edzer commented Sep 6, 2024

As a package developer I often want to grab the geometry column from an sf object.

For that, sf provides the generic st_geometry().

@JosiahParry
Copy link
Contributor Author

JosiahParry commented Sep 6, 2024

Perhaps I should edit the text, I often want to get the geometry column name. st_geometry() is indeed useful for accessing the underlying sfc. But often i need to know which column is the geometry column by name or position and the sf_column attribute helps with that .

A quick search shows that the top hit is in fact a function called sf_column() for the same purpose! There are a good handful of folks who have built something similar https://github.com/search?q=%22sf_column%22&type=code

@edzer
Copy link
Member

edzer commented Dec 8, 2024

There are a good handful of folks

I count three, and pretty different ones.

@edzer edzer closed this as completed Dec 8, 2024
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

No branches or pull requests

2 participants