-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script to conert tiff to target ntf formats
- Loading branch information
Showing
2 changed files
with
133 additions
and
105 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Copyright 2023 Amazon.com, Inc. or its affiliates. | ||
|
||
""" | ||
################################################################################################### | ||
This Python script provides functionality to convert GeoTIFF images to NITF (National Imagery Transmission Format) | ||
format. The script supports two target NITF formats: greyscale and WIBD (Wavelet Image-Based Data). It uses the GDAL | ||
(Geospatial Data Abstraction Library) library for the conversion process. | ||
The script defines three main functions: 1. `Translate_to_greyscale`: Converts a GeoTIFF dataset to a greyscale NITF | ||
format. 2. `Translate_to_wibd`: Converts a GeoTIFF dataset to a NITF file in the WIBD format. 3. | ||
`Convert_geotiff_to_nitf`: Converts GeoTIFF files in a specified input directory to the desired NITF format (either | ||
greyscale or WIBD) and saves them in an output directory. | ||
The script accepts command-line arguments for specifying input and output directories, target format, inclusion of | ||
XML metadata, and compression method. | ||
Usage: python script_name.py --input_dir input_directory --output_format {GREYSCALE/WIBD} [--output_dir | ||
output_directory] [--include_xml {NO/YES}] [--compression compression_method] | ||
Example Usage: python script_name.py --input_dir input_data --output_format GREYSCALE --output_dir output_data | ||
--include_xml YES --compression LZW | ||
Please ensure you have the GDAL library and its Python bindings (osgeo.gdal) installed before running this script. | ||
You can customize the script's behavior by modifying the function parameters and GDAL options as needed. | ||
###################################################################################################""" | ||
|
||
import argparse | ||
import glob | ||
import os | ||
from enum import Enum, auto | ||
from typing import Optional | ||
|
||
from osgeo import gdal, gdalconst | ||
from osgeo.gdal import Translate | ||
|
||
|
||
class TargetFormat(Enum): | ||
""" | ||
Enum for specifying the target NITF format for conversion. | ||
""" | ||
|
||
GREYSCALE = auto() | ||
WIBD = auto() | ||
|
||
|
||
def translate_to_greyscale(out_file: str, ds: gdal.Dataset) -> gdal.Dataset: | ||
""" | ||
Translate a GeoTIFF dataset to a greyscale NITF format. | ||
:param out_file: The path to the output NITF file to be created. | ||
:param ds: The input GeoTIFF dataset to be translated. | ||
:return: The translated NITF dataset. | ||
""" | ||
return Translate( | ||
destName=out_file, | ||
srcDS=ds, | ||
bandList=[float(1)], | ||
format="NITF", | ||
outputType=gdalconst.GDT_Int16, | ||
creationOptions=["IC=C8", "PROFILE=NPJE_NUMERICALLY_LOSSLESS"], | ||
) | ||
|
||
|
||
def translate_to_wibd(out_file: str, ds: gdal.Dataset) -> gdal.Dataset: | ||
""" | ||
Converts a GeoTIFF dataset to a NITF file in the WIBD format. | ||
:param out_file: The path to the output NITF file to be created. | ||
:param ds: The input GeoTIFF dataset to be converted. | ||
:return: The converted NITF dataset. | ||
""" | ||
return Translate( | ||
destName=out_file, | ||
srcDS=ds, | ||
bandList=[1], | ||
format="NITF", | ||
outputType=gdalconst.GDT_Byte, | ||
creationOptions=["IC=C8", "PROFILE=NPJE_NUMERICALLY_LOSSLESS"], | ||
) | ||
|
||
|
||
def convert_geotiff_to_nitf(input_dir: str, target_format: TargetFormat, output_dir: Optional[str] = None) -> None: | ||
""" | ||
Convert GeoTIFF files in the input directory to NITF format (either greyscale or WIBD). | ||
:param input_dir: The directory containing the input GeoTIFF files. | ||
:param target_format: The target format for conversion, either GREYSCALE or WIBD. | ||
:param output_dir: The directory where converted NITF files will be saved. | ||
If not provided, a default 'output' directory will be created | ||
inside the input directory. | ||
:raises Exception: If an invalid target format is provided. | ||
""" | ||
if output_dir is None: | ||
output_dir = os.path.join(input_dir, "output") | ||
|
||
if not os.path.exists(output_dir): | ||
os.makedirs(output_dir) | ||
|
||
input_files = glob.glob(os.path.join(input_dir, "*.tif")) | ||
|
||
for input_file in input_files: | ||
output_file = os.path.join(output_dir, os.path.splitext(os.path.basename(input_file))[0] + ".ntf") | ||
src_ds = gdal.Open(input_file) | ||
|
||
if target_format == TargetFormat.GREYSCALE: | ||
translate_to_greyscale(output_file, src_ds) | ||
elif target_format == TargetFormat.WIBD: | ||
translate_to_wibd(output_file, src_ds) | ||
else: | ||
raise Exception(f"Invalid target format: {target_format}") | ||
|
||
print("Conversion completed.") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Convert GeoTIFF images to a target NITF format.") | ||
parser.add_argument("input_dir", help="Input directory containing GeoTIFF images") | ||
parser.add_argument("output_format", help="Target format to convert to (options: GREYSCALE/WIBD)") | ||
parser.add_argument("--output_dir", help="Output directory for NITF images (default: input_dir/output)", type=str) | ||
parser.add_argument( | ||
"--include_xml", help="Output directory for NITF images (default: NO, options: NO/YES)", type=bool, default=False | ||
) | ||
parser.add_argument("--compression", help="Output directory for NITF images (default: LZW)", type=str, default="LZW") | ||
args = parser.parse_args() | ||
|
||
gdal.SetConfigOption("COMPRESS_OVERVIEW", args.compression) | ||
gdal.SetConfigOption("GDAL_PAM_ENABLED", args.include_xml) | ||
|
||
usr_input_dir = args.input_dir | ||
usr_output_dir = args.output_dir | ||
|
||
convert_geotiff_to_nitf(usr_input_dir, TargetFormat[args.output_format], usr_output_dir) |