1

Given a line and two points along that line, how can I highlight the section of the line between the two points?

The code below provides an example of a line (tr) and two points (sg) that fall on it, with the output (R/Leaflet) looking like this.

I have found examples of connecting points (e.g. here), but isn't really what I'm looking for: I need to draw/highlight the segment of the line between the two points, and not a straight line between the two points. The closest thing I could find is highlighting a section of a street (e.g. here), but I can't really apply that here (working with railway lines + I won't be able to use Google Maps).

library(tidyverse)
library(sf) 
library(leaflet)

tr <- structure(
  list(
    SHAPE_Length = 22210.9047689, 
    SHAPE = structure(list(
      structure(c(-3.447431, -3.426467, -3.3717497, -3.3504529, 
                  -3.34384, -3.3063404, -3.2875194, -3.2388496, -3.1727098, 
                  -3.136474, -3.1031447, 58.51414, 58.5140392, 58.5066418, 
                  58.5013098, 58.4950488, 58.4901998, 58.4837109, 58.4745371, 
                  58.4552422, 58.4512823, 58.4421872), .Dim = c(11L, 2L),
                class = c("XY", "LINESTRING", "sfg"))), 
      n_empty = 0L, crs = structure(list(
        input = "+init=epsg:4326", 
        wkt = "GEOGCRS[\"WGS 84\",\n    
        DATUM[\"World Geodetic System 1984\",\n        
        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            
        LENGTHUNIT[\"metre\",1]],\n        
        ID[\"EPSG\",6326]],\n    
        PRIMEM[\"Greenwich\",0,\n        
        ANGLEUNIT[\"degree\",0.0174532925199433],\n        
        ID[\"EPSG\",8901]],\n    
        CS[ellipsoidal,2],\n        
        AXIS[\"longitude\",east,\n            
        ORDER[1],\n            
        ANGLEUNIT[\"degree\",0.0174532925199433,\n                
        ID[\"EPSG\",9122]]],\n        
        AXIS[\"latitude\",north,\n            
        ORDER[2],\n            
        ANGLEUNIT[\"degree\",0.0174532925199433,\n                
        ID[\"EPSG\",9122]]],\n    
        USAGE[\n        
        SCOPE[\"unknown\"],\n        
        AREA[\"World.\"],\n        
        BBOX[-90,-180,90,180]]]"), 
        class = "crs"), 
      class = c("sfc_LINESTRING", "sfc"), 
      precision = 0, 
      bbox = structure(c(xmin = -3.447431, ymin = 58.4421872, 
                         xmax = -3.1031447, ymax = 58.51414), class = "bbox"))), 
  row.names = 1L, class = c("sf", "data.frame"), 
  sf_column = "SHAPE", 
  agr = structure(c(SHAPE_Length = NA_integer_), 
                  .Label = c("constant", "aggregate", "identity"), class = "factor"))


sg <- structure(list(
  geometry = structure(list(structure(c(-3.12213008366773, 58.4467460240336), 
                                      class = c("XY", "POINT", "sfg")), 
                            structure(c(-3.28773441769833, 58.483750111291), 
                                      class = c("XY", "POINT", "sfg"))), 
                       class = c("sfc_POINT", "sfc"), 
                       precision = 0, 
                       bbox = structure(c(xmin = -3.28773441769833, ymin = 58.4467460240336, 
                                          xmax = -3.12213008366773, ymax = 58.483750111291), 
                                        class = "bbox"), 
                       crs = structure(list(input = "+init=epsg:4326",
                                            wkt = "GEOGCRS[\"WGS 84\",\n    
                                            DATUM[\"World Geodetic System 1984\",\n        
                                            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            
                                            LENGTHUNIT[\"metre\",1]],\n        
                                            ID[\"EPSG\",6326]],\n    
                                            PRIMEM[\"Greenwich\",0,\n        
                                            ANGLEUNIT[\"degree\",0.0174532925199433],\n        
                                            ID[\"EPSG\",8901]],\n    
                                            CS[ellipsoidal,2],\n        
                                            AXIS[\"longitude\",east,\n            
                                            ORDER[1],\n            
                                            ANGLEUNIT[\"degree\",0.0174532925199433,\n                
                                            ID[\"EPSG\",9122]]],\n        
                                            AXIS[\"latitude\",north,\n            
                                            ORDER[2],\n            
                                            ANGLEUNIT[\"degree\",0.0174532925199433,\n                
                                            ID[\"EPSG\",9122]]],\n    
                                            USAGE[\n        
                                            SCOPE[\"unknown\"],\n        
                                            AREA[\"World.\"],\n        
                                            BBOX[-90,-180,90,180]]]"), 
                                       class = "crs"), n_empty = 0L)), 
  row.names = 1:2, class = c("sf","data.frame"), 
  sf_column = "geometry", 
  agr = structure(integer(0), .Label = c("constant","aggregate", "identity"), 
                  class = "factor", .Names = character(0)))


# Plot the stuff
leaflet() %>%
  addTiles() %>%
  addPolylines(data = tr) %>%
  addMarkers(data = sg)

sessionInfo():

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
tidyverse_1.3.1
sf_1.0-3
leaflet_2.0.4.1
  • 2
    Please read [(1)](https://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](https://stackoverflow.com/help/mcve) how to create a MCVE as well as [(3)](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. Then edit and improve your question accordingly. I.e., abstract from your real problem... – Christoph Oct 25 '21 at 18:44
  • 1
    Thanks for your response @Christoph, have updated my question accordingly (I hope). Would appreciate any further feedback. – munchausend Oct 27 '21 at 10:51
  • Does this https://stackoverflow.com/q/57014381/5784831 or this https://gis.stackexchange.com/questions/338679/spatial-subsetting-spatialpolygonsdataframes-completely-within-selecting-area-us help? – Christoph Oct 27 '21 at 11:25
  • For bachground see e.g. https://r-spatial.github.io/sf/articles/sf1.html – Christoph Oct 27 '21 at 11:26
  • 1
    Thank you! These look very useful, especially the first one. I'll take this away and see if I can make it work. – munchausend Oct 27 '21 at 11:49
  • Good luck! I could not make it work in 5 to 10 minutes ;-) – Christoph Oct 27 '21 at 11:52

0 Answers0