# Split a polylines according to a specific point in R

I have 2 distinct spatial objects:

• on one hand I have a SpatialLinesDataFrame
• on the other hand I have a SpatialPointsDataFrame

Considering one line and one point that intersects that line, I would like to split the line into two segments according to the point. The point is fixed and does intersect the line, the following command from the rgeos package

``````gIntersects(line.shp,point.shp)
``````

returns TRUE.

A similar question has been asked here but the answer is based on the st_split function. However, this function is no longer implemented in the latest version of the sf package (version 0.7-3).

• Is `st_split` now in the `lwgeom` package? – Spacedman Apr 8 '19 at 18:40
• I just download thew `lwgeom` package and in deed the function is inside. Thanks for the tip – Karim Claudio Apr 11 '19 at 14:27

in your particular context, your point divides the original line in the longitude axis. Thus, you could create two other lines, each of which would have all points of the original one to the left and right of the longitude of the point of interest. A clumsy way of doing so is to filter the coordinates of the `SpatialLinesDataFrame` out of a data frame

``````library(data.table)
library(rgdal)
crs.geo <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84") # CRS for projection

line.coords <- data.table(spdf.line@lines[]@Lines[]@coords) # spdf.line is your spatial line object
segment.1 <- SpatialLines(list(Lines(Line(as.matrix(line.coords[longitude<=sp.point@coords])), ID = 'id')), proj4string = crs.geo)
segment.2 <- SpatialLines(list(Lines(Line(as.matrix(line.coords[longitude>sp.point@coords])), ID = 'id')), proj4string = crs.geo)

# plot it:
plot(segment.1, col = 'red')