I'm getting an error with a line of code that I've run successfully in the past. My goal is to extract landsat pixel values from training data polygon shapefile for classification. Here's my code up until the error:
Load, stack, and brick Landsat rasters
all_landsat_bands<-list.files("E:/NorthBahamas_GDB/WorkingFiles/POST_DORIAN/Train/L2SP_POST", pattern =".tif$", full.names=TRUE)
landsat_stack <-stack(all_landsat_bands)
landsat_brick <- brick(landsat_stack)
'stack': Links several raster objects written to different files, 'brick': Creates one multi-layer file
Assign column headers to Landsat brick
names(landsat_brick)
vecnames=c("b2","b3","b4","b5","b6","b7", "NDVI")
names(landsat_brick)=vecnames
Load training data
All_CAT<-shapefile("E:/NorthBahamas_GDB/WorkingFiles/POST_DORIAN/Train/POST_MapV5_15.shp")
Check CRS and projection of landsat and training data
crs(All_CAT)
crs(landsat_brick)
Set the CRS of training data to match the landsat brick
ALLCAT_prj <- spTransform(All_CAT, crs(landsat_brick))
Extract the landsat pixel values from training data
ext2<-extract(landsat_brick, ALLCAT_prj, df = TRUE)
The error I'm getting here with the 'extract' function is Error in res[i] <- readBin(x@file@con, what = dtype, n = 1, size = dsize, : replacement has length zero
I obviously still have a lot to learn with spatial analysis in R but was unable to find a solution through troubleshooting.
Do you have any ideas on what could be causing this error?
This line of code worked just yesterday. The only thing that changed was the input rasters.
ALLCAT_prj
on a map and show that? Or share the data files? Or at least show us some summary info of your data - bounding boxes, CRS, number of polygons etc?raster
package is no longer in active development. Not long ago I had some very strange behavior with brick objects so, I think there may be something amiss with the object class. So, either just useraster::stack
or, mover over toterra
, the package that is replacingraster
. Since you are learning, I would recommend learning the new interface and not the old. The raster/brick difference is related to on disk performance and not all that important a distinction, you would want a stack anyway.