Skip to content

Commit

Permalink
add raster saving test script
Browse files Browse the repository at this point in the history
  • Loading branch information
blenback committed Mar 5, 2024
1 parent 4213e2f commit f1131b8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
^\.github$
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions NASCENT-Peru.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ Encoding: UTF-8

RnwWeave: Sweave
LaTeX: XeLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
56 changes: 56 additions & 0 deletions src/scripts/preparation/test_rast_save_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
install.packages("terra")
library(terra)

#load test rast
test_rast <- terra::rast("X:/NASCENT-Peru/03_workspaces/02_modelling/Data_collab/Preds/Prepared/Suitability/Biophysical/Bulk_density/bdod_5-15cm_mean.tif")

#check min/max values
minmax(test_rast)

#test if rats is integer
is.int(test_rast)

### =========================================================================
### Convert to integer
### =========================================================================

#multiple by 1000 to convert to integer
test_rast_int <- test_rast * 10000

#check min/max values
minmax(test_rast_int)

#test if rats is integer
is.int(test_rast_int)

### =========================================================================
### Check size using Geotiff
### =========================================================================

#save using INT2U datatype
writeRaster(test_rast,
datatype= "INT2U",
filename = "X:/NASCENT-Peru/03_workspaces/02_modelling/Data_collab/Preds/Prepared/Suitability/Biophysical/Bulk_density/test_int_rast.tif")


#re-read
test_int2U <- rast("X:/NASCENT-Peru/03_workspaces/02_modelling/Data_collab/Preds/Prepared/Suitability/Biophysical/Bulk_density/test_int_rast.tif")
is.int(test_int2U)

#attempt to force rast values into memory
test_rds <- test_int2U *1

### =========================================================================
### Check size using RDS
### =========================================================================

# Convert back to raster as terra::saveRDS only saves reference
test_rds <- raster::raster(test_int2U)

# Use raster::readall to ensure values are in memory
# THIS FAILS BECAUSE OF RAM LIMITS
test_rds <- raster::readAll(test_rds)

#save as RDS instead
terra::saveRDS(test_rds, "X:/NASCENT-Peru/03_workspaces/02_modelling/Data_collab/Preds/Prepared/Suitability/Biophysical/Bulk_density/test_rast.rds")

0 comments on commit f1131b8

Please sign in to comment.