Skip to content

Commit 4569f09

Browse files
authored
Merge pull request #95 from UchidaMizuki/fix-st_split-#94
fix st_split() #94
2 parents a96f95b + baab345 commit 4569f09

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

R/split.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ st_split.sfg = function(x, y) {
1919

2020
#' @export
2121
st_split.sfc = function(x, y) {
22+
y = st_geometry(y)
2223
if (length(y) > 1) y = sf::st_combine(y)
2324
if (inherits(x, "sfc_POLYGON") || inherits(x, "sfc_MULTIPOLYGON"))
2425
stopifnot(inherits(y, "sfc_LINESTRING") || inherits(y, "sfc_MULTILINESTRING"))
2526
else
2627
stopifnot(inherits(x, "sfc_LINESTRING") || inherits(x, "sfc_MULTILINESTRING"))
27-
st_sfc(CPL_split(x, st_geometry(y)), crs = st_crs(x))
28+
st_sfc(CPL_split(x, y), crs = st_crs(x))
2829
}
2930

3031
#' @export

tests/testthat/test_lwgeom.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,26 @@ test_that("st_wrap_x works", {
130130
expect_equal(st_bbox(x)$xmin, splitline, check.attributes = FALSE)
131131
expect_equal(st_bbox(x)$xmax, splitline + offset, check.attributes = FALSE)
132132
})
133+
134+
test_that("st_split works", {
135+
library(lwgeom)
136+
library(sf)
137+
138+
l = st_as_sfc('MULTILINESTRING((10 10, 190 10, 190 190), (15 15, 30 30, 100 90))')
139+
140+
pts = st_sfc(st_multipoint(matrix(c(30, 30,
141+
190, 10),
142+
ncol = 2,
143+
byrow = TRUE)))
144+
pts = st_cast(pts, "POINT")
145+
pts_sf = st_sf(pts)
146+
147+
splitted1 = st_split(l, pts)
148+
splitted1 = st_collection_extract(splitted1, "LINESTRING")
149+
150+
splitted2 = st_split(l, pts_sf)
151+
splitted2 = st_collection_extract(splitted2, "LINESTRING")
152+
153+
expect_equal(length(splitted1), 4)
154+
expect_equal(splitted1, splitted2)
155+
})

0 commit comments

Comments
 (0)