将点位置添加到R中的三维DEM绘图

将点位置添加到R中的三维DEM绘图,r,plot,rgl,r-raster,R,Plot,Rgl,R Raster,我有一些点位置,包括UTM和高程作为数据框 我还有一个DEM图层 我已经了解了如何使用rgl中的plot3D在三维中绘制DEM 我还可以使用points3d在3D中绘制点 我已经能够使用points3d和add=TRUE 然而,点和DEM之间的距离非常遥远 在下面的代码中,我也尝试将其更改为空间数据帧,但rgl似乎不喜欢这样 是否可以将它们与DEM上的点一起绘制 我一直在寻找解决这个问题的办法 以下是我迄今为止使用的R代码: > library(raster) > librar

我有一些点位置,包括UTM和高程作为数据框 我还有一个DEM图层

我已经了解了如何使用
rgl
中的
plot3D
在三维中绘制DEM

我还可以使用
points3d
在3D中绘制点

我已经能够使用
points3d
add=TRUE
然而,点和DEM之间的距离非常遥远

在下面的代码中,我也尝试将其更改为空间数据帧,但
rgl
似乎不喜欢这样

是否可以将它们与DEM上的点一起绘制

我一直在寻找解决这个问题的办法

以下是我迄今为止使用的R代码:

> library(raster)
> library(rgdal)
> library(maptools)
> library(rgeos)
> library(lattice)
> library(latticeExtra)
> library(sp)
> library(rasterVis)
> library(rgl)
> 
> # taking data read from a .csv of UTM and elevation values
> 
> Points.Sp <- data.frame(Points=Rawdata$PointName, UTM.N=Rawdata$UTM.N, UTM.W=Rawdata$UTM.W, Elevation=Rawdata$Elevation)
> Points.Sp <- unique(Points.Sp) #weeding out duplicates
> Points.Sp <- Points.Sp[,c(3,2,4)] #getting rid of point names # I realize this looks messy but it gets what I want
> head(Points.Sp)
    UTM.W   UTM.N Elevation
