Mask raster with shapefile, error in colnames

130 views Asked by At

In the R Raster package, when I load a netCDF file into a raster and attempt to mask or crop the raster with a shapefile, I am getting the error:

"*Error in `colnames<-`(`*tmp*`, value = names(object)) : 
  attempt to set 'colnames' on an object with less than two dimensions*"

Here is example code

library(ncdf4)
library(raster)

nc <- brick("in.netcdf.nc", varname = "diff", values=TRUE )
crs(nc) <- "+proj=utm +zone=17 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
everShp <- shapefile("EVERareas")

nc.sub <- mask(nc, everShp)

Plotting shows both nc and everShp are 2D and spatially overlap. Thanks for any suggestions with this error.

1

There are 1 answers

0
Len Pearlstine On

if I replace

nc <- brick("in.netcdf.nc", varname = "diff", values=TRUE )

with

nc <- raster("in.netcdf.nc", varname = "diff", values=TRUE )

the mask and crop commands work as expected.