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

vecnames=c("b2","b3","b4","b5","b6","b7", "NDVI")

Load training data


Check CRS and projection of landsat and training data


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.

  • Hard to tell without some information about your data - can you plot the brick and the 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?
    – Spacedman
    Commented Feb 16, 2022 at 16:15
  • I would note that, even if there were a bug, it will likely never be addressed as the 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 use raster::stack or, mover over to terra, the package that is replacing raster. 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. Commented Feb 16, 2022 at 21:47

1 Answer 1


You might check for insufficient disk space, guessing from the error thrown by readBin: https://gis.stackexchange.com/a/108360/201201 (or rather switch to package {terra} as Jeffrey Evans suggested).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.