R 中的 3D 交集(空间分析)

3D intersection in R (spatial analysis)

R 爱好者大家好,

我无法找到 R 中几何问题的解决方案。我有一个栅格数据集,它表示具有障碍物的地形(障碍物为 5 米)。

 dat1=list()
 dat1$x=seq(481018,by=10,len=10)
 dat1$y=seq(5628255,by=10,len=10)
 dat1$z=matrix(c(rep(1,40),rep(5,20),rep(1,40)),10,10)
 r=raster(dat1)
 crs(r) <- "+proj=utm +zone=32 +datum=WGS84"

我还有一个空间线数据框,它与那个障碍 MAYBE 相交。直线的一点在障碍物下方 (2 m),另一点在障碍物上方 (7 m)。

x <- c(481060,481060)
y <- c(5628340,5628260)
line <- SpatialLines(list(Lines(Line(cbind(x,y)), ID="a")))
line <- SpatialLinesDataFrame(sl = line, data = data.frame("p1"=2,"p2"=7),   match.ID = FALSE)
proj4string(line) <-CRS("+proj=utm +zone=32 +datum=WGS84")

可在此处找到可视化:3D Intersection of raster and spatial line。


我怎样才能知道线是否与障碍物相交以及在哪里相交?这一定是一个 3D 问题还是也可以在 2D 中解决?我在不同的解决方案上搜索了很长时间,但还没有找到任何有用的东西。
预先感谢您的帮助!

有一个简单的解决方案,但肯定不是最有效的解决方案。假设栅格中像元的值为高度。

e<-extract(r,line,cellnumbers=T)[[1]]

给出线条穿过的每个单元格的高度,以及我们稍后需要的单元格编号。

下一步是计算这些单元格中线条的高度。由于您有起点和终点,您可以计算斜率。

slope <- (line@data[1,2]-line@data[1,1])/dist(cbind(x,y))

现在我们有了坡度,我们可以根据到第一个点的距离计算每个单元格的线的高度。

cellcenters <- xyFromCell(r, e[,1], spatial=FALSE) #find the coordinates for the center of the cell
dists <- spDistsN1(cellcenters, c(x[1],y[1]), longlat = FALSE) #calculate the distance to the beginning of the line
height <- dists*slope

现在要找到交点,我们只需要查看线的高度小于或等于从栅格中提取的值的位置:

any(height<=e[,2])