I'm working with a large land cover file (~2.3 GB) saved in NetCDF format (it's the 2018 gridded map from here). My aim is to associate GPS points with this land cover data using R.
The problem is that R cannot allocate a vector of sufficient size to get at the land cover data types once I load it.
Is there a way to bound the data first (I want only the land cover from sub-Saharan Africa) before I try to manipulate it?
Here's my code so far with a failed attempt (based on this tutorial):
#' load any packages
library(ncdf4)
# set path and filename
ncpath <- "C:\\Users\\Me\\Documents\\Science\\GIS\\Global Land Cover\\"
ncname <- "C3S-LC-L4-LCCS-Map-300m-P1Y-2018-v2.1.1"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "lccs_class"
# open a netCDF file
ncin <- nc_open(ncfname)
print(ncin)
# get longitude and latitude
lon <- ncvar_get(ncin,"lon")
nlon <- dim(lon)
head(lon)
lat <- ncvar_get(ncin,"lat")
nlat <- dim(lat)
head(lat)
print(c(nlon,nlat))
#' create a bounding box to work with a subset
LonIdx <- which( ncin$dim$lon$vals > 30 & ncin$dim$lon$vals < 32)
LatIdx <- which( ncin$dim$lat$vals > -27 & ncin$dim$lat$vals < -26)
MyVariable <- ncvar_get( ncin, dname)[ LonIdx, LatIdx] # THIS THROWS AN ERROR
# 'Error: cannot allocate vector of size 31.3 Gb'