繁体   English   中英

在r中的3d表面地图上叠加地图

[英]Overlay a map on top of a 3d surface map in r

我使用rgl.surface()创建了一个3d地图,主要是按照Shane在这篇文章中的回答。 使用我自己的数据,我得到了这张地图

在此输入图像描述

在这张表面地图的顶部,我想添加一张植被密度图,这样我就可以获得这样的东西(用Surfer软件获得):

在此输入图像描述

是否可以使用rgl执行此操作,或者对于r中的任何其他包或者是唯一的解决方案,有两个地图,如Shane的答案?

谢谢。

编辑:

根据@ gsk3的请求,以下是此地图的代码:

library(rgl)

# Read the z (i.e. elevation) dimension from file
z1 = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
#create / open x y (i.e. easting and northing coordinates) dimensions 
y=8*(1:ncol(z)) # Each point is 8 m^2
x=8*(1:nrow(z))

# See https://stackoverflow.com/questions/1896419/plotting-a-3d-surface-plot-with-contour-map-overlay-using-r for details of code below
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x,y,z)

我无法发布高程代码,因为有65536(即x * y = 256 * 256)点,但它是一个看起来像这样的矩阵

            [,1]     [,2]     [,3]     [,4]     [,5] 
[1,]    1513.708 1513.971 1514.067 1513.971 1513.875 
[2,]    1513.622 1513.524 1513.578 1513.577 1513.481

等等。 植被密度图也是如此,格式完全相同,每个x * y点都有一个值。 我希望这会让事情变得更清楚......?

编辑2,最终版本

这是我用R制作的地图。我还没有传说,但这是我稍后会做的事情。

在此输入图像描述

最终的代码是

library(rgl)
z1 = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
# Multiply z by 2 to accentuate the relief otherwise it looks a little bit flat.
z= z1*2

#create / open x y dimensions
y=8*(1:ncol(z))
x=8*(1:nrow(z))

trn = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
fv = trn*100
trnlim = range(fv)

fv.colors = colorRampPalette(c("white","tan4","darkseagreen1","chartreuse4")) ## define the color ramp
colorlut =fv.colors(100)c(1,seq(35,35,length.out=9),seq(35,75,length.out=30),seq(75,100,length.out=61))] 

# Assign colors to fv for each point
col = colorlut[fv-trnlim[1]+1 ] 
open3d()
rgl.surface(x,y,z,color=col) 

非常感谢@ gsk3和@nullglob在这篇文章中的帮助。 希望这篇文章能帮到很多人!

修改上面的代码来给出答案。 请注意,terrain应该是与高程矩阵格式相同的矩阵。 我在函数调用中添加了一个,color参数,因此它实际上使用了您创建的颜色矩阵。

library(rgl)

# Read the z (i.e. elevation) dimension from file
z1 = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
#create / open x y (i.e. easting and northing coordinates) dimensions 
y=8*(1:ncol(z)) # Each point is 8 m^2
x=8*(1:nrow(z))

# Read the terrain types from a file
trn = matrix(scan("terrain.txt"),nrow=256, ncol=256, byrow=TRUE)

# See http://stackoverflow.com/questions/1896419/plotting-a-3d-surface-plot-with-contour-map-overlay-using-r for details of code below
trnlim <- range(trn)
trnlen <- trnlim[2] - trnlim[1] + 1
colorlut <- terrain.colors(trnlen,alpha=0) # height color lookup table
col <- colorlut[ trn-trnlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x,y,z,color=col)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM