3

I have a set of polygons and I want to find the bbox for each polygon using sf. With st_bbox I obtain the bbox for the whole set of features but I want to obtain bbox for each feature.

I try to use group_by() but the output is always the same.

library(sf)

g <-  st_sfc(st_point(1:2), st_point(3:4)) %>% st_buffer(dist = 1)
s <-  st_sf(a=3:4, g)
s %>% group_by(a) %>% st_bbox()%>% st_as_sfc
3
g0 <- st_sfc(st_point(1:2), st_point(3:4))  
g1 <- st_buffer(g0, dist=1)  
is.list(g1)  
f <- function(x) st_as_sfc(st_bbox(x))    
bbs <- do.call("c", lapply(g1, f))  
bbs  
plot(bbs)  
plot(g1, add = T)

I take solution from here https://github.com/r-spatial/sf/issues/1179

0

an alternative could be:

library(sf)

data <- st_read('input_vector.shp')

for (i in 1:(nrow(data))) {
    print(st_bbox(data[i,]))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.