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 do an intersection as in st_intersection? #98

Open
vronizor opened this issue Dec 5, 2024 · 1 comment
Open

How to do an intersection as in st_intersection? #98

vronizor opened this issue Dec 5, 2024 · 1 comment

Comments

@vronizor
Copy link

vronizor commented Dec 5, 2024

Hi,

Many thanks for the great package. I was wondering if geometry operations were possible in geos. In particular, I wanted to do an intersection of two collection of features of different length and get a result like in st_intersection. I've been looking around the reference but haven't found a function that does the same. Am I not looking properly or this something not support by geos?

Thanks

@paleolimbot
Copy link
Owner

You didn't miss anything, it's not obvious! The geos package doesn't do data frame things, but it does provide you the tools to implement it (and maybe it should 🤷 ?).

Here's an example...the totally non-obvious function you're looking for is geos_inner_join_keys().

library(geos)

nc <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))
nc <- tibble::tibble(name = nc$NAME, geom_nc = as_geos_geometry(nc$geometry))

line <- as_geos_geometry("LINESTRING (-85 36, -75 36)")
wk::wk_crs(line) <- wk::wk_crs(nc$geom_nc)
line_df <- tibble::tibble(x = "foofy", geom_line = line)

keys <- geos_inner_join_keys(line_df$geom_line, nc$geom_nc, predicate = "intersects")

intersected <- vctrs::vec_cbind(
  line_df[keys$x, , drop = FALSE],
  nc[keys$y, , drop = FALSE]
)
intersected$geom <- geos_intersection(intersected$geom_line, intersected$geom_nc)
intersected$geom_line <- NULL
intersected$geom_nc <- NULL

plot(nc$geom_nc)
plot(line_df$geom_line, col = "purple", add = T)
plot(intersected$geom, col = "red", add = T)

Created on 2024-12-05 with reprex v2.1.1

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