x y坐标与rgl颠倒。

时间:2022-09-10 18:00:41

Apologies if I'm missing the obvious...

如果我错过了明显的……

I am plotting a 3d surface with rgl. My code is

我正在用rgl绘制一个三维曲面。我的代码是

library(rgl)
dem1 = read.table(file="file.txt",skip=5,header=F,na.strings="0.")
dem = dem1[order(dem1$V1,dem1$V2),] 
z = matrix(dem$V3,nrow=1250,ncol=1250)
is.na(z) = (z < 200)

#create x y dimensions
x=4*(1:nrow(z))
y=4*(1:ncol(z))

open3d()
bg3d("white")
persp3d(x,y,z)

which gives this map (the color was added to see the features better even though I didn't put the code for it above)

这就给出了这张地图(添加了颜色以更好地查看特性,尽管我没有将代码放在上面)

x y坐标与rgl颠倒。

The problem is that whatever I do to this map, it is upside down i.e. x should be y and what is currently y goes from west (0) to east (5000) but it should be the opposite such that the elevated feature should actually be bottom left rather than bottom right.

问题是无论我做这张地图,它是颠倒即x应该和目前y从西方(0)向东(5000),但它应该是相反的,高架特性实际上应该左下角,而不是右下角。

I plotted a very simple contour map using the same file with this script

我用这个脚本绘制了一个非常简单的等高线图。

dem=read.table("file.txt",header=F,skip=5,na.strings="0.")
library(lattice)
contourplot(dem$V3 ~ dem$V1+dem$V2)

which gives

这给了

x y坐标与rgl颠倒。

and which gets the right axes and the most elevated region in the bottom left, exactly where it should be, so there is no problem with the data.

这就得到了右下角的坐标轴和最上层的区域,也就是它应该在的位置,所以数据没有问题。

I explain how the data looks here and why I feel the need to reorder it with

我解释了数据在这里的样子,以及为什么我觉得需要重新排序。

dem = dem1[order(dem1$V1,dem1$V2),] 

The odd thing is whether I use the above command or not the 3d surface map looks exactly the same, which makes me wonder if the code is really using the "dem" dataset created with the order command or whether it is still using the original "dem1" data which it read from the file and which is in the wrong order.

奇怪的是我是否使用上述命令表面3 d地图看起来一模一样,这让我想知道代码是用“*党”数据集创建的订单命令仍然还是用最初的“dem1”它从文件读取数据和错误的订单。

I am very happy to send the data on request or to put it somewhere it can be seen but I can't copy it here as it is 1250 rows x1250 columns.

我很乐意发送请求数据或者把它放到一个可以看到的地方但是我不能把它复制到这里因为它是1250行x1250列。

Thanks in advance!

提前谢谢!

1 个解决方案

#1


2  

The problem is with the creation of z, the matrix of elevations. R fills matrices by columns when creating matrices. It is this filling by columns that is rearranging the elevations relative to one another. This is compounded by the fact that the matrix is square. If the matrix were not square, the relationship between x, y and z would have changed more markedly, instead of just being flipped.

问题在于z的生成,即海拔矩阵。R在创建矩阵时用列填充矩阵。这是由列组成的填充,重新排列相对于另一个的高度。矩阵是方形的这一事实使这更加复杂。如果矩阵不是方形的,那么x、y和z之间的关系就会发生更大的变化,而不是被翻转。

The solution is to have R fill the matrix by rows, e.g. define z using:

解决方案是让R用行填充矩阵,例如:

z <- matrix(dem$V3, nrow=1250, ncol=1250, byrow = TRUE)

#1


2  

The problem is with the creation of z, the matrix of elevations. R fills matrices by columns when creating matrices. It is this filling by columns that is rearranging the elevations relative to one another. This is compounded by the fact that the matrix is square. If the matrix were not square, the relationship between x, y and z would have changed more markedly, instead of just being flipped.

问题在于z的生成,即海拔矩阵。R在创建矩阵时用列填充矩阵。这是由列组成的填充,重新排列相对于另一个的高度。矩阵是方形的这一事实使这更加复杂。如果矩阵不是方形的,那么x、y和z之间的关系就会发生更大的变化,而不是被翻转。

The solution is to have R fill the matrix by rows, e.g. define z using:

解决方案是让R用行填充矩阵,例如:

z <- matrix(dem$V3, nrow=1250, ncol=1250, byrow = TRUE)