Search code examples
rshapefiler-rasterrgdal

How to extract values from the Landsat image and plot the spectral profiles of the different materials in R


I have a shapefile for an area that I would like to get the spectral profile of different materials in the area for example water, roads, or vegetation. Something that looks like this?

Spectral Profile Graph

What would be the best way to do this?

I have loaded the shapefile in r but not sure how to extract the values from the Landsat raster stack.

```{r}
landsat<-stack(B1, B2, B3, B4, B5, B6, B7)
roi <- readOGR("roi.shp")
shapes
```

Thank you in advance


Solution

  • Use the following code

    df = raster::extract(landsat,     # raster layer
        roi,                       #shapefile
        fun=mean,         # what value to extract
        df=TRUE)          # return a dataframe
    

    Your raster object and roi should have same coordinate system.