Last active
March 21, 2025 19:02
-
-
Save wiesehahn/7e865a04a8d8646944fd13ffc1f483de to your computer and use it in GitHub Desktop.
Compare image compression algorithms regarding size and performance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pak::pak("USDAForestService/gdalraster") | |
library(gdalraster) | |
library(purrr) | |
library(fs) | |
# sset gdal configurations | |
set_config_option("GDAL_NUM_THREADS", "16") | |
set_config_option("GDAL_CACHEMAX", "4000") | |
set_config_option("OVERVIEWS", "IGNORE_EXISTING") | |
# get orthoimages from LGLN open data | |
urls <- c( | |
"https://dop20-rgbi.s3.eu-de.cloud-object-storage.appdomain.cloud/324905842/2024-09-05/dop20rgbi_32_490_5842_2_ni_2024-09-05.tif", | |
"https://dop20-rgbi.s3.eu-de.cloud-object-storage.appdomain.cloud/326085740/2022-05-09/dop20rgbi_32_608_5740_2_ni_2022-05-09.tif", | |
"https://dop20-rgbi.s3.eu-de.cloud-object-storage.appdomain.cloud/326085732/2022-05-09/dop20rgbi_32_608_5732_2_ni_2022-05-09.tif", | |
"https://dop20-rgbi.s3.eu-de.cloud-object-storage.appdomain.cloud/326125846/2024-09-21/dop20rgbi_32_612_5846_2_ni_2024-09-21.tif", | |
"https://dop20-rgbi.s3.eu-de.cloud-object-storage.appdomain.cloud/326045852/2024-09-21/dop20rgbi_32_604_5852_2_ni_2024-09-21.tif") | |
dsns <- paste0("/vsicurl/", urls) # prefix for virtual file source | |
# convert list of image paths to gdalraster objects | |
dss <- dsns |> | |
map(\(datasourcename) new(GDALRaster, datasourcename, read_only = TRUE)) | |
# list compression types for images | |
compression <- dss |> | |
map(\(datasource) datasource$getMetadataItem(band = 0, mdi_name = "COMPRESSION", domain = "IMAGE_STRUCTURE")) | |
# list image sizes | |
size <- dsns |> | |
map(\(datasourcename) utils:::format.object_size(vsi_stat(datasourcename, "size"), "auto")) | |
# Create a function to measure performance for each datasource and compression | |
raster_compression <- function(dsns, options) { | |
results <- list() | |
for (datasourcename in dsns) { | |
for (option in options) { | |
# Start timer | |
start_time <- Sys.time() | |
output_file <- file_temp(ext = ".tif") | |
temp_raster <- createCopy( | |
src_filename = datasourcename, | |
dst_filename = output_file, | |
format = "COG", | |
options = option$setting | |
) | |
# End timer | |
end_time <- Sys.time() | |
runtime <- as.numeric(difftime(end_time, start_time, units = "secs")) | |
# Get file size in MB | |
data_size_uncompressed <- vsi_stat(datasourcename, "size") / (1024^2) | |
data_size_compressed <- vsi_stat(output_file, "size") / (1024^2) | |
# Store results | |
results[[length(results) + 1]] <- list( | |
datasource = basename(datasourcename), | |
settings = option$naming, | |
uncompressed_mb = data_size_uncompressed, | |
compressed_mb = data_size_compressed, | |
runtime_sec = runtime | |
) | |
} | |
} | |
# Convert list to data frame | |
results_df <- do.call(rbind, lapply(results, data.frame)) | |
return(results_df) | |
} | |
# create list of options | |
options <- list( | |
# lossless | |
list(setting = c("COMPRESS=NONE")), | |
list(setting = c("COMPRESS=LZW", "PREDICTOR=YES")), | |
list(setting = c("COMPRESS=LZW", "PREDICTOR=NO")), | |
list(setting = c("COMPRESS=DEFLATE", "PREDICTOR=YES", "LEVEL=1")), | |
list(setting = c("COMPRESS=DEFLATE", "PREDICTOR=YES", "LEVEL=6")), | |
list(setting = c("COMPRESS=DEFLATE", "PREDICTOR=YES", "LEVEL=9")), | |
list(setting = c("COMPRESS=DEFLATE", "PREDICTOR=NO", "LEVEL=1")), | |
list(setting = c("COMPRESS=DEFLATE", "PREDICTOR=NO", "LEVEL=6")), | |
list(setting = c("COMPRESS=DEFLATE", "PREDICTOR=NO", "LEVEL=9")), | |
list(setting = c("COMPRESS=LZMA", "LEVEL=1")), | |
list(setting = c("COMPRESS=LZMA", "LEVEL=9")), | |
list(setting = c("COMPRESS=ZSTD", "PREDICTOR=YES", "LEVEL=1")), | |
list(setting = c("COMPRESS=ZSTD", "PREDICTOR=YES", "LEVEL=9")), | |
list(setting = c("COMPRESS=ZSTD", "PREDICTOR=YES", "LEVEL=22")), | |
list(setting = c("COMPRESS=ZSTD", "PREDICTOR=NO", "LEVEL=1")), | |
list(setting = c("COMPRESS=ZSTD", "PREDICTOR=NO", "LEVEL=9")), | |
list(setting = c("COMPRESS=ZSTD", "PREDICTOR=NO", "LEVEL=22")), | |
list(setting = c("COMPRESS=WEBP", "QUALITY=100")), | |
list(setting = c("COMPRESS=LERC", "MAX_Z_ERROR=0")), | |
list(setting = c("COMPRESS=LERC_DEFLATE", "LEVEL=1")), | |
list(setting = c("COMPRESS=LERC_DEFLATE", "LEVEL=6")), | |
list(setting = c("COMPRESS=LERC_DEFLATE", "LEVEL=9")), | |
list(setting = c("COMPRESS=LERC_ZSTD", "LEVEL=1")), | |
list(setting = c("COMPRESS=LERC_ZSTD", "LEVEL=9")), | |
list(setting = c("COMPRESS=LERC_ZSTD", "LEVEL=22")), | |
# lossy overviews | |
list(setting = c("COMPRESS=WEBP", "QUALITY=100", "OVERVIEW_QUALITY=75")), | |
# lossy | |
list(setting = c("COMPRESS=WEBP", "QUALITY=75")) | |
) | |
for (i in 1:length(options)) { | |
options[[i]]$naming <- paste(options[[i]]$setting, collapse = ", ") | |
} | |
# apply function on list | |
benchmark_results <- raster_compression(dsns, options) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results for Orthophotos
Single-threaded (
GDAL_NUM_THREADS=DEFAULT
,GDAL_CacheMAX=4000
)Multi-threaded (
GDAL_NUM_THREADS=32
,GDAL_CacheMAX=8000
)