沿横断面提取栅格像素的位置
Extract location of raster pixel along a transect
我有以下光栅
library(raster)
r <- raster(ncol=2421, nrow=5005)
r:
class : RasterLayer
dimensions : 2421, 5005, 12117105 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 501121, 506126, 2809088, 2811509 (xmin, xmax, ymin,
ymax)
crs : +proj=utm +zone=17 +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0
source : E:/Datat Layers/Clip/Harney_XMerge.tif
names : Harney_XMerge
values : -3.126388e-13, 57.14 (min, max)
以及以下折线形式的横断面
Line:
class : SpatialLinesDataFrame
features : 7
extent : 500864.6, 505506.2, 2809553, 2811079 (xmin, xmax, ymin,
ymax)
crs : +proj=utm +zone=17 +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0
variables : 3
names : OBJECTID, Id, Shape_Leng
min values : 1, 0, 2716.24783826
max values : 7, 0, 3188.64130203
我想提取沿横断面落下的每个像素的坐标,但无法自行找到任何函数。
我使用了以下提取函数,但它仅从栅格中提取信息,在本例中为树高数据。有没有办法让我使用提取函数或其他函数来提取沿我已建立的横断面落下的每个像素的 UTM 坐标?
extract(r,line )
这是一个最小的可重现示例(复制自,?raster::extract
):
library(raster)
r <- raster(ncol=36, nrow=18, vals=1)
cds1 <- rbind(c(-50,0), c(0,60), c(40,5), c(15,-45), c(-10,-25))
cds2 <- rbind(c(80,20), c(140,60), c(160,0), c(140,-55))
lines <- spLines(cds1, cds2)
extract(r, lines)
在 extract (?extract
) 的文档中,您可以看到有一个参数 cellnumbers
。你可以做到
e <- extract(r, lines, cellnumbers = TRUE)
returns 一个列表,对于每条折线,都有一个包含单元格编号和值的矩阵。从 cellnumbers 你可以得到坐标。
f <- lapply(e, function(x) xyFromCell(r, x[,1]))
或者如果您更喜欢 data.frame
e <- extract(r, lines, cellnumbers = TRUE, df=T)
d <- data.frame(ID=e[,1], xyFromCell(r, e[,2]))
head(d)
# ID x y
#[1,] 1 -5 55
#[2,] 1 5 55
#[3,] 1 -15 45
#[4,] 1 -5 45
#[5,] 1 5 45
#[6,] 1 15 45
如果您需要沿线对点进行排序,请使用提取参数 along=TRUE
我有以下光栅
library(raster)
r <- raster(ncol=2421, nrow=5005)
r:
class : RasterLayer
dimensions : 2421, 5005, 12117105 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 501121, 506126, 2809088, 2811509 (xmin, xmax, ymin,
ymax)
crs : +proj=utm +zone=17 +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0
source : E:/Datat Layers/Clip/Harney_XMerge.tif
names : Harney_XMerge
values : -3.126388e-13, 57.14 (min, max)
以及以下折线形式的横断面
Line:
class : SpatialLinesDataFrame
features : 7
extent : 500864.6, 505506.2, 2809553, 2811079 (xmin, xmax, ymin,
ymax)
crs : +proj=utm +zone=17 +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0
variables : 3
names : OBJECTID, Id, Shape_Leng
min values : 1, 0, 2716.24783826
max values : 7, 0, 3188.64130203
我想提取沿横断面落下的每个像素的坐标,但无法自行找到任何函数。
我使用了以下提取函数,但它仅从栅格中提取信息,在本例中为树高数据。有没有办法让我使用提取函数或其他函数来提取沿我已建立的横断面落下的每个像素的 UTM 坐标?
extract(r,line )
这是一个最小的可重现示例(复制自,?raster::extract
):
library(raster)
r <- raster(ncol=36, nrow=18, vals=1)
cds1 <- rbind(c(-50,0), c(0,60), c(40,5), c(15,-45), c(-10,-25))
cds2 <- rbind(c(80,20), c(140,60), c(160,0), c(140,-55))
lines <- spLines(cds1, cds2)
extract(r, lines)
在 extract (?extract
) 的文档中,您可以看到有一个参数 cellnumbers
。你可以做到
e <- extract(r, lines, cellnumbers = TRUE)
returns 一个列表,对于每条折线,都有一个包含单元格编号和值的矩阵。从 cellnumbers 你可以得到坐标。
f <- lapply(e, function(x) xyFromCell(r, x[,1]))
或者如果您更喜欢 data.frame
e <- extract(r, lines, cellnumbers = TRUE, df=T)
d <- data.frame(ID=e[,1], xyFromCell(r, e[,2]))
head(d)
# ID x y
#[1,] 1 -5 55
#[2,] 1 5 55
#[3,] 1 -15 45
#[4,] 1 -5 45
#[5,] 1 5 45
#[6,] 1 15 45
如果您需要沿线对点进行排序,请使用提取参数 along=TRUE