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

bbox experiments #6

Closed
mdsumner opened this issue Jan 5, 2018 · 1 comment
Closed

bbox experiments #6

mdsumner opened this issue Jan 5, 2018 · 1 comment

Comments

@mdsumner
Copy link
Member

mdsumner commented Jan 5, 2018

From @mdsumner on February 7, 2017 4:28

I've been thinking similarly about the bounding box culls for st_intersect, is it worth having "feature-level bbox" as a fully fledged POLYGON sf used to pre-filter the combinations tested in full.

There is also scope for more general decomposition of objects for specialized optimizations (convex parts, triangles). I was working on benchmarks for this, which led to the motivation for #209. I think intersection is not improved by decomposition, but intersection-overlay definitely is.


library(sf)
example(st_read)
library(tibble)
npts <- 1e5
library(dplyr)
library(tibble)
pts <- st_as_sf(setNames(as_tibble(geosphere::randomCoordinates(npts)), c("x", "y")) %>% mutate(a = row_number()), 
                coords = c("x", "y"), crs = st_crs(nc))
#plot(st_geometry(pts), pch = ".")

tbox <- function(x) {
  as_tibble(do.call(rbind, lapply(st_geometry(x), st_bbox))) 
}

tbox(nc)

get_points <- function(x) {
  setNames(as_tibble(do.call(rbind, lapply(st_geometry(x), function(x) unclass(x)))), c("x_", "y_"))
}

prefilter_bbox <- function(x, y) {
  y_bb <- tbox(y) %>% mutate(y_id = row_number())
  
  xpoints <- get_points(x) ##sc::sc_coord(x)
  xpoints <- mutate(xpoints, x_id = as.character(row_number()))
  y_bb <- bind_rows(replicate(nrow(xpoints), y_bb, simplify = FALSE), .id = "x_id") 
  tab <- inner_join(xpoints, y_bb)
  tab %>% filter(x_ >= xmin, x_ <= xmax, y_ >= ymin, y_ <= ymax ) %>% 
    select(x_id, y_id) %>% mutate(x_id = as.integer(x_id))
}
tofilter <- prefilter_bbox(pts, nc)
st_intersects(pts[tofilter$x_id, ], nc[tofilter$y_id, ])

Copied from original issue: r-spatial/sfdct#10

@mdsumner
Copy link
Member Author

see extents branch in spex https://github.com/mdsumner/spex/tree/extents

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

1 participant