File: dump_jp2.py

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (224 lines) | stat: -rw-r--r-- 8,126 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/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:])
                    )
                return 0

    print("No CRS dictionary content found in %s" % filename)
    return 1


def extract_all_xml_boxes(filename, prefix):
    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]
            boxname = domain[4:]
            if boxname == "gml.root-instance":
                boxname = "gml_root_instance.gml"
            out_filename = prefix + boxname

            # Correct references to gmljp2://xml/foo to prefix_foo
            out_content = ""
            pos = 0
            while True:
                new_pos = mdd_item.find("gmljp2://xml/", pos)
                if new_pos < 0:
                    out_content += mdd_item[pos:]
                    break

                # Check that the referenced box really exists
                end_gmljp2_link_space = mdd_item.find(" ", new_pos)
                end_gmljp2_link_double_quote = mdd_item.find('"', new_pos)
                end_gmljp2_link = -1
                if end_gmljp2_link_space >= 0 and end_gmljp2_link_double_quote >= 0:
                    if end_gmljp2_link_space < end_gmljp2_link_double_quote:
                        end_gmljp2_link = end_gmljp2_link_space
                    else:
                        end_gmljp2_link = end_gmljp2_link_double_quote
                elif end_gmljp2_link_space >= 0:
                    end_gmljp2_link = end_gmljp2_link_space
                elif end_gmljp2_link_double_quote >= 0:
                    end_gmljp2_link = end_gmljp2_link_double_quote
                if end_gmljp2_link >= 0:
                    referenced_box = mdd_item[
                        new_pos + len("gmljp2://xml/") : end_gmljp2_link
                    ]
                    if ("xml:" + referenced_box) not in mdd_list:
                        print(
                            "Warning: box %s reference box %s, but the latter is not found"
                            % (boxname, referenced_box)
                        )

                out_content += mdd_item[pos:new_pos]
                out_content += prefix
                pos = new_pos + len("gmljp2://xml/")

            f = open(out_filename, "wt")
            f.write(out_content)
            f.close()
            print("INFO: %s written" % out_filename)

    if not mdd_list:
        print("No XML box found")
        return 1
    return 0


def main(argv=sys.argv):
    i = 1
    out_gmljp2 = None
    out_crsdictionary = None
    extract_all_xml_boxes_prefix = None
    filename = None
    while i < len(argv):
        if argv[i] == "-dump_gmljp2":
            if i >= len(argv) - 1:
                return Usage()
            out_gmljp2 = argv[i + 1]
            i = i + 1
        elif argv[i] == "-dump_crsdictionary":
            if i >= len(argv) - 1:
                return Usage()
            out_crsdictionary = argv[i + 1]
            i = i + 1
        elif argv[i] == "-extract_all_xml_boxes":
            if i >= len(argv) - 1:
                return Usage()
            extract_all_xml_boxes_prefix = argv[i + 1]
            i = i + 1
        elif argv[i][0] == "-":
            return Usage()
        elif filename is None:
            filename = argv[i]
        else:
            return Usage()

        i = i + 1

    if filename is None:
        return Usage()

    if out_gmljp2 or out_crsdictionary or extract_all_xml_boxes_prefix:
        if out_gmljp2:
            if dump_gmljp2(filename, out_gmljp2) != 0:
                return 1
        if out_crsdictionary:
            if dump_crsdictionary(filename, out_crsdictionary) != 0:
                return 1
        if extract_all_xml_boxes_prefix:
            if extract_all_xml_boxes(filename, extract_all_xml_boxes_prefix) != 0:
                return 1
    else:
        s = gdal.GetJPEG2000StructureAsString(filename, ["ALL=YES"])
        print(s)

    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))