Issue with using Raster::reclassify with CropscapeR package data in Linux

Issue

I am using the package CropscapeR to get Cropland Data Layer (CDL) rasters. However, when I try to mask this raster using reclassify I sometimes run into issues. Since I am a Linux user (for R and OS specs see below), I have to do a distinct process to access the data than windows/mac users which involves the package httr and a tif file. This is the standard process recommended by the CropscapeR creator. If I then run raster::reclassify in my data, I have no problems. However, if I save the my RData and then reopen/restart R, and try to run raster::reclassify, it does not work. In particular, I get the following error message: Error: C stack usage 7977940 is too close to the limit. Also, I have a bunch of warnings of the sort:

    1: In new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
    GDAL Error 4: /tmp/Rtmpf7yIsG/file28f21b32987a.tif: File or folder does not exist

Here is the code I am trying to run:

library(CropScapeR)    
library(httr)    
library(raster)    
library(sf)    

#Skip the SSL check    
httr::set_config(httr::config(ssl_verifypeer = 0L))    
#Automatically generate a temporary path to save the data    
tif_file <- tempfile(fileext = '.tif')    
#Download the raster TIF file into specified path, also read into R     
ST_CDL <- GetCDLData(aoi = '34007', year = 2021, type = 'f', save_path = tif_file)    

#The output file ST_CDL I get is a Formal class RasterLayer.    
#This is the way I am trying to mask the values of the raster I do not want.    
#Notice, again, that this code runs if I do it in sequence, I only get into    
#trouble if I first get ST_CDL, then save it as RData, reopen/restart R and     
#then try to run the code below directly without getting ST_CDL again through    
#the above process.

ST_CDL_blueberries <- 
  raster::reclassify(ST_CDL,
                     c(-0.1,241.9,NA,
                       242.1,255.1,NA),
                     progress="text")

My specs:

  • Ubuntu 22.04.1 LTS
  • R 4.2.1 (2022-06-23) — "Funny-Looking Kid"
  • RStudio 2022.07.2+576 "Spotted Wakerobin" Release (e7373ef832b49b2a9b88162cfe7eac5f22c40b34, 2022-09-06) for Ubuntu Jammy Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36
  • CropScapeR 1.1.4
  • httr 1.4.4
  • raster 3.6-3

Thanks for your help!

I got my data, saved it, restarted R and tried to run the code from there (without getting the data again). Supposedly this should work, but it did not.

Solution

I managed to solve the problem by writing my raster. Namely, I did the following:

library(CropScapeR)    
library(httr)    
library(raster)    
library(sf)    

#Skip the SSL check    
httr::set_config(httr::config(ssl_verifypeer = 0L))    
#Automatically generate a temporary path to save the data    
tif_file <- tempfile(fileext = '.tif')    
#Download the raster TIF file into specified path, also read into R     
ST_CDL <- GetCDLData(aoi = '34007', year = 2021, type = 'f', save_path = tif_file)
#Now I write and save the raster, so it is available next time I open R
writeRaster(ST_CDL, "ST_CDL.tif", overwrite=TRUE)
ST_CDL = raster("ST_CDL.tif")  

ST_CDL_blueberries <- raster::reclassify(ST_CDL,
                 c(-0.1,241.9,NA,
                   242.1,255.1,NA),
                 progress="text")

Answered By – RobertoAS

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published