假设我有一个通过
派生的以下格式的矩阵https://www.mediafire.com/file/n1bds3eo7n8p34c/Bank_Farm_2_elev.tif
setwd("D:/Gs Data")
require(raster)
require(rgdal)
GDALinfo("Bank_Farm_2_elev.tif")
DEM <- raster("Bank_Farm_2_elev.tif")
test_xyz <- rasterToPoints(DEM)
colnames(test_xyz) <- c("xt", "yt", "Elev")
plot(DEM,
maxpixels=ncell(DEM),
col = grey(1:100/100),
main= "LIDAR data")
目的是从左下角到右上角提取横断面。计算点之间的最小距离,并从第三列获取该位置的值。因此,从第3列创建1D横断面数据。简单说明:
我在matlab中找到了以下代码来执行此操作:
%将横断面上每个点的测量值放在一个名为zt
的变量中 for (j in 1:528) { % assuming nr of points for the transect. Transect
coordinates in xt and yt
myMinDist=9999999 % initialise the variable to a very large number
for (i in 1:length(text_xyz)) { % assuming the amount of points in the
whole dataset to search trhough and find the point that lies on the
transect
if ((sqrt((xt(j)-x(i))^2+(yt(j)-y(i))^2))<myMinDist){ % test to try
and get the point closes to our transect point xt(i) and yt(i)
myMinDist=sqrt((xt(j)-x(i))^2+(yt(j)-y(i))^2) % update minDist
zt(j)=(zi)
}
}
}
我试图将R重新写入附加的.tif数据文件
for (j in 1:528) { # the number of rows when dataset is plotted as .tif
file (given by 'dimensions' in the GDALInfo() command)
for (i in 1:length(test_xyz)) { # although length of test_xyz does not
line up with ncell?
if ((sqrt((test_xyz["xt"][j]-x[i])^2+(test_xyz["yt"][j]-y[i])^2))
<myMinDist) {
myMinDist=sqrt((test_xyz["xt"][j]-x[i])^2+(test_xyz["yt"][j]-y[i])^2)
} else {
test_xyz["Elev"][j]=(zi)
}
}
}
我认为我缺少的关键点是设置一个空向量来编写输出,以及在代码中分配xt和x,yt和y,zt和z之间的正确区别。 期待建议。
问候。