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

Assigning the overlap between polygons to the nearest polygon does not really remove the overlap #1936

Closed
fgoerlich opened this issue Apr 27, 2022 · 1 comment

Comments

@fgoerlich
Copy link

Taking the example from this stackexchange question and his st_no_overlap function:

st_no_overlap <- function(polygons) {

  centroids <- st_centroid(polygons)

  # Voronoi tesselation
  voronoi <-
    centroids %>%
    st_geometry() %>%
    st_union() %>%
    st_voronoi() %>%
    st_collection_extract()

  # Put them back in their original order
  voronoi <- voronoi[unlist(st_intersects(centroids, voronoi))]

  # Keep the attributes
  result <- centroids

  # Intersect voronoi zones with polygons
  st_geometry(result) <-
    mapply(function(x, y) st_intersection(x, y),
           st_geometry(polygons),
           voronoi,
           SIMPLIFY = FALSE) %>%
    st_sfc(crs = st_crs(polygons))

  result
}

I run his example,

pol <- st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0))))
b   <- st_sfc(pol, pol + c(.8, .2), pol + c(.2, .8))
cl = sf.colors(3, categorical = TRUE)

plot(st_no_overlap(st_sf(geometry = b)), col = cl)

and get the same nice picture without overlaping.

image

but this is only in the picture. If I check for overlapping with st_overlaps, I get that the polygons indeed overlap!

st_overlaps(st_no_overlap(st_sf(geometry = b)))
Sparse geometry binary predicate list of length 3, where the predicate was `overlaps'
 1: 2, 3
 2: 1
 3: 1

The same happens with the st_buffer_without_overlap function mentioned in #824, so I am not sure if this has to do with the geometrical operations done in the functions (voronoi, intersections,...) o with the slivers mentioned in #547.

Is there a general way to prevent this behaviour or at least to solve it? Thanks!

@edzer
Copy link
Member

edzer commented Apr 27, 2022

A consequence of not setting a precision:

> st_overlaps(st_set_precision(st_no_overlap(st_sf(geometry = b)),1e8))
Sparse geometry binary predicate list of length 3, where the predicate
was `overlaps'
 1: (empty)
 2: (empty)
 3: (empty)

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