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

How to sp with sf #210

Closed
edzer opened this issue Feb 7, 2017 · 10 comments
Closed

How to sp with sf #210

edzer opened this issue Feb 7, 2017 · 10 comments

Comments

@edzer
Copy link
Member

edzer commented Feb 7, 2017

It would be nice to have a list of sp, rgeos and rgdal commands with the corresponding command in sf, e.g.

  • sp::disaggregate -> sf::st_cast

I started this in the sf wiki

@edzer edzer changed the title How to do sp with sf How to sp with sf Feb 7, 2017
@tim-salabim
Copy link
Member

This is great! Will be very helpful to update workflows. Thanks for these references!

@edzer
Copy link
Member Author

edzer commented May 18, 2017

See also here, calling for improvement.

@Robinlovelace
Copy link
Contributor

Robinlovelace commented Oct 5, 2017

Here's a challenge (as discussed with @Nowosad, example from vignette("over")) - how to do this with sf?

library(sp)
g = SpatialGrid(GridTopology(c(0, 0), c(1, 1), c(3, 3)))
p = as(g, "SpatialPolygons")
plot(p)

over(p[5], p, returnList = TRUE, minDimension = 0)
#> $g5
#> [1] 5 2 4 6 8 1 3 7 9
over(p[5], p, returnList = TRUE, minDimension = 1)
#> $g5
#> [1] 5 2 4 6 8
over(p[5], p, returnList = TRUE, minDimension = 2)
#> $g5
#> [1] 5

@Robinlovelace
Copy link
Contributor

If I find out the answer I'll happily document it.

@edzer
Copy link
Member Author

edzer commented Oct 6, 2017

see #234 .

@Robinlovelace
Copy link
Contributor

Thanks for the link - hadn't read that and really useful things in there that can solve that specific use case, as commented in that issue.

However, I don't think that thread addresses the question of how to "sp with sf" in that it does not provide a general replacement for the minDimension argument. People with perseverance and time could read-up on DE-9IM and find the right incantation but I think most people would find a minDimension argument, or alternative to it more user friendly, especially if they've already used minDimension in sp like me.

@edzer
Copy link
Member Author

edzer commented Oct 27, 2017

Here's a function that you could use in an lapply to get the same result as sp::over with returnList=TRUE:

s = st_as_sf(p)
> x = st_relate(s[5,], s)
> which_min_dim = function(x, n) {
	switch(as.character(n),
		"2" = which(substr(x, 1, 1) == "2"),
		"1" = which(substr(x, 5, 5) == "1"),
		"0" = which(substr(x, 5, 5) %in% c("0", "1")),
		stop("wrong value for n")
	)
}
> which_min_dim(x, 2)
[1] 5
> which_min_dim(x, 1)
[1] 2 4 5 6 8
> which_min_dim(x, 0)
[1] 1 2 3 4 5 6 7 8 9

@Robinlovelace
Copy link
Contributor

Robinlovelace commented Oct 27, 2017

Wow that's impressive! I've been thinking about this and was thinking of a helper function like st_over() that contain an interface for an equivalent of minDim. which_min_dim() should make that easy to do. I think that function will be useful if incorporated somewhere/somehow.

@edzer edzer closed this as completed Oct 29, 2017
@rsbivand
Copy link
Member

rsbivand commented May 4, 2020 via email

@katossky
Copy link

katossky commented May 4, 2020

I kind-of realised that in the following minutes and suppressed the comment. (I am not sure I agree that it has nothing to do with sf though, but let's not discuss that here.) Thanks for taking the time to answer.

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

5 participants