Skip to content

Commit c818629

Browse files
authored
Merge pull request #2345 from bart1/pointx
Fix value replacement for sfc_POINT
2 parents 4ce501e + 5126cd6 commit c818629

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

R/sfc.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,14 @@ st_sfc = function(..., crs = NA_crs_, precision = 0.0, check_ring_dir = FALSE, d
181181
else
182182
do.call(rbind, value)
183183
attr(x, "points")[i, ] = repl
184-
return(structure(x, n_empty = sum(is.na(attr(x, "points")[,1])))) # RETURNS
184+
return(structure(x,
185+
n_empty = sum(is.na(attr(x, "points")[,1])),
186+
bbox = bbox.pointmatrix(attr(x, "points"))
187+
)) # RETURNS
185188
} else
186189
x = x[] # realize
187190
}
191+
value = value[] # realize in case sfc_POINT while x is not
188192
x = unclass(x) # becomes a list, but keeps attributes
189193
ret = st_sfc(NextMethod(), recompute_bbox = TRUE)
190194
structure(ret, n_empty = sum(sfc_is_empty(ret)))

tests/testthat/test_sfc.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,13 @@ test_that("c.sfc n_empty returns sum of st_is_empty(sfg)", {
119119
test_that("st_is_longlat warns on invalid bounding box", {
120120
expect_warning(st_is_longlat(st_sfc(st_point(c(0,-95)), crs = 4326)))
121121
})
122+
123+
test_that("value replacement works for sfc_POINT",{
124+
pts1<-st_geometry(st_as_sf(data.frame(x=1:3,y=1:3), coords = c("x","y")))
125+
pts2<-st_geometry(st_as_sf(data.frame(x=4:5,y=4:5), coords = c("x","y")))
126+
expect_identical(replace(pts1[],2:3,pts2), replace(pts1[],2:3,pts2[]))
127+
expect_identical(replace(pts1,2:3,pts2[])[], replace(pts1[],2:3,pts2[]))
128+
expect_identical(replace(pts1,2:3,pts2)[], replace(pts1[],2:3,pts2[]))
129+
expect_identical(st_bbox(replace(pts1,2:3,pts2)),
130+
st_bbox(replace(pts1[],2:3,pts2[])))# check if bbox is correct without realization
131+
})

0 commit comments

Comments
 (0)