1  275815 3879223      1340
8  274813 3879727      1325
29 275312 3879727      1258
45 275812 3879724      1169
66 276313 3879727      1067
75 276813 3879727      1208
> 
> dem.in <- raster("D:/Thesis/SouthernApps/Coweeta/Coweeta/DEM_30m_wgs84.img") # reading in DEM
> plot(dem.in) # check in 2D # takes a long time very large, need to crop
> 
> dem.crop <- crop(dem.in, c(272000, 282000, 3878000, 3884000))
> plot(dem.crop) # check in 2D, looks good.
> 
> plot3D(dem.crop) # plot in 3D looks like exactly what I want
> 
> points3d(Points.Sp, pch=19, cex=2, col="black", add=TRUE) # adds the points to plot but in wrong place
> 
> #attempting to set a CRS in case this is the problem.
> coordinates(Points.Sp)=c(1,2)
> proj4string(Points.Sp)=CRS("++proj=utm +zone=17") # set CRS
> str(Points.Sp)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 71 obs. of  1 variable:
  .. ..$ Elevation: int [1:71] 1340 1325 1258 1169 1067 1208 1256 1089 1031 959 ...
  ..@ coords.nrs : num [1:2] 1 2
  ..@ coords     : num [1:71, 1:2] 275815 274813 275312 275812 276313 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:71] "1" "8" "29" "45" ...
  .. .. ..$ : chr [1:2] "UTM.W" "UTM.N"
  ..@ bbox       : num [1:2, 1:2] 274309 3878440 279876 3883732
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "UTM.W" "UTM.N"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=utm +zone=17 +ellps=WGS84"
> 
> # trying this a different way after setting CRS
> x <- Points.Sp@coords[1:71,1]
> y <- Points.Sp@coords[1:71,2]
> z <- Points.Sp@data$Elevation
> m <- data.frame(x=x,y=y,z=z)
> 
> plot3D(dem.crop) #again, plot in 3D looks like exactly what I want
> points3d(m, pch=19, cex=2, col="black", add=TRUE) # still adds the points to plot but in wrong place
>库(光栅)
>图书馆(rgdal)
>图书馆(地图工具)
>图书馆(rgeos)
>图书馆(格子)
>图书馆(latticeExtra)
>图书馆(sp)
>图书馆(拉斯特维斯)
>图书馆(rgl)
> 
>#从UTM和高程值的.csv读取数据
> 
>点.Sp点.Sp点.Sp头(点.Sp)
UTM.W UTM.N立面图
1  275815 3879223      1340
8  274813 3879727      1325
29 275312 3879727      1258
45 275812 3879724      1169
66 276313 3879727      1067
75 276813 3879727      1208
> 
>dem.in plot(dem.in)#检入2D#需要很长时间,需要裁剪
> 
>dem.crop绘图(dem.crop)#检查2D,看起来不错。
> 
>plot3D(dem.crop)#3D打印看起来正是我想要的
> 
>点3d(Points.Sp,pch=19,cex=2,col=“black”,add=TRUE)#将点添加到绘图中,但位置错误
> 
>#尝试设置CRS,以防出现问题。
>坐标(Points.Sp)=c(1,2)
>proj4string(Points.Sp)=CRS(“++proj=utm+zone=17”)#设置CRS
>str(Points.Sp)
带有5个插槽的正式类“SpatialPointsDataFrame”[包“sp”]
..@data:'data.frame':71 obs。第1个变量:
.. ..$ 立面图:int[1:71]1340 1325 1258 1169 1067 1208 1256 1089 1031 959。。。
..@coords.nrs:num[1:2]12
..@coords:num[1:71,1:2]275815 274813 275312 275812 276313。。。
.. ..- 属性(*,“dimnames”)=2个列表
.. .. ..$ : chr[1:71]“1”“8”“29”“45”。。。
.. .. ..$ : chr[1:2]“UTM.W”“UTM.N”
..@bbox:num[1:2,1:2]274309 3878440 279876 3883732
.. ..- 属性(*,“dimnames”)=2个列表
.. .. ..$ : chr[1:2]“UTM.W”“UTM.N”
.. .. ..$ : chr[1:2]“最小”“最大”
..@proj4string:正式类“CRS”[包“sp”]带1个插槽
.. .. ..@ 项目名称:chr“+proj=utm+zone=17+ellps=WGS84”
> 
>#在设置CRS后尝试另一种方式
>x y z m
>plot3D(dem.crop)#同样,在3D中绘图看起来正是我想要的
>点3d(m,pch=19,cex=2,col=“black”,add=TRUE)#仍然将点添加到绘图中,但位置错误
这段代码再现了这个问题。 定义光栅对象 数据(火山)
r如帮助页面中所述,x轴和y轴均使用z值进行调整。您可以使用
adjust=FALSE
禁用此默认设置:

library(rgl)
library(rasterVis)

## define a Raster object
data(volcano)
r <- raster(volcano)
extent(r) <- c(0, 610, 0, 870)

## extract sample points
xy <- sampleRandom(r, 100, xy = TRUE)     

## display them
plot3D(r, adjust = FALSE)
points3d(xy)
库(rgl)
图书馆(拉斯特维斯)
##定义光栅对象
数据(火山)
r
定义光栅对象
数据(火山)

r这看起来像是
plot3D
中的错误或设计缺陷,它不是
rgl
函数。我不知道你是从哪个包裹买的。
rgl
功能是
plot3d
,它可能对
dem.crop
对象不起作用。请,发布a。上面的代码应该运行一个可复制的示例。我希望点平放在3d光栅上…我刚刚找到了一个解决方案,我将在下面发布,只要我找到一种方法用火山示例重现它,我没有正确理解您的问题。建议使用
rasterVis
:为
rgl::plot3d
定义S3方法,而不是新的不兼容函数
plot3d
。使用
rgl::aspect3d
功能调整显示,而不是修改数据。我刚刚尝试了发布的adjust=FALSE解决方案,虽然这有助于将点与DEM对齐,但它没有将点设置在景观上,而是点仍然非常遥远,即使在查看窗口中调整,没有排得那么好。对不起,我应该说您的示例运行得很好,但是我无法获得数据来做同样的事情。再次发布一个可复制的问题。@user2554330谢谢您的建议。我会用这个函数的原始开发人员来考虑它们。