How to extract data from a RasterBrick?

后端 未结 2 1532
误落风尘
误落风尘 2020-12-16 06:02

I have a RasterBrick consisting of monthly rainfall data over 7 years, so it has 7 layers with 12 slots each:

rainfall <- brick(\"Rainfall.tif\")
    >         


        
2条回答
  •  不知归路
    2020-12-16 06:12

    Each layer in your RasterBrick will have a unique name, so you can use match('name', names(b)) to find the numeric index of the layer you're interested in. Then use the layer= argument to extract() to point to the layer from which you'd like to extract (setting nl=1 to indicate that you only want that one layer).

    Here's a reproducible example, in which I use cell numbers to do the extracting. (This will work exactly the same way when using SpatialPoints to indicate which values are to be grabbed.)

     ## An example SpatialBrick
    b <- brick(system.file("external/rlogo.grd", package="raster"))
    nlayers(b)
    # [1] 3
    names(b)
    # [1] "red"   "green" "blue"
    
    ## Extract data from given cells in the "green" layer, 
    ii <- match("green", names(b))
    extract(b, 1000:1003, layer=ii, nl=1)
    #      green
    # [1,]   254
    # [2,]   255
    # [3,]   255
    # [4,]   255
    

提交回复
热议问题