从线串形状文件中提取坐标并找到它们之间的距离

Extract coordinates from linestring shapefiles and find distance between them

我有两个 LINESTRING 类型的 shapefile。

一个是对象A和对象B之间的9行列表。

另一个是对象B和对象C之间的18行列表

对于共享相同对象 B 坐标的线串,我想找到相应的对象 A 坐标和对象 C 坐标之间的距离。

例如;

对象 B 到对象 C 线串: 线绳 (21 15, 40.5 10.5) 线串(23 15,40.5 10.5)

对象 B 到对象 A 线串: 线串(53 11, 40.5 10.5)

那我想计算坐标(21, 15)和(53,11)之间的距离,计算(23, 15)和(53,11)之间的距离

要重现我的 LINESTRING shapefile,您可以运行以下代码:

对象 B 到对象 A

ObjectBtoA <-structure(list(structure(c(53, 40.5, 11, 10.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(53, 41.5, 
11, 10.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(53, 42.5, 11, 10.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(53, 40.5, 11.5, 11.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(53, 41.5, 
11.5, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(53, 42.5, 11.5, 11.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(53, 40.5, 12.5, 12.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(53, 41.5, 
12.5, 12.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(53, 42.5, 12.5, 12.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCS[\"WGS 84\",\n    DATUM[\"WGS_1984\",\n        
SPHEROID[\"WGS 84\",6378137,298.257223563,\n            AUTHORITY[\"EPSG\",\"7030\"]],\n        
AUTHORITY[\"EPSG\",\"6326\"]],\n    PRIMEM[\"Greenwich\",0,\n        
AUTHORITY[\"EPSG\",\"8901\"]],\n    UNIT[\"degree\",0.0174532925199433,\n        
AUTHORITY[\"EPSG\",\"9122\"]],\n    AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), class = 
c("sfc_LINESTRING", 
"sfc"), bbox = structure(c(xmin = 40.5, ymin = 10.5, xmax = 53, 
ymax = 12.5), class = "bbox"))

对象 B 到对象 C:

structure(list(structure(c(21, 40.5, 15, 10.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(21, 41.5, 
15, 10.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(21, 42.5, 15, 10.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(21, 40.5, 15, 11.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(21, 41.5, 
15, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
 )), structure(c(21, 42.5, 15, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(21, 40.5, 15, 12.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(21, 41.5, 
15, 12.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(21, 42.5, 15, 12.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(23, 40.5, 15, 10.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(23, 41.5, 
15, 10.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(23, 42.5, 15, 10.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(23, 40.5, 15, 11.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(23, 41.5, 
15, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(23, 42.5, 15, 11.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(23, 40.5, 15, 12.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(23, 41.5, 
15, 12.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(23, 42.5, 15, 12.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCS[\"WGS 84\",\n    DATUM[\"WGS_1984\",\n        
SPHEROID[\"WGS 84\",6378137,298.257223563,\n            AUTHORITY[\"EPSG\",\"7030\"]],\n        
AUTHORITY[\"EPSG\",\"6326\"]],\n    PRIMEM[\"Greenwich\",0,\n        
AUTHORITY[\"EPSG\",\"8901\"]],\n    UNIT[\"degree\",0.0174532925199433,\n        
AUTHORITY[\"EPSG\",\"9122\"]],\n    AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), class = 
c("sfc_LINESTRING", 
"sfc"), bbox = structure(c(xmin = 21, ymin = 10.5, xmax = 42.5, 
ymax = 15), class = "bbox"))

为了让这个答案更简单,而不是使用您数据中的许多行,我只使用从某些点创建的几个线串来回答这个问题。

关于问题:“对于共享相同对象 B 坐标的线串,我想找到对应的对象 A 坐标和对象 C 坐标之间的距离。”

创建点和线:

# Create the lines
x1 <- c(2,3,4)    # str line 1
y1 <- c (3,3,3)
y2 <- c (4,4,4)

x2 <- c(x1 - 5)  # str line 2
x3 <- c(x1-2)    # str line 3
ya <- c(3,4,5)    # diagonal line

l1 <- matrix(data = c(x1, y1), nrow = 3, ncol = 2)
l2 <- matrix(data = c(x2, y1), nrow = 3, ncol = 2)
l3 <- matrix(data = c(x3, y1), nrow = 3, ncol = 2)
l4 <- matrix(data = c(x1, ya), nrow = 3, ncol = 2)

创建简单要素线串 (sfc) 对象:

l1 <- st_linestring(l1, dim = "XY")
l2 <- st_linestring(l2, dim = "XY")
l3 <- st_linestring(l3, dim = "XY")
l4 <- st_linestring(l4, dim = "XY")


g1 <- c(l1, l3)                     # new var with multiple linestrings
g2 <- c(l1, l3, l4)                  # new var with multiple linestrings

绘制新变量:

par(mfrow=c(1,2))    # set the plotting area into a 1*2 array
plot(g1, axes = T)       # plot  horizontal line
plot(g2, axes = T)        # plot horizontal + diag line

确定线串的长度:

st_length(g1)
st_length(g2)

    4
    6.82842712474619

所以这个答案表明当两个或多个线串被分配给一个变量时,当这些线串共享一个公共坐标时,结果长度与连接字符串的长度相同。这条线是直的还是不直的并不重要。变量 g2 与三个线串共享一个公共点。可以证明返回的值 6.82。只需使用勾股定理找到斜边的长度,并将该值添加到其他两个水平线段的长度:( 2 + 2 + 2.828)