Skip to content

Commit

Permalink
NormalizeData#add_alpha_channel: use compression option when calling …
Browse files Browse the repository at this point in the history
…gdalwarp

ref #789
ref #802

uses a more circuitous way of adding alpha channel (gdalwarp to add alpha channel and output to vrt, then pipe that to gdal_translate for compression).  this provides better compression and faster computation than solely using gdalwarp.
  • Loading branch information
jmartin-sul committed Feb 21, 2024
1 parent 2b9a4cd commit 5d4a9e8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/gis_robot_suite/raster_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def normalize

epsg4326_projection? ? compress_only : reproject_and_compress
convert_8bit_to_rgb if eight_bit?
add_alpha_channel
compute_statistics
tmpdir
end
Expand Down Expand Up @@ -77,6 +78,28 @@ def convert_8bit_to_rgb
File.delete(temp_filename)
end

def add_alpha_channel
# NOTE: gdalwarp is smart enough not to add a new alpha channel (band) if one is already there.
# If we want to improve the performance of the normalize step, and many GeoTIFFs already
# have alpha channels, then we could introspect on the GeoTIFF file with gdalinfo and skip
# this call to gdalwarp if one is already present.

# NOTE: the roundabout approach of outputting the alpha channel add to a vrt, and piping that
# gdalwarp result to gdal_translate to create an actual compressed tif is an attempt to workaround
# a known issue with the compression option being provided directly to gdalwarp. See:
# https://trac.osgeo.org/gdal/wiki/UserDocs/GdalWarp#GeoTIFFoutput-coCOMPRESSisbroken
# https://gdal.org/programs/gdalwarp.html#compressed-output
# https://gdal.org/user/virtual_file_systems.html#vsistdout-standard-output-streaming
# https://gis.stackexchange.com/questions/89444/file-size-inflation-normal-with-gdalwarp

logger.info "load-raster: adding alpha channel for #{output_filepath}"
temp_filepath = "#{tmpdir}/#{geo_object_name}_alpha.tif"
gdalwarp_cmd = "#{Settings.gdal_path}gdalwarp -dstalpha -of vrt #{output_filepath} /vsistdout/"
gdal_translate_cmd = "#{Settings.gdal_path}gdal_translate -co 'compress=LZW' /vsistdin/ #{temp_filepath}"
Kernel.system("#{gdalwarp_cmd} | #{gdal_translate_cmd}", exception: true)
FileUtils.mv(temp_filepath, output_filepath)
end

def compute_statistics
Kernel.system("#{Settings.gdal_path}gdalinfo -mm -stats -norat -noct #{output_filepath}", exception: true)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/gis_robot_suite/raster_normalizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"gdal_translate -expand rgb /tmp/normalizeraster_bb021mm7809/raw8bit.tif /tmp/normalizeraster_bb021mm7809/MCE_FI2G_2014.tif -co 'COMPRESS=LZW'",
exception: true
)
# Adds an alpha channel
expect(Kernel).to have_received(:system).with(
"gdalwarp -dstalpha -of vrt /tmp/normalizeraster_bb021mm7809/MCE_FI2G_2014.tif /vsistdout/ | gdal_translate -co 'compress=LZW' /vsistdin/ /tmp/normalizeraster_bb021mm7809/MCE_FI2G_2014_alpha.tif", # rubocop:disable Layout/LineLength
exception: true
)
# Stats
expect(Kernel).to have_received(:system).with(
'gdalinfo -mm -stats -norat -noct /tmp/normalizeraster_bb021mm7809/MCE_FI2G_2014.tif',
Expand Down

0 comments on commit 5d4a9e8

Please sign in to comment.