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.

enter image description here

The point is fixed and does intersect the line, the following command from the rgeos package


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 at 18:40
  • I just download thew lwgeom package and in deed the function is inside. Thanks for the tip – Karim Claudio Apr 11 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

crs.geo <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84") # CRS for projection

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

# plot it:
plot(segment.1, col = 'red')
plot(segment.1, col = 'green', add=T)
  • Perfect, my only concern is that I have thousands of cases to solve like that and I'm not 100% sure that all points cut the line in the longitud axis. It's a really good starting point. Thank you very much. – Karim Claudio Apr 11 at 14:25

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.