我想计算数据集中每行显示的点和线之间的距离。下面是一个小例子的数据,我有
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之间的距离。
谢谢你的帮助
1条答案
按热度按时间n8ghc7c11#
这是一个有点不常见的结构,因为它带有一个
geometry
/ sfc列,但sf
类属性没有设置或被故意删除(即sf
方法不能用于帧本身)我们可以这样做,同时保持结果为
data.frame
:sf
点对象,假设坐标在WGS 84中(y
参数)geometry
列(x
参数)和生成的sf
对象(y
)中的行之间的元素距离创建于2023-09-28带有reprex v2.0.2