使用R中的SpatialLine对象提取栅格像素值以及像素xy坐标 [英] extract raster pixel value as well as pixel xy coordinates using a SpatialLine object in R

查看:1189
本文介绍了使用R中的SpatialLine对象提取栅格像素值以及像素xy坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spatialline,我从多边形shapefile转换(根据imagebrick中的功能手动数字化 - 这意味着polyline和imagebrick在空间上重叠,如我所愿)

I have a Spatialline that I converted from a polygon shapefile (digitized manually based on features in "imagebrick" - this means that spatially the "polyline" and "imagebrick" are overlapped as I wanted)

polyline <- as(shapefiles_data[1,],"SpatialLines")

> polyline
class       : SpatialLines 
features    : 1 
extent      : 357714.3, 357719, 4076030, 4076035  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=11 +datum=NAD27 +units=m +no_defs +ellps=clrk66 +nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat 

和Rasterbrick

And a Rasterbrick

> imagebrick
class       : RasterBrick 
dimensions  : 29180, 14940, 435949200, 4  (nrow, ncol, ncell, nlayers)
resolution  : 0.6, 0.6  (x, y)
extent      : 354038.4, 363002.4, 4066992, 4084500  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=11 +datum=NAD27 +units=m +no_defs +ellps=clrk66 +nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat 
names       : band1, band2, band3, band4 
min values  :     0,     0,     0,     0 
max values  : 65535, 65535, 65535, 65535 

我试图提取Rasterbrick中被Line(SpatialLine)触及的像素。但我还想提取与这些提取的像素相关联的xy坐标,并将它们一起存储在SpatialPointsDataFrame中。我试过了:

I was trying to extract pixels in the Rasterbrick that touched by the Line(SpatialLine). But I also wanted to pull out xy coordinates associated with those extracted pixels and store them together in a SpatialPointsDataFrame. I've tried:

extract_polyline_test <- extract(imagebrick, polyline)
extract_polyline_test <- as.data.frame(extract_polyline_test)
> extract_polyline_test
band1 band2 band3 band4
1    125   156    73   392
2    129   161    80   366
3    156   208   122   374
4    142   175    97   330
5    102   117    31   389
6    117   143    57   381
7    162   221   127   413
8    213   302   204   405
..   ...   ...   ...   ...

然后,我试图使用RasterToPoints函数来提取xy坐标。这需要一个包含我想要xy的所有像素的栅格图层。我试图使用crop来获得光栅线,但它不起作用。

Then, I was trying to use RasterToPoints function to pull out the xy coordinates. This requires a raster layer that contains all the pixels that I want xy from. I've tried to use "crop" to get that raster "line" but it does not work.

rastercrop_polyline <-crop(imagebrick,polyline)
rastercrop_polyline

> rastercrop_polyline
class       : RasterBrick 
dimensions  : 8, 7, 56, 4  (nrow, ncol, ncell, nlayers)
resolution  : 0.6, 0.6  (x, y)
extent      : 357714.6, 357718.8, 4076030, 4076035  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=11 +datum=NAD27 +units=m +no_defs +ellps=clrk66 +nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat 
data source : in memory
names       : band1, band2, band3, band4 
min values  :    72,    63,     0,   156 
max values  :   274,   415,   299,   497 

s.polyline <- rasterToPoints(rastercrop_polyline)
> s.polyline
         x       y band1 band2 band3 band4
 [1,] 357714.9 4076035    93    96    17   342
 [2,] 357715.5 4076035    72    63     0   377
 [3,] 357716.1 4076035    90    93    17   371
 [4,] 357716.7 4076035   125   156    73   392
 [5,] 357717.3 4076035   129   161    80   366
 [6,] 357717.9 4076035   156   208   122   374
 [7,] 357718.5 4076035   142   175    97   330
 [8,] 357714.9 4076034   100   108    25   326
 ..  ........  .......   ...   ..     ..    ...

似乎裁剪功能不会裁剪光栅的线但整个多边形。有没有其他方法来获取我提取的像素的xy坐标?

It seems that the "crop" function does not crop a "line" of raster but a whole "polygon". Is there any other way to get the xy coordiantes of the pixels that I extracted?

p.s。我还试图提取每个提取像素的周围像素。我该怎么做?我注意到提取函数中有一个函数参数。我应该在提取函数中编写一个小函数,还是有其他方法可以做到这一点?

p.s. I was also trying to extract surrounding pixels of each extracted pixels. How should I do this? I notice that there is a function parameter in extract function. Should I work on writing a small function within the extraction function or there are any other ways to do this?

p.p.s我还想使用Rasterize函数来栅格化SpatialLine,然后使用RasterToPoints拉出xy坐标。但我无法控制栅格化的SpatialLine的每个像素等于原始图像砖中线所触及的像素。

p.p.s I was also thinking to use Rasterize function to rasterize the "SpatialLine" and then use the RasterToPoints to pull out the xy coordinates. But I can not control each pixel of the "rasterized SpatialLine" equals to the pixels that touched by the "line" in the original imagebrick.

感谢大家抽出时间查看我的问题,并提前感谢您的帮助。

Thanks everyone for your time viewing my question and thanks in advance for your help.

推荐答案

你说你正在考虑使用Rasterize函数来栅格化SpatialLine,然后使用RasterToPoints来拉出xy坐标。

You say that you were "thinking to use Rasterize function to rasterize the "SpatialLine" and then use the RasterToPoints to pull out the xy coordinates."

这对你想要的东西来说似乎是正确的

That seems correct for what you want

然后你说你无法控制栅格化的SpatialLine的每个像素等于触及的像素 在原始图像砖中。

But then you say that you "can not control each pixel of the "rasterized SpatialLine" equals to the pixels that touched by the "line" in the original imagebrick."

但它们是,但具有多行的单元格仅表示一次。

But they are, except that cells with multiple lines are only represented once.

另一种方法:

# set up some example data
r <- raster()
values(r) <- runif(ncell(r))
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 <- SpatialLines(list(Lines(list(Line(cds1)), "1"), Lines(list(Line(cds2)), "2") ))

# values for lines
v <- extract(r, lines)
# create raster with cell numbers
ids <- init(r, v='cell')
# extract these
cells <- extract(ids, lines)
# compute xy
xy = lapply(cells, function(x) xyFromCell(r, x))

这篇关于使用R中的SpatialLine对象提取栅格像素值以及像素xy坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