hunzikp / velox

https://hunzikp.github.io/velox/
119 stars 23 forks source link

Velox extract error: "Error in boostFactory$makePointGrid ... std::bad_alloc" #34

Open GatesDupont opened 5 years ago

GatesDupont commented 5 years ago

Hello,

I'm trying to extract raster data from a grid of points (right now, n=8), but I keep getting am ambiguous error message, and I don't have any idea why. This seems like a fairly straightforward operation with Velox. I guess it could be something wrong with my code? I have followed the vignette pretty closely, however.

Any help you could offer would be greatly appreciated!

#----Loading NLCD raster----
nlcd=raster(file_name)

#----Generating overall outline of selected states----
states.full = c("Maine", "New Hampshire", "Vermont", "Massachusetts",
                "Rhode Island", "Connecticut", "New York", "Pennsylvania",
                "New Jersey")
us = raster::getData('GADM', country = 'US', level = 1)

#----Generating regular grid within that outline----
st.contour <- us[us$NAME_1 %in% states.full,]
st.contour = spTransform(st.contour, CRS("+proj=laea +x_0=0 +y_0=0 +lon_0=-74 +lat_0=40 +units=m"))
grid <- makegrid(st.contour, cellsize = 250000) # cell-size in +units=m
grid <- SpatialPoints(grid, proj4string = CRS(proj4string(st.contour)))
date() ; grid <- grid[st.contour, ] ; date()

#----Cropping the nlcd raster----
grid.r = spTransform(grid, crs=crs(nlcd))
grid.r@bbox = matrix(c(1550000.0, 2550000.0, -434099.8, 722809.3), 2,2, byrow=T) # Extending bbox so all buffers fall on raster
nlcd.crop = crop(nlcd, grid.r)
nlcd.vx <- velox(nlcd.crop)
# I have also tried: nlcd.vx = velox(stack(nlcd.crop))

#----Creating polygon buffers----
spol <- gBuffer(grid.r, width=500, byid=TRUE)
spdf <- SpatialPolygonsDataFrame(spol, data.frame(id=1:length(spol)), FALSE)

#----Velox Extract----
date()
ex.mat <- nlcd.vx$extract(spdf)
ex.mat
date()

Error in boostFactory$makePointGrid(origin = origin, dim = dim, res = res) : 
  std::bad_alloc
mvevans89 commented 5 years ago

@GatesDupont Did you ever solve this issue? I was just rerunning some code that worked previously with a newly updated landcover raster and now have this error and am trouble interpreting what it means.

EDIT: Upon further investigation, this looks to be a memory allocation error. Likely solved by using smaller rasters, fewer in a stack, or running it on a machine with more memory.

GatesDupont commented 5 years ago

@mvevans89 I didn't solve the issue directly. I ended up using a workaround that minced up the land cover raster and ran the velox extraction in parallel across 16 cores. Works great, very fast, but a bit tedious to code.

John-Mola commented 4 years ago

@GatesDupont do you have the code that you ran for velox in parallel? I'm running into the same issue and having trouble implementing it. TY!

GatesDupont commented 4 years ago

Hi @John-Mola – Sure, I just saw your email on your website, sending it there now.

greenLauren commented 4 years ago

@GatesDupont would also be interested in seeing your parallel code, just ran into this issue today

LuMesserschmidt commented 3 years ago

@GatesDupont facing the same issue right now. Although I am working on a 360GB RAM R cloud, velox is producing these errors. Do you mind sharing your code with me as well? Thanks!