1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ******************************************************************************
# $Id$
#
# Project: GDAL
# Purpose: Simple command line program for copying the color table of a
# raster into another raster.
# Author: Frank Warmerdam, warmerda@home.com
#
# ******************************************************************************
# Copyright (c) 2000, Frank Warmerdam
# Copyright (c) 2020-2021, Idan Miara <idan@miara.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ******************************************************************************
import sys
from typing import Optional
from osgeo import gdal
from osgeo_utils.auxiliary.base import PathLikeOrStr
from osgeo_utils.auxiliary.color_table import ColorTableLike, get_color_table
from osgeo_utils.auxiliary.util import GetOutputDriverFor, open_ds
def Usage():
print("Usage: gdalattachpct.py <pctfile> <infile> <outfile>")
return 2
def main(argv=sys.argv):
if len(argv) < 3:
return Usage()
pct_filename = None
src_filename = None
dst_filename = None
driver_name = None
argv = gdal.GeneralCmdLineProcessor(argv)
if argv is None:
return 0
# Parse command line arguments.
i = 1
while i < len(argv):
arg = argv[i]
if arg == "-of" or arg == "-f":
i = i + 1
driver_name = argv[i]
elif pct_filename is None:
pct_filename = argv[i]
elif src_filename is None:
src_filename = argv[i]
elif dst_filename is None:
dst_filename = argv[i]
else:
return Usage()
i = i + 1
_ds, err = doit(
src_filename=src_filename,
pct_filename=pct_filename,
dst_filename=dst_filename,
driver_name=driver_name,
)
return err
def doit(
src_filename,
pct_filename: Optional[ColorTableLike],
dst_filename: Optional[PathLikeOrStr] = None,
driver_name: Optional[str] = None,
):
# =============================================================================
# Get the PCT.
# =============================================================================
ct = get_color_table(pct_filename)
if pct_filename is not None and ct is None:
print("No color table on file ", pct_filename)
return None, 1
# =============================================================================
# Create a MEM clone of the source file.
# =============================================================================
src_ds = open_ds(src_filename)
mem_ds = gdal.GetDriverByName("MEM").CreateCopy("mem", src_ds)
# =============================================================================
# Assign the color table in memory.
# =============================================================================
mem_ds.GetRasterBand(1).SetRasterColorTable(ct)
mem_ds.GetRasterBand(1).SetRasterColorInterpretation(gdal.GCI_PaletteIndex)
# =============================================================================
# Write the dataset to the output file.
# =============================================================================
if not driver_name:
driver_name = GetOutputDriverFor(dst_filename)
dst_driver = gdal.GetDriverByName(driver_name)
if dst_driver is None:
print('"%s" driver not registered.' % driver_name)
return None, 1
if driver_name.upper() == "MEM":
out_ds = mem_ds
else:
out_ds = dst_driver.CreateCopy(dst_filename or "", mem_ds)
mem_ds = None
src_ds = None
return out_ds, 0
if __name__ == "__main__":
sys.exit(main(sys.argv))
|