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.
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.
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?
Maybe try LERC compression?
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