Question

How do I convert a Raster file to COG (Cloud optimised Geotiff) with 4 bands (R,G,B,Alpha)?

  • 22 April 2024
  • 5 replies
  • 87 views

Badge +5

I want to convert JPEG file (4 band) to COG with below parameter:

I have problem with “Interleaving Type”:

When i chooses “BIP”, i received a message log:

COG writer: JPEG compression supports at most 3 bands with BIP interleaving. Please change the interleaving to BSQ, choose another compression method, or reduce the number of bands

When i chooses “BSQ”, i received a message log:

COG writer: The file layout 'Cloud Optimized Tiles' may only be used with BIP interleaving. Please change the interleaving to BIP or select a different file layout

I need to convert JPEG file to COG with compression method “JPEG” and keep alpha band.

Can i do that? Am I mistaken about something?


5 replies

Userlevel 4
Badge +18

I am using FME 2021 and COG was not yet an option in that version.

So I downloaded GDAL COG library and do these conversions with that library, within FME.

Userlevel 4
Badge +18
import fme
import fmeobjects
import subprocess
import os
import mimetypes

class FeatureProcessor(object):
def __init__(self):
print("Initialise Python GDAL Conversion Library")
# Path to folder containing GDAL
gdal_folder = FME_MacroValues['GdalDir']
os.environ["GDAL_DATA"] = os.path.join(gdal_folder, 'data')
self.gdal_translate = os.path.join(gdal_folder, "bin", "gdal_translate.exe")

options_cog_with_alpha = ["-of","COG" ,
"-co","BIGTIFF=YES",
"-co","COMPRESS=JPEG",
"-co","QUALITY=90",
"-co","BLOCKSIZE=512" ,
"-co","OVERVIEWS=IGNORE_EXISTING"]

def input(self, feature):
print("Start Feature Conversion with GDAL to COG")

file_dir = feature.getAttribute("LocalWorkDir")
input_filename = feature.getAttribute("LocalFileName")
output_folder = 'Converted'
output_filename = feature.getAttribute("NewLocalFileName") + ".tif"

input_file = os.path.join(file_dir, input_filename)
output_file = os.path.join(file_dir, output_folder, output_filename)

# Check if output directory exits, else create it
if not os.path.exists(os.path.join(file_dir, output_folder)):
print("Creating directory:", os.path.join(file_dir, output_folder))
os.makedirs(os.path.join(file_dir, output_folder))

print("Converting file:", input_file)
print("to location:", output_file)

# Run GDAL Translate to convert to COG
subprocess.run([self.gdal_translate] + self.options_cog_with_alpha + [input_file, output_file])

# Set MIME type attribute for the output file
feature.setAttribute("ContentType", "image/tiff")

print("Conversion to COG completed.")
self.pyoutput(feature)

def close(self):
pass

I had AI strip the code to the bare basics. But this is an example of using the External GDAL Lib.

At start I used the GDAL Python module, but that did not have the functions I wanted to use.

Badge +5
import fme
import fmeobjects
import subprocess
import os
import mimetypes

class FeatureProcessor(object):
def __init__(self):
print("Initialise Python GDAL Conversion Library")
# Path to folder containing GDAL
gdal_folder = FME_MacroValues['GdalDir']
os.environ["GDAL_DATA"] = os.path.join(gdal_folder, 'data')
self.gdal_translate = os.path.join(gdal_folder, "bin", "gdal_translate.exe")

options_cog_with_alpha = ["-of","COG" ,
"-co","BIGTIFF=YES",
"-co","COMPRESS=JPEG",
"-co","QUALITY=90",
"-co","BLOCKSIZE=512" ,
"-co","OVERVIEWS=IGNORE_EXISTING"]

def input(self, feature):
print("Start Feature Conversion with GDAL to COG")

file_dir = feature.getAttribute("LocalWorkDir")
input_filename = feature.getAttribute("LocalFileName")
output_folder = 'Converted'
output_filename = feature.getAttribute("NewLocalFileName") + ".tif"

input_file = os.path.join(file_dir, input_filename)
output_file = os.path.join(file_dir, output_folder, output_filename)

# Check if output directory exits, else create it
if not os.path.exists(os.path.join(file_dir, output_folder)):
print("Creating directory:", os.path.join(file_dir, output_folder))
os.makedirs(os.path.join(file_dir, output_folder))

print("Converting file:", input_file)
print("to location:", output_file)

# Run GDAL Translate to convert to COG
subprocess.run([self.gdal_translate] + self.options_cog_with_alpha + [input_file, output_file])

# Set MIME type attribute for the output file
feature.setAttribute("ContentType", "image/tiff")

print("Conversion to COG completed.")
self.pyoutput(feature)

def close(self):
pass

I had AI strip the code to the bare basics. But this is an example of using the External GDAL Lib.

At start I used the GDAL Python module, but that did not have the functions I wanted to use.

Thanks for your response.

I have tried to use your code and I also tried to use my code with GDAL lib, but both of them don’t work as expected. It just has 3 bands without Alpha band.

Perhaps COG doesn’t have the function for converting alpha band with compression JPEG.

I want to use COG on my geoserver to remove the black border after georeferencing RASTER with the smallest size.

Do you have any suggestions for me?

Userlevel 4
Badge +18

Maybe try LERC compression?

Badge +5

Maybe try LERC compression?

I tried all of compression and I found that JPEG is the best compression in my case.

All remaining methods produce results with large capacity

Reply