diff --git a/autotest/pyscripts/test_gdal_utils.py b/autotest/pyscripts/test_gdal_utils.py index 7a85a0421ae3..855e8db048e6 100644 --- a/autotest/pyscripts/test_gdal_utils.py +++ b/autotest/pyscripts/test_gdal_utils.py @@ -42,11 +42,17 @@ pytest.importorskip('osgeo_utils') +<<<<<<< HEAD from osgeo_utils.auxiliary import util, raster_creation, base, array_util, color_table from osgeo_utils.auxiliary.color_palette import ColorPalette from osgeo_utils.auxiliary.color_table import get_color_table from osgeo_utils.auxiliary.extent_util import Extent import gdaltest +======= +from osgeo_utils.auxiliary import util, raster_creation, base, array_util +from osgeo_utils.auxiliary.color_palette import ColorPalette +from osgeo_utils.auxiliary.extent_util import Extent +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal) temp_files = [] @@ -165,6 +171,7 @@ def test_utils_np_arrays(): assert isinstance(arr, array_util.ArrayLike.__args__) +<<<<<<< HEAD @pytest.mark.parametrize("name,count,pal", [['color_paletted_red_green_0-255.qml', 256, {0: 0x00ffffff, 1: 0xFF808080}], ['color_pseudocolor_spectral_0-100.qml', 5, {0: 0xFFD7191C, 25: 0xFFFFFFBF}]]) @@ -264,6 +271,28 @@ def test_read_write_color_table_from_raster(): ds = None gdaltest.tiff_drv.Delete('tmp/ct8.tif') +======= +def test_utils_color_files(): + """ test color palettes: read QML and TXT files """ + items = [ + dict(name='color_paletted_red_green_0-255.qml', count=256, pal={0: 0x00ffffff, 1: 0xFF808080}), + dict(name='colro_pseudocolor_spectral_0-100.qml', count=5, pal={0: 0xFFD7191C, 25: 0xFFFFFFBF}), + ] + root = Path(test_py_scripts.get_data_path('utilities')) + for item in items: + path = root / item['name'] + path2 = path.with_suffix('.txt') + cp1 = ColorPalette() + cp2 = ColorPalette() + cp1.read_file(path) + # cp1.write_file(path2) + cp2.read_file(path2) + assert cp1 == cp2 + assert len(cp1.pal) == item['count'] + for k, v in item['pal'].items(): + # compare the first values against the hard-coded test sample + assert cp1.pal[k] == v +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal) def test_utils_py_cleanup(): diff --git a/autotest/utilities/data/colro_pseudocolor_spectral_0-100.qml b/autotest/utilities/data/colro_pseudocolor_spectral_0-100.qml new file mode 100644 index 000000000000..8e2a50ed70ee --- /dev/null +++ b/autotest/utilities/data/colro_pseudocolor_spectral_0-100.qml @@ -0,0 +1,78 @@ + + + + 1 + 1 + 1 + 0 + + + + + + + + + + + + + + + + + + + + + None + WholeRaster + Estimated + 0.02 + 0.98 + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + resamplingFilter + + 0 + diff --git a/autotest/utilities/data/colro_pseudocolor_spectral_0-100.txt b/autotest/utilities/data/colro_pseudocolor_spectral_0-100.txt new file mode 100644 index 000000000000..981b307cb7e0 --- /dev/null +++ b/autotest/utilities/data/colro_pseudocolor_spectral_0-100.txt @@ -0,0 +1,5 @@ +0 215 25 28 +25 255 255 191 +50 253 174 97 +75 171 221 164 +100 43 131 186 diff --git a/frmts/nitf/nitfdataset.cpp b/frmts/nitf/nitfdataset.cpp index e590d8e1bd69..372f0644800f 100644 --- a/frmts/nitf/nitfdataset.cpp +++ b/frmts/nitf/nitfdataset.cpp @@ -68,6 +68,7 @@ static bool NITFPatchImageLength( const char *pszFilename, GUIntBig nImageOffset, GIntBig nPixelCount, const char *pszIC, +<<<<<<< HEAD:frmts/nitf/nitfdataset.cpp <<<<<<< HEAD:frmts/nitf/nitfdataset.cpp vsi_l_offset nICOffset, CSLConstList papszCreationOptions ); @@ -76,6 +77,9 @@ static bool NITFWriteExtraSegments( const char *pszFilename, CSLConstList papszTextMD, CSLConstList papszOptions ); ======= +======= + int nICOffset, +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp CSLConstList papszCreationOptions ); static bool NITFWriteCGMSegments( const char *pszFilename, char **papszList ); static bool NITFWriteTextSegments( const char *pszFilename, char **papszList ); @@ -212,8 +216,12 @@ int NITFDataset::CloseDependentDatasets() "C8", m_nICOffset, nullptr )); ======= NITFPatchImageLength( GetDescription(), nImageStart, nPixelCount, +<<<<<<< HEAD:frmts/nitf/nitfdataset.cpp "C8", nullptr )); >>>>>>> 33d99249ed (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp +======= + "C8", m_nICOffset, nullptr )); +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp } bJP2Writing = FALSE; @@ -4199,6 +4207,7 @@ NITFDataset::NITFDatasetCreate( const char *pszFilename, int nXSize, int nYSize, /* Create the file. */ /* -------------------------------------------------------------------- */ +<<<<<<< HEAD:frmts/nitf/nitfdataset.cpp int nIMIndex = 0; int nImageCount = 0; vsi_l_offset nImageOffset = 0; @@ -4207,6 +4216,12 @@ NITFDataset::NITFDatasetCreate( const char *pszFilename, int nXSize, int nYSize, GDALGetDataTypeSize( eType ), pszPVType, papszFullOptions, &nIMIndex, &nImageCount, &nImageOffset, &nICOffset ) ) +======= + int nICOffset = 0; + if( !NITFCreateEx( pszFilename, nXSize, nYSize, nBands, + GDALGetDataTypeSize( eType ), pszPVType, + papszFullOptions, &nICOffset ) ) +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp { CSLDestroy(papszTextMD); CSLDestroy(papszCgmMD); @@ -4248,9 +4263,12 @@ NITFDataset::NITFDatasetCreate( const char *pszFilename, int nXSize, int nYSize, NITFDataset::OpenInternal(&oOpenInfo, poWritableJ2KDataset, true, nIMIndex); if (poDS) { +<<<<<<< HEAD:frmts/nitf/nitfdataset.cpp poDS->m_nImageOffset = nImageOffset; poDS->m_nIMIndex = nIMIndex; poDS->m_nImageCount = nImageCount; +======= +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp poDS->m_nICOffset = nICOffset; poDS->papszTextMDToWrite = papszTextMD; poDS->papszCgmMDToWrite = papszCgmMD; @@ -5027,6 +5045,7 @@ NITFDataset::NITFCreateCopy( } } +<<<<<<< HEAD:frmts/nitf/nitfdataset.cpp int nIMIndex = 0; int nImageCount = 0; vsi_l_offset nImageOffset = 0; @@ -5035,6 +5054,12 @@ NITFDataset::NITFCreateCopy( GDALGetDataTypeSize( eType ), pszPVType, papszFullOptions, &nIMIndex, &nImageCount, &nImageOffset, &nICOffset ) ) +======= + int nICOffset = 0; + if (!NITFCreateEx( pszFilename, nXSize, nYSize, poSrcDS->GetRasterCount(), + GDALGetDataTypeSize( eType ), pszPVType, + papszFullOptions, &nICOffset )) +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp { CSLDestroy( papszFullOptions ); CSLDestroy(papszCgmMD); @@ -5122,7 +5147,7 @@ NITFDataset::NITFCreateCopy( } ======= bool bOK = NITFPatchImageLength( pszFilename, nImageOffset, nPixelCount, - "C8", papszFullOptions ); + "C8", nICOffset, papszFullOptions ); bOK &= NITFWriteCGMSegments( pszFilename, papszCgmMD ); bOK &= NITFWriteTextSegments( pszFilename, papszTextMD ); >>>>>>> 33d99249ed (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp @@ -5197,7 +5222,7 @@ NITFDataset::NITFCreateCopy( } ======= bool bOK = NITFPatchImageLength( pszFilename, nImageOffset, - nPixelCount, pszIC, papszFullOptions ); + nPixelCount, pszIC, nICOffset, papszFullOptions ); bOK &= NITFWriteCGMSegments( pszFilename, papszCgmMD ); bOK &= NITFWriteTextSegments( pszFilename, papszTextMD ); @@ -5414,10 +5439,14 @@ static bool NITFPatchImageLength( const char *pszFilename, GUIntBig nImageOffset, GIntBig nPixelCount, const char *pszIC, +<<<<<<< HEAD:frmts/nitf/nitfdataset.cpp <<<<<<< HEAD:frmts/nitf/nitfdataset.cpp vsi_l_offset nICOffset, ======= >>>>>>> 33d99249ed (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp +======= + int nICOffset, +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.cpp CSLConstList papszCreationOptions ) { diff --git a/frmts/nitf/nitfdataset.h b/frmts/nitf/nitfdataset.h index d791882fa58e..ef3eec246daf 100644 --- a/frmts/nitf/nitfdataset.h +++ b/frmts/nitf/nitfdataset.h @@ -72,10 +72,14 @@ class NITFDataset final: public GDALPamDataset GDALDataset *poJ2KDataset; int bJP2Writing; +<<<<<<< HEAD:frmts/nitf/nitfdataset.h vsi_l_offset m_nImageOffset = 0; int m_nIMIndex = 0; int m_nImageCount = 0; vsi_l_offset m_nICOffset = 0; +======= + int m_nICOffset = 0; +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitfdataset.h GDALDataset *poJPEGDataset; diff --git a/frmts/nitf/nitffile.c b/frmts/nitf/nitffile.c index 0e83a9928cd9..ed9725b6b577 100644 --- a/frmts/nitf/nitffile.c +++ b/frmts/nitf/nitffile.c @@ -544,6 +544,7 @@ int NITFCreate( const char *pszFilename, { return NITFCreateEx(pszFilename, nPixels, nLines, nBands, nBitsPerSample, +<<<<<<< HEAD:frmts/nitf/nitffile.c pszPVType, papszOptions, NULL, NULL, NULL, NULL); } @@ -555,6 +556,15 @@ int NITFCreateEx( const char *pszFilename, int* pnImageCount, vsi_l_offset* pnImageOffset, vsi_l_offset* pnICOffset ) +======= + pszPVType, papszOptions, NULL); +} + +int NITFCreateEx( const char *pszFilename, + int nPixels, int nLines, int nBands, + int nBitsPerSample, const char *pszPVType, + char **papszOptions, int* pnICOffset ) +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitffile.c { VSILFILE *fp; @@ -1092,11 +1102,16 @@ int NITFCreateEx( const char *pszFilename, } } +<<<<<<< HEAD:frmts/nitf/nitffile.c if( pnICOffset ) { if( iIM == 0 || bAppendSubdataset ) *pnICOffset = nCur+nOffset+1; } +======= + if( pnICOffset && iIM == 0 ) + *pnICOffset = (int)(nCur+nOffset+1); +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitffile.c OVR( 2,nCur+nOffset+1, IC , "NC" ); if( pszIC[0] != 'N' ) diff --git a/frmts/nitf/nitflib.h b/frmts/nitf/nitflib.h index c6c39063cf6d..e7f956bcd0cb 100644 --- a/frmts/nitf/nitflib.h +++ b/frmts/nitf/nitflib.h @@ -95,6 +95,7 @@ int CPL_DLL NITFCreate( const char *pszFilename, char **papszOptions ); int NITFCreateEx( const char *pszFilename, +<<<<<<< HEAD:frmts/nitf/nitflib.h int nPixels, int nLines, int nBands, int nBitsPerSample, const char *pszPVType, char **papszOptions, @@ -102,6 +103,11 @@ int NITFCreateEx( const char *pszFilename, int* pnImageCount, vsi_l_offset* pnImageOffset, vsi_l_offset* pnICOffset ); +======= + int nPixels, int nLines, int nBands, + int nBitsPerSample, const char *pszPVType, + char **papszOptions, int* pnICOffset ); +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/frmts/nitf/nitflib.h const char CPL_DLL *NITFFindTRE( const char *pszTREData, int nTREBytes, const char *pszTag, int *pnFoundTRESize ); diff --git a/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py b/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py index 7f653ed8ef8e..ba3871ad1411 100644 --- a/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py +++ b/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py @@ -57,6 +57,7 @@ def __repr__(self): return str(self.pal) def __eq__(self, other): +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py return self.pal == other.pal and self.ndv == other.ndv def is_numeric(self): @@ -81,6 +82,9 @@ def get_all_keys(self, with_ndv: bool = True): if with_ndv and self.ndv is not None: keys = ['nv', *keys] return keys +======= + return self.pal == other.pal +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def replace_absolute_values_with_percent(self, ndv=True): new_pal = ColorPalette() @@ -149,7 +153,11 @@ def assign(self, other: 'ColorPalette'): self._all_numeric = other._all_numeric @staticmethod +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def get_supported_extenstions() -> Sequence[str]: +======= + def get_supported_extenstions(): +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py return [ 'txt', # GDAL Text-based color configuration file 'qlr', # QGIS Layer Definition File (qlr) @@ -163,8 +171,17 @@ def is_supported_format(self, filename: PathLikeOrStr): return False def set_ndv(self, ndv: Optional[int], override: bool = True): +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py if override or (self.ndv is None): self.ndv = ndv +======= + if ndv is not None: + if override or ('nv' not in self.pal): + self.pal['nv'] = ndv + else: + if override and ('nv' in self.pal): + del self.pal['nv'] +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def read(self, filename_or_strings: Optional[ColorPaletteOrPathOrStrings]): if filename_or_strings is None: @@ -186,7 +203,11 @@ def read_file(self, filename: PathLikeOrStr): else: self.read_file_txt(filename) +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def read_file_qml(self, qml_filename: PathLikeOrStr, tag_name=None, type=None): +======= + def read_file_qml(self, qml_filename, tag_name=None, type=None): +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py """ Read QGIS Layer Style File (qml) or QGIS Layer Definition File (qlr) """ qlr = minidom.parse(str(qml_filename)) if tag_name is None: @@ -248,6 +269,7 @@ def read_file_txt(self, filename: Optional[PathLikeOrStr] = None, lines: Optiona self._all_numeric = False self.pal[key] = color +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def get_txt_color_entry(self, key): color = self.get_color(key) key = self.get_txt_key(key) @@ -275,6 +297,27 @@ def to_mem_buffer(self, with_ndv: bool = True) -> str: s = s + line return s +======= + def write_file(self, color_filename: Optional[PathLikeOrStr] = None): + if color_filename is None: + color_filename = tempfile.mktemp(suffix='.txt') + os.makedirs(os.path.dirname(color_filename), exist_ok=True) + with open(color_filename, mode='w') as fp: + for key, color in self.pal.items(): + color_entry = self.color_to_color_entry(color) + color_entry = ' '.join(str(c) for c in color_entry) + fp.write('{} {}\n'.format(key, color_entry)) + return color_filename + + def to_mem_buffer(self): + s = '' + for key, color in self.pal.items(): + cc = self.color_to_color_entry(color) + cc = ' '.join(str(c) for c in cc) + s = s + '{} {}\n'.format(key, cc) + return s + +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py @staticmethod def from_string_list(color_palette_strings: Optional[ColorPaletteOrPathOrStrings]) -> 'ColorPalette': res = ColorPalette() @@ -290,22 +333,33 @@ def format_color(col): return col if isinstance(col, str) else '#{:06X}'.format(col) @staticmethod +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def color_to_color_entry(color, with_alpha: Optional[bool]=None): +======= + def color_to_color_entry(color): +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py b = base.get_byte(color, 0) g = base.get_byte(color, 1) r = base.get_byte(color, 2) a = base.get_byte(color, 3) +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py if with_alpha or (with_alpha is None and (a < 255)): +======= + if a < 255: +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py return r, g, b, a else: return r, g, b @staticmethod +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def color_entry_to_color(r, g, b, a): return (a << 24) + (r << 16) + (g << 8) + b @staticmethod +======= +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def pal_color_to_rgb(cc: str) -> int: # r g b a -> argb # todo: support color names as implemented in the cpp version of this function... @@ -366,6 +420,10 @@ def get_xkcd_palette(): read_color_file = read write_color_file = write_file read_file_xml = read_file_qlr = read_file_qml +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py +======= + +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_palette.py def xml_to_color_file(xml_filename: Path, **kwargs) -> Tuple[ColorPalette, Path]: diff --git a/swig/python/gdal-utils/osgeo_utils/auxiliary/color_table.py b/swig/python/gdal-utils/osgeo_utils/auxiliary/color_table.py index e71337ea35a3..cf546c67ad89 100644 --- a/swig/python/gdal-utils/osgeo_utils/auxiliary/color_table.py +++ b/swig/python/gdal-utils/osgeo_utils/auxiliary/color_table.py @@ -50,11 +50,29 @@ def get_color_table_from_raster(path_or_ds: PathOrDS) -> Optional[gdal.ColorTabl return ct.Clone() +<<<<<<< HEAD:swig/python/gdal-utils/osgeo_utils/auxiliary/color_table.py def color_table_from_color_palette(pal: ColorPalette, color_table: gdal.ColorTable, fill_missing_colors=True, min_key=0, max_key=255) -> bool: """ returns None if pal has no values, otherwise returns a gdal.ColorTable from the given ColorPalette""" if not pal.pal or not pal.is_numeric(): raise Exception('palette has no values or not fully numeric') +======= +def get_color_table(color_palette_or_path_or_strings_or_ds: Optional[ColorTableLike], + min_key=0, max_key=255, fill_missing_colors=True) -> Optional[gdal.ColorTable]: + if (color_palette_or_path_or_strings_or_ds is None or + isinstance(color_palette_or_path_or_strings_or_ds, gdal.ColorTable)): + return color_palette_or_path_or_strings_or_ds + + if isinstance(color_palette_or_path_or_strings_or_ds, gdal.Dataset): + return get_color_table_from_raster(color_palette_or_path_or_strings_or_ds) + + try: + pal = get_color_palette(color_palette_or_path_or_strings_or_ds) + except: + # the input might be a filename of a raster file + return get_color_table_from_raster(color_palette_or_path_or_strings_or_ds) + color_table = gdal.ColorTable() +>>>>>>> 5742ec588f (Merge branch 'master' of github.com:OSGeo/gdal):gdal/swig/python/gdal-utils/osgeo_utils/auxiliary/color_table.py if fill_missing_colors: keys = sorted(list(pal.pal.keys())) if min_key is None: