1

I am using R and the sf package to manipulate shapefiles.

I have transect lines that intersect an island going a few hundred meters inland (see image below). The objective of my project is to trim each transect such that the inland portion is removed. Transect lines extending into Kauai, HI I used sf::st_difference to clip these transect lines with the island shapefile: Trimmed_Transects<-st_difference(st_geometry(Transects), st_union(st_geometry(Coasts)))

This worked great (see image below)! Transect lines trimmed to the coastline of Kauai, HI

However, for rivers, lagoons, and other locations the lines are cut multiple times. For my project, I only want to retain the outermost lines that intersect the coastline (below in red ) and want to remove the inland lines (below in blue). Could anyone recommend a good method to select only the outermost lines?

Transect lines cut multiple times due to inland rivers

2
  • Do you need to retain any attributes of the original lines onto the outputs? One approach could be to extract the endpoints of the original lines, select those that do not intersect the polygon, then retain the line segments that touch that set of points. – Spacedman Jan 24 at 14:05
  • @Spacedman: The attributes of the original lines aren't that important. I mostly need the geometry. I think I see what you mean by keeping the endpoint of the original line and then selecting the line segment that overlaps with the endpoint of the original line. I'll try that out! – Stephan Feb 2 at 21:58
0

The steps I took to solve this problem are as follows.

  1. Obtain lines that were trimmed by a coastline: Trimmed_Transects<-st_difference(Transects, st_union(st_geometry(Coasts)))
  2. Cast the trimmed transects to linestring: Trimmed_Transects_2 <- st_cast(st_cast(Trimmed_Transects, "MULTILINESTRING"),"LINESTRING")
  3. Save the last point of the original transect lines (assuming the last point is offshore): Transect_ends <- st_line_sample(Transects, sample=1)
  4. Create a new sf layer where only linestrings that touch the Transect_ends points are kept: Final_Transects<- Trimmed_Transects_2 %>% st_filter(Transect_ends, .predicate = st_touches)

Example images:

  1. Before

Before

  1. After

After

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.