1
  X   ˆl–Ðz¾” ”Ê:”Ê�jã/\ªbÑ¿�þC,eY¹BÓÅp¡ÍùÀƒx"s¿_ŠI|¥�+Wé3ž¯a–Ü4ý( ©¤ýÔâ€vã/ÜÊbÑ¿Ä ¾    #!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ******************************************************************************
#  $Id$
#
#  Project:  GDAL
#  Purpose:  Dump JPEG2000 file structure
#  Author:   Even Rouault, <even dot rouault at spatialys dot com>
#
# ******************************************************************************
#  Copyright (c) 2015, European Union (European Environment Agency)
#  Copyright (c) 2015, European Union Satellite Centre
#
#  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 osgeo import gdal


def Usage():
    print("Usage:  dump_jp2 [-dump_gmljp2 out.xml|-] [-dump_crsdictionary out.xml|-]")
    print("                 [-extract_all_xml_boxes filename_prefix]")
    print("                 test.jp2")
    print("")
    print("Options (all are exclusive of the regular dump):")
    print("")
    print("-dump_gmljp2: Writes the content of the GMLJP2 box in the specified")
    print('              file, or on the console if "-" syntax is used.')
    print(
        "-dump_crsdictionary: Writes the content of the GML CRS dictionary box in the specified"
    )
    print('                     file, or on the console if "-" syntax is used.')
    print(
        "-extract_all_xml_boxes: Extract all XML boxes in separate files, and prefix each filename"
    )
    print(
        "                        with the supplied prefix. gmljp2://xml/ link will be replaced by"
    )
    print("                        links to on-disk files.")

    return 2


def dump_gmljp2(filename, out_gmljp2):
    ds = gdal.Open(filename)
    if ds is None:
        print("Cannot open %s" % filename)
        return 1
    mdd = ds.GetMetadata("xml:gml.root-instance")
    if mdd is None:
        print("No GMLJP2 content found in %s" % filename)
        return 1
    if out_gmljp2 == "-":
        print(mdd[0])
    else:
        f = open(out_gmljp2, "wt")
        f.write(mdd[0])
        f.close()
        print("INFO: %s written with content of GMLJP2 box" % out_gmljp2)
    return 0


def dump_crsdictionary(filename, out_crsdictionary):
    ds = gdal.Open(filename)
    if ds is None:
        print("Cannot open %s" % filename)
        return 1
    mdd_list = ds.GetMetadataDomainList()
    if mdd_list is None:
        mdd_list = []
    for domain in mdd_list:
        if domain.startswith("xml:"):
            mdd_item = ds.GetMetadata(domain)[0]
            if "<Dictionary" in mdd_item or "<gml:Dictionary" in mdd_item:
                if out_crsdictionary == "-":
                    print(mdd_item)
                else:
                    f = open(out_crsdictionary, "wt")
                    f.write(mdd_item)
                    f.close()
                    print(
                        "INFO: %s written with content of CRS dictionary box (%s)"
                        % (out_crsdictionary, domain[4:])
                 