Skip to content

Commit

Permalink
Add an alpha layer to GeoTIFF raster data
Browse files Browse the repository at this point in the history
Note, an alpha layer is always added (to single and 3 band GeoTIFF
files) since gdalwarp doesn't add a new alpha band if one is already
present.

Fixes #570
  • Loading branch information
edsu committed Jan 26, 2024
1 parent 80fa1c3 commit fd88301
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/robots/dor_repo/gis_assembly/normalize_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def normalize
# if using 8-bit color palette, convert into RGB
convert_8bit_to_rgb if eight_bit?

add_alpha_channel

compute_statistics
end

Expand Down Expand Up @@ -270,6 +272,17 @@ def eight_bit?
false
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.
logger.info "normalize-data: adding alpha channel for #{output_filepath}"
temp_filepath = "#{output_dir}/#{geo_object_name}_alpha.tif"
system_with_check("#{Settings.gdal_path}gdalwarp -dstalpha #{output_filepath} #{temp_filepath}")
FileUtils.mv(temp_filepath, output_filepath)
end

def compute_statistics
system_with_check("#{Settings.gdal_path}gdalinfo -mm -stats -norat -noct #{output_filepath}")
end
Expand Down
4 changes: 4 additions & 0 deletions spec/robots/dor_repo/gis_assembly/normalize_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@
expect(Kernel).to have_received(:system).with(
"gdal_translate -expand rgb /tmp/normalize_bb021mm7809/raw8bit.tif /tmp/normalize_bb021mm7809/EPSG_4326/MCE_FI2G_2014.tif -co 'COMPRESS=LZW'"
)
# Adds an alpha channel
expect(Kernel).to have_received(:system).with(
'gdalwarp -dstalpha /tmp/normalize_bb021mm7809/EPSG_4326/MCE_FI2G_2014.tif /tmp/normalize_bb021mm7809/EPSG_4326/MCE_FI2G_2014_alpha.tif'
)
# Stats
expect(Kernel).to have_received(:system).with(
'gdalinfo -mm -stats -norat -noct /tmp/normalize_bb021mm7809/EPSG_4326/MCE_FI2G_2014.tif'
Expand Down

0 comments on commit fd88301

Please sign in to comment.