R语言 按行计算点与线之间的距离

k7fdbhmy  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(139)

我想计算数据集中每行显示的点和线之间的距离。下面是一个小例子的数据,我有

point_id point_lat point_lon                       geometry line_id
1    25417   47.2085   6.23435 LINESTRING (5.71922 47.01, ...    1203
2    21475   47.0499   5.21145 LINESTRING (5.13693 46.9399...    1120

dput(data)

structure(list(point_id = c("25417", "21475"), point_lat = c(47.2084948390554, 
47.049888920732), point_lon = c(6.23434636370335, 5.21144989212184
), geometry = structure(list(structure(c(5.7192239291661, 5.72408115442851, 
47.0100084570507, 47.0055943850847), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(5.13693475951681, 5.14588443111801, 
46.9399237940937, 46.9393947420422), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg"))), class = c("sfc_LINESTRING", "sfc"), precision = 0, bbox = structure(c(xmin = 5.13693475951681, 
ymin = 46.9393947420422, xmax = 5.72408115442851, ymax = 47.0100084570507
), class = "bbox"), crs = structure(list(input = "EPSG:4326", 
    wkt = "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L), 
    line_id = c(1203L, 1120L)), row.names = c(NA, -2L), class = "data.frame")

我想计算出这两点之间的距离,它对应的直线.因此,点25417和线1203之间的距离以及21475和线1120之间的距离。
谢谢你的帮助

n8ghc7c1

n8ghc7c11#

这是一个有点不常见的结构,因为它带有一个geometry/ sfc列,但sf类属性没有设置或被故意删除(即sf方法不能用于帧本身)
我们可以这样做,同时保持结果为data.frame

  • 从点坐标生成sf点对象,假设坐标在WGS 84中(y参数)
  • 查找geometry列(x参数)和生成的sf对象(y)中的行之间的元素距离
library(sf)
data$dist_m <- st_distance(x = data$geometry, 
                           y = st_as_sf(data, coords = c("point_lon", "point_lat"), crs = "WGS84"),
                           by_element = TRUE)
data
#>   point_id point_lat point_lon                       geometry line_id
#> 1    25417  47.20849  6.234346 LINESTRING (5.719224 47.010...    1203
#> 2    21475  47.04989  5.211450 LINESTRING (5.136935 46.939...    1120
#>         dist_m
#> 1 44725.65 [m]
#> 2 13254.55 [m]

创建于2023-09-28带有reprex v2.0.2

相关问题