2

我在光栅文件上绘制了一个区域,但我需要知道这个框(通过区域)覆盖了多少像素:光栅文件是1440 pixels*720 lines``(25km*25km).

例子:

   saf <- stack(system.file("external/rlogo.grd", package="raster")) 
    plotRGB( saf )
    e <- drawExtent()

所以在这之后我画了e一个盒子,但是有多少像素/面积是多少?谢谢

4

1 回答 1

3

Try using raster::crop...

crop(saf , e )
#class       : RasterBrick 
#dimensions  : 40, 50, 2000, 3  (nrow, ncol, ncell, nlayers)
#resolution  : 1, 1  (x, y)
#extent      : 23, 73, 26, 66  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=merc 
#data source : in memory
#names       : red, green, blue 
#min values  :   0,     0,    0 
#max values  : 255,   255,  255

And if you just want the number of cells...

ncell( crop(saf , e ) )
#[1] 2000

And to eliminate NA....

x <- crop( saf , e )
ncell( ! is.na(x[]) )
于 2013-09-09T15:28:01.623 回答