从 R 中的坐标列表在简单特征中创建多行
Create numerous lines in Simple Features from list of coordinates in R
我正在尝试检测成对的对象(树木)是否被道路隔开或位于它们的同一侧。我已经下载了我的路网,我想我或多或少知道如何使用st_intersects
。所以我所缺少的只是我正在考虑的成对树木之间的线段,以便测试与道路的交叉点..
但是,我似乎无法弄清楚如何在我的对象之间创建线条。我有很多对 (300K+),所以必须能够以编程方式执行此操作,而我找到的所有示例似乎都是 "hand coded".
假设有以下两个矩阵,包含每对"origin"和"destination"的坐标。
orig = matrix(runif(20),ncol=2)
dest = matrix(runif(20),ncol=2)
在这个例子中,我需要创建 10 行:一个在 orig[1,]
和 dest[1,]
之间,另一个(不同的)在 orig[2,]
和 dest[2,]
之间,等等。我的理解是我应该使用 st_multilinestring
,但我不知道如何制定调用。通常,我要么以 "XYZM" 点结束,要么以从 orig[1,]
开始并在经过所有其他坐标后以 dest[10,]
结束的多段线结束。当它不是这些结果之一时,它就是一大堆错误。
st_multilinestring
是我应该使用的吗?如果是,如何做到这一点?谢谢!!
使用 lapply
遍历起点和终点矩阵的行并创建 LINESTRING 对象的向量:
> lines = do.call(st_sfc,
lapply(
1:nrow(orig),
function(i){
st_linestring(
matrix(
c(orig[i,],dest[i,]), ncol=2,byrow=TRUE)
)
}
)
)
这给你这个:
> lines
Geometry set for 10 features
geometry type: LINESTRING
dimension: XY
bbox: xmin: 0.06157865 ymin: 0.007712881 xmax: 0.967166 ymax: 0.9864812
epsg (SRID): NA
proj4string: NA
First 5 geometries:
LINESTRING (0.6646269 0.1545195, 0.8333102 0.40...
LINESTRING (0.5588124 0.5166538, 0.3213998 0.08...
LINESTRING (0.06157865 0.6138778, 0.06212246 0....
LINESTRING (0.202455 0.4883115, 0.5569435 0.986...
LINESTRING (0.3120373 0.8189916, 0.8499419 0.73...
让我们检查一下我们是否做对了。第四行从哪里来,去哪里?
> orig[4,]
[1] 0.2024550 0.4883115
> dest[4,]
[1] 0.5569435 0.9864812
这看起来像第四个 LINESTRING 输出中的坐标。
然后您可以 st_intersects
将其与另一组功能结合使用,看看其中哪些与它们相交。
(您可能还需要为它们添加坐标系...)
这是使用 library(sfheaders)
构造 sfc
/ sf
对象的方法
library(sf)
library(sfheaders)
## If you add a pseudo-id column
orig <- cbind( orig, 1:nrow( orig ) )
dest <- cbind( dest, 1:nrow( dest ) )
## you can bind these matrices together
m <- rbind( orig, dest )
## set the order by the 'id' column
m <- m[ order( m[,3] ), ]
## then use `sfheaders` to create your linestrings
sfc <- sfheaders::sfc_linestring(
obj = m
, linestring_id = 3 ## 3rd column
)
sfc
# Geometry set for 10 features
# geometry type: LINESTRING
# dimension: XY
# bbox: xmin: 0.01952919 ymin: 0.04603703 xmax: 0.9172785 ymax: 0.9516615
# epsg (SRID): NA
# proj4string: NA
# First 5 geometries:
# LINESTRING (0.7636528 0.2465392, 0.05899529 0.7...
# LINESTRING (0.6435893 0.9158161, 0.01952919 0.1...
# LINESTRING (0.05632407 0.3106372, 0.03306822 0....
# LINESTRING (0.1978259 0.07432209, 0.2907429 0.0...
# LINESTRING (0.1658199 0.6436758, 0.1407145 0.75...
我正在尝试检测成对的对象(树木)是否被道路隔开或位于它们的同一侧。我已经下载了我的路网,我想我或多或少知道如何使用st_intersects
。所以我所缺少的只是我正在考虑的成对树木之间的线段,以便测试与道路的交叉点..
但是,我似乎无法弄清楚如何在我的对象之间创建线条。我有很多对 (300K+),所以必须能够以编程方式执行此操作,而我找到的所有示例似乎都是 "hand coded".
假设有以下两个矩阵,包含每对"origin"和"destination"的坐标。
orig = matrix(runif(20),ncol=2)
dest = matrix(runif(20),ncol=2)
在这个例子中,我需要创建 10 行:一个在 orig[1,]
和 dest[1,]
之间,另一个(不同的)在 orig[2,]
和 dest[2,]
之间,等等。我的理解是我应该使用 st_multilinestring
,但我不知道如何制定调用。通常,我要么以 "XYZM" 点结束,要么以从 orig[1,]
开始并在经过所有其他坐标后以 dest[10,]
结束的多段线结束。当它不是这些结果之一时,它就是一大堆错误。
st_multilinestring
是我应该使用的吗?如果是,如何做到这一点?谢谢!!
使用 lapply
遍历起点和终点矩阵的行并创建 LINESTRING 对象的向量:
> lines = do.call(st_sfc,
lapply(
1:nrow(orig),
function(i){
st_linestring(
matrix(
c(orig[i,],dest[i,]), ncol=2,byrow=TRUE)
)
}
)
)
这给你这个:
> lines
Geometry set for 10 features
geometry type: LINESTRING
dimension: XY
bbox: xmin: 0.06157865 ymin: 0.007712881 xmax: 0.967166 ymax: 0.9864812
epsg (SRID): NA
proj4string: NA
First 5 geometries:
LINESTRING (0.6646269 0.1545195, 0.8333102 0.40...
LINESTRING (0.5588124 0.5166538, 0.3213998 0.08...
LINESTRING (0.06157865 0.6138778, 0.06212246 0....
LINESTRING (0.202455 0.4883115, 0.5569435 0.986...
LINESTRING (0.3120373 0.8189916, 0.8499419 0.73...
让我们检查一下我们是否做对了。第四行从哪里来,去哪里?
> orig[4,]
[1] 0.2024550 0.4883115
> dest[4,]
[1] 0.5569435 0.9864812
这看起来像第四个 LINESTRING 输出中的坐标。
然后您可以 st_intersects
将其与另一组功能结合使用,看看其中哪些与它们相交。
(您可能还需要为它们添加坐标系...)
这是使用 library(sfheaders)
sfc
/ sf
对象的方法
library(sf)
library(sfheaders)
## If you add a pseudo-id column
orig <- cbind( orig, 1:nrow( orig ) )
dest <- cbind( dest, 1:nrow( dest ) )
## you can bind these matrices together
m <- rbind( orig, dest )
## set the order by the 'id' column
m <- m[ order( m[,3] ), ]
## then use `sfheaders` to create your linestrings
sfc <- sfheaders::sfc_linestring(
obj = m
, linestring_id = 3 ## 3rd column
)
sfc
# Geometry set for 10 features
# geometry type: LINESTRING
# dimension: XY
# bbox: xmin: 0.01952919 ymin: 0.04603703 xmax: 0.9172785 ymax: 0.9516615
# epsg (SRID): NA
# proj4string: NA
# First 5 geometries:
# LINESTRING (0.7636528 0.2465392, 0.05899529 0.7...
# LINESTRING (0.6435893 0.9158161, 0.01952919 0.1...
# LINESTRING (0.05632407 0.3106372, 0.03306822 0....
# LINESTRING (0.1978259 0.07432209, 0.2907429 0.0...
# LINESTRING (0.1658199 0.6436758, 0.1407145 0.75...