1

给定一条线和沿该线的两个点,我如何突出显示两点之间的线段?

下面的代码提供了一条线 ( tr ) 和落在其上的两个点 ( sg ) 的示例,输出 (R/Leaflet) 如下所示

我找到了连接点的示例(例如此处),但这并不是我真正想要的:我需要绘制/突出显示两点之间的线段,而不是两点之间的直线。我能找到的最接近的东西是突出显示街道的一部分(例如这里),但我不能在这里真正应用它(使用铁路线+我将无法使用谷歌地图)。

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)

会话信息():

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
4

0 回答 0