Skip to content

Commit

Permalink
Fix value replacement for sfc_POINT
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart committed Feb 16, 2024
1 parent 4ce501e commit 5126cd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion R/sfc.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ st_sfc = function(..., crs = NA_crs_, precision = 0.0, check_ring_dir = FALSE, d
else
do.call(rbind, value)
attr(x, "points")[i, ] = repl
return(structure(x, n_empty = sum(is.na(attr(x, "points")[,1])))) # RETURNS
return(structure(x,
n_empty = sum(is.na(attr(x, "points")[,1])),
bbox = bbox.pointmatrix(attr(x, "points"))
)) # RETURNS
} else
x = x[] # realize
}
value = value[] # realize in case sfc_POINT while x is not
x = unclass(x) # becomes a list, but keeps attributes
ret = st_sfc(NextMethod(), recompute_bbox = TRUE)
structure(ret, n_empty = sum(sfc_is_empty(ret)))
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test_sfc.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,13 @@ test_that("c.sfc n_empty returns sum of st_is_empty(sfg)", {
test_that("st_is_longlat warns on invalid bounding box", {
expect_warning(st_is_longlat(st_sfc(st_point(c(0,-95)), crs = 4326)))
})

test_that("value replacement works for sfc_POINT",{
pts1<-st_geometry(st_as_sf(data.frame(x=1:3,y=1:3), coords = c("x","y")))
pts2<-st_geometry(st_as_sf(data.frame(x=4:5,y=4:5), coords = c("x","y")))
expect_identical(replace(pts1[],2:3,pts2), replace(pts1[],2:3,pts2[]))
expect_identical(replace(pts1,2:3,pts2[])[], replace(pts1[],2:3,pts2[]))
expect_identical(replace(pts1,2:3,pts2)[], replace(pts1[],2:3,pts2[]))
expect_identical(st_bbox(replace(pts1,2:3,pts2)),
st_bbox(replace(pts1[],2:3,pts2[])))# check if bbox is correct without realization
})

0 comments on commit 5126cd6

Please sign in to comment.