I wrote some R codes to manually create the properties files which is generated by geoserver (actually by GeoTools). The codes are not perfect, but work for me. I post here for other who will need them in R.
geoserver_retile <- function(
input_file,
target_dir,
levels = 8)
{
# Call gdal_retile to generate tiles
cmd <- paste0(
'gdal_retile.py -v -r bilinear -levels ', levels
, ' -ps 256 256 -co "TILED=YES" -co "COMPRESS=LZW" '
, '-tileIndex tmp_tile_index '
, ' -targetDir ', target_dir
, ' ', input_file)
system(cmd)
dir.create(file.path(target_dir, '0'))
# Create the zero level files
zero_files <- list.files(target_dir, '*.tif', full.names = TRUE)
file.rename(
zero_files
, file.path(target_dir, '0', basename(zero_files)))
# Change file name of shapefile
shp_files <- list.files(target_dir, 'tmp_tile_index*', full.names = TRUE)
file.rename(
shp_files
, file.path(target_dir, '0', basename(shp_files)))
shp_files <- list.files(
target_dir, 'tmp_tile_index*'
, full.names = TRUE, recursive = TRUE)
idx <- gsub('.*/(\\d+)/.*', '\\1', shp_files)
shp_files_new <- unlist(lapply(seq(along = shp_files)
, function(x) gsub('tmp_tile_index', idx[x], shp_files[x])))
file.rename(shp_files, shp_files_new)
# Write properities
library(rgdal)
options(digits = 6)
library(raster)
input_raster <- raster(input_files)
writeLines(showWKT(proj4string(input_raster)),
file.path(target_dir,
paste0(basename(target_dir), '.prj')))
input_res <- res(input_raster)
for (i in seq(0, levels)) {
# i <- 0
file_name <- file.path(target_dir, i,
paste0(i, '.properties'))
prop <- sprintf('Levels=%s,%s
Heterogeneous=false
AbsolutePath=false
Name=%s
TypeName=%s
Caching=false
ExpandToRGB=false
LocationAttribute=location
SuggestedSPI=it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi
CheckAuxiliaryMetadata=false
LevelsNum=1', input_res[1] * 2^i, input_res[2] * 2^i, i, i)
writeLines(prop, file_name)
}
file_name <- file.path(target_dir,
paste0(basename(target_dir), '.properties'))
input_extend <- extent(input_raster)
prop <- sprintf('Name=%s
Levels=%s
LevelsNum=%s
Envelope2D=%s
LevelsDirs=%s',
basename(target_dir)
, paste(paste0(input_res[1] * 2 ^ seq(0, levels), ',', input_res[2] * 2 ^ seq(0, levels)), collapse = ' ')
, levels + 1
, paste0(input_extend[1], ',', input_extend[3], ' ', input_extend[2], ',', input_extend[4])
, paste(seq(0, levels), collapse = ' '))
writeLines(prop, file_name)
}