2

I have grayscale DEM that I've needed to convert to RGBA in order to play well with how blender addresses alpha channels. I'm using this script to create 2 more bands and then stick my alpha band at the end.

gdal_translate -of GTiff -co -b 1 -b 1 -b 1 -b 2 Filein fileout

It's working great, the only issue is that the file is bloated by about 2 gigs because it now has 2 bands with tons of data that are not used (blender is only pulling data from bands 1 and 4 but still seems to need them there). I'd like to clear out only bands 2 and 3 of all data to keep the files smaller. Any tips? I've looked through all the GDAL documentation and can't see a way to set only one band's values to nodata rather than applying an operation to the whole raster.

Best

2 Answers 2

0

I would rather start by preparing bands that can be combined directly into desired result instead of updating pixels of that 4 band image.

You need

  • band 1 to be used as band 1
  • band 2 to be used as band 4
  • two dummy bands to be used as bands 2 and 3

You know already how to get the first two

gdal_translate -of GTiff -b 1 Filein.tif band1.tif
gdal_translate -of GTiff -b 2 Filein.tif band2.tif

A dummy band can be created with gdal_create utility https://gdal.org/programs/gdal_create.html. Notice that you need GDAL version 3.2 or newer. What you want is an image that takes most features from the source image, but has only 1 band with all pixels having a fixed value, let's say "0".

gdal_create -bands 1 -burn 0 -if Filein.tif dummy.tif

Finally you can combine the ingredients with gdal_merge https://gdal.org/programs/gdal_merge.html. Notice that you must have GDAL Python bindings installed for running gdal_merge because it is a python script. You want to take 4 input files and write them into one GeoTIFF, each input on a separate band.

Notice also that your assumption that updating a whole band into nodata would automatically save space. It does not because in an uncompressed TIFF every pixels requires the same amount of space. Fortunately a band with all equal pixels is compressing extremely well even with lossless compression methods like "deflate". Thus the gdal_merge command would look as follows. Notice that the dummy TIFF is selected twice.

gdal_merge -o 4bandtest.tif -of GTiff -co compress=deflate -co tiled=yes -separate b1.tif dummy.tif dummy.tif b2.tif

Check with gdalinfo proves that the result has 4 bands and bands 2 and 3 have just zeroes.

gdalinfo 4bandtest.tif -stats
Driver: GTiff/GeoTIFF
Files: 4bandtest.tif
...
Band 1 Block=256x256 Type=Byte, ColorInterp=Red
  Minimum=0.000, Maximum=255.000, Mean=63.804, StdDev=31.731
  Mask Flags: PER_DATASET ALPHA
  Metadata:
    STATISTICS_MAXIMUM=255
    STATISTICS_MEAN=63.803541618056
    STATISTICS_MINIMUM=0
    STATISTICS_STDDEV=31.731235967103
    STATISTICS_VALID_PERCENT=100
Band 2 Block=256x256 Type=Byte, ColorInterp=Green
  Minimum=0.000, Maximum=0.000, Mean=0.000, StdDev=0.000
  Mask Flags: PER_DATASET ALPHA
  Metadata:
    STATISTICS_MAXIMUM=0
    STATISTICS_MEAN=0
    STATISTICS_MINIMUM=0
    STATISTICS_STDDEV=0
    STATISTICS_VALID_PERCENT=100
Band 3 Block=256x256 Type=Byte, ColorInterp=Blue
  Minimum=0.000, Maximum=0.000, Mean=0.000, StdDev=0.000
  Mask Flags: PER_DATASET ALPHA
  Metadata:
    STATISTICS_MAXIMUM=0
    STATISTICS_MEAN=0
    STATISTICS_MINIMUM=0
    STATISTICS_STDDEV=0
    STATISTICS_VALID_PERCENT=100
Band 4 Block=256x256 Type=Byte, ColorInterp=Alpha
  Minimum=0.000, Maximum=255.000, Mean=68.127, StdDev=32.036
  Metadata:
    STATISTICS_MAXIMUM=255
    STATISTICS_MEAN=68.126588152778
    STATISTICS_MINIMUM=0
    STATISTICS_STDDEV=32.036317455231
    STATISTICS_VALID_PERCENT=100
0

Another approach than to prepare the 4 bands as ready to use is to update the pixel values of an existing GeoTIFF with gdal_rasterize https://gdal.org/programs/gdal_rasterize.html.

For updating a whole band user must have a polygon geometry that totally covers the image. Such polygon can be created for example with QGIS be digitizing and saving the result into GeoTIFF or any other vector format.

Usage:

gdal_rasterize -b 2 -b 3 -burn 0 big_polygon.json 4bandimage.tif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.