rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
533 stars 87 forks source link

add `pointOnSurface` function? #588

Closed AMBarbosa closed 2 years ago

AMBarbosa commented 2 years ago

A function analogous to rgeos::gPointOnSurface or sf::st_point_on_surface would also be useful in 'terra', as well as mentioning it in the "See also" section of ?centroids. Cheers!

rhijmans commented 2 years ago

I have added that functionality via an argument to centroids. Do you think that is reasonable?

library(terra)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
x <- centroids(v)
y <- centroids(v, inside=TRUE)

plot(v)
points(x)
points(y, col="red")
AMBarbosa commented 2 years ago

That's awesome, thanks! However, with inside=TRUE for an input lines map, we get "Error: [] input must be polygons". If it's not too much trouble to implement this for lines too, that would be great. Cheers!

v <- as.lines(crop(v, ext(6, 6.2, 49, 49.6)))
plot(v)
plot(centroids(v), add=TRUE)
plot(centroids(v, inside=TRUE), add=TRUE)
rhijmans commented 2 years ago

Did not think of lines. Fixed now.