【问题标题】:Overlay a map on top of a 3d surface map in r在 r 中的 3d 表面地图上叠加地图
【发布时间】:2011-10-17 14:12:02
【问题描述】:

我使用 rgl.surface() 创建了一个 3d 地图,主要遵循 Shane 在 this 帖子中的回答。使用我自己的数据,我得到了这张地图

在这张表面图之上,我想添加一张植被密度图,这样我就可以得到这样的东西(通过软件 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 在this 帖子中的帮助。希望这篇文章能帮助更多人!

【问题讨论】:

  • 有趣的问题,但如果没有更多信息很难回答。请张贴您的代码,如果您可以在某处发布您的地图,或者至少描述一下。一种可能性是将植被分数作为彩色地图放入,我很确定rgl.surface() 会支持....但是如果没有更多信息很难说。
  • 对于信息,我不知道如何按照@gsk3 的建议将植被分数作为颜色图放入,所以如果我能获得一个如何执行此操作的示例,那就太好了。谢谢。
  • 只需更改您正在使用的所有颜色线以使用地形矩阵而不是 z 矩阵....

标签: r rgl


【解决方案1】:

修改上面的代码给出答案。请注意,地形应该是与高程矩阵格式相同的矩阵。我在您的函数调用中添加了一个,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)

【讨论】:

  • 非常感谢。我仍然没有得到我想要的东西,但我认为这是因为我给你的颜色代码不适合我想要的东西。感谢您的建议,我会解决的,完成后我会发布我的最终代码和地图。
  • 当然。感谢您承诺在这里跟进:-)。希望这些建议有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
相关资源
最近更新 更多