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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# *****************************************************************************
# $Id: validate_cloud_optimized_geotiff.py ff8477723e9fd9ee2f60beeaf1da8c4df4c4a44a 2018-10-17 16:01:10 -0500 Norman Barker $
#
# Project: GDAL
# Purpose: Validate Cloud Optimized GeoTIFF file structure
# Author: Even Rouault, <even dot rouault at spatialys dot com>
#
# *****************************************************************************
# Copyright (c) 2017, Even Rouault
#
# 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: validate_cloud_optimized_geotiff.py [-q] test.tif')
print('')
return 1
class ValidateCloudOptimizedGeoTIFFException(Exception):
pass
def validate(ds, check_tiled=True):
"""Check if a file is a (Geo)TIFF with cloud optimized compatible structure.
Args:
ds: GDAL Dataset for the file to inspect.
check_tiled: Set to False to ignore missing tiling.
Returns:
A tuple, whose first element is an array of error messages
(empty if there is no error), and the second element, a dictionary
with the structure of the GeoTIFF file.
Raises:
ValidateCloudOptimizedGeoTIFFException: Unable to open the file or the
file is not a Tiff.
"""
if int(gdal.VersionInfo('VERSION_NUM')) < 2020000:
raise ValidateCloudOptimizedGeoTIFFException(
'GDAL 2.2 or above required')
unicode_type = type(''.encode('utf-8').decode('utf-8'))
if isinstance(ds, (str, unicode_type)):
gdal.PushErrorHandler()
ds = gdal.Open(ds)
gdal.PopErrorHandler()
if ds is None:
raise ValidateCloudOptimizedGeoTIFFException(
'Invalid file : %s' % gdal.GetLastErrorMsg())
if ds.GetDriver().ShortName != 'GTiff':
raise ValidateCloudOptimizedGeoTIFFException(
'The file is not a GeoTIFF')
details = {}
errors = []
warnings = []
filename = ds.GetDescription()
main_band = ds.GetRasterBand(1)
ovr_count = main_band.GetOverviewCount()
filelist = ds.GetFileList()
if filelist is not None and filename + '.ovr' in filelist:
errors += [
'Overviews found in external .ovr file. They should be internal']
if main_band.XSize >= 512 or main_band.YSize >= 512:
if check_tiled:
block_size = main_band.GetBlockSize()
if block_size[0] == main_band.XSize and block_size[0] > 1024:
errors += [
'The file is greater than 512xH or Wx512, but is not tiled']
if ovr_count == 0:
warnings += [
'The file is greater than 512xH or Wx512, it is recommended '
'to include internal overviews']
ifd_offset = int(main_band.GetMetadataItem('IFD_OFFSET', 'TIFF'))
ifd_offsets = [ifd_offset]
if ifd_offset not in (8, 16):
errors += [
'The offset of the main IFD should be 8 for ClassicTIFF '
'or 16 for BigTIFF. It is %d instead' % ifd_offsets[0]]
details['ifd_offsets'] = {}
details['ifd_offsets']['main'] = ifd_offset
for i in range(ovr_count):
# Check that overviews are by descending sizes
ovr_band = ds.GetRasterBand(1).GetOverview(i)
if i == 0:
if (ovr_band.XSize > main_band.XSize or
ovr_band.YSize > main_band.YSize):
errors += [
'First overview has larger dimension than main band']
else:
prev_ovr_band = ds.GetRasterBand(1).GetOverview(i - 1)
if (ovr_band.XSize > prev_ovr_band.XSize or
ovr_band.YSize > prev_ovr_band.YSize):
errors += [
'Overview of index %d has larger dimension than '
'overview of index %d' % (i, i - 1)]
if check_tiled:
block_size = ovr_band.GetBlockSize()
if block_size[0] == ovr_band.XSize and block_size[0] > 1024:
errors += [
'Overview of index %d is not tiled' % i]
# Check that the IFD of descending overviews are sorted by increasing
# offsets
ifd_offset = int(ovr_band.GetMetadataItem('IFD_OFFSET', 'TIFF'))
ifd_offsets.append(ifd_offset)
details['ifd_offsets']['overview_%d' % i] = ifd_offset
if ifd_offsets[-1] < ifd_offsets[-2]:
if i == 0:
errors += [
'The offset of the IFD for overview of index %d is %d, '
'whereas it should be greater than the one of the main '
'image, which is at byte %d' %
(i, ifd_offsets[-1], ifd_offsets[-2])]
else:
errors += [
'The offset of the IFD for overview of index %d is %d, '
'whereas it should be greater than the one of index %d, '
'which is at byte %d' %
(i, ifd_offsets[-1], i - 1, ifd_offsets[-2])]
# Check that the imagery starts by the smallest overview and ends with
# the main resolution dataset
block_offset = main_band.GetMetadataItem('BLOCK_OFFSET_0_0', 'TIFF')
if not block_offset:
errors += ['Missing BLOCK_OFFSET_0_0']
data_offset = int(block_offset) if block_offset else None
data_offsets = [data_offset]
details['data_offsets'] = {}
details['data_offsets']['main'] = data_offset
for i in range(ovr_count):
ovr_band = ds.GetRasterBand(1).GetOverview(i)
data_offset = int(ovr_band.GetMetadataItem('BLOCK_OFFSET_0_0', 'TIFF'))
data_offsets.append(data_offset)
details['data_offsets']['overview_%d' % i] = data_offset
if data_offsets[-1] < ifd_offsets[-1]:
if ovr_count > 0:
errors += [
'The offset of the first block of the smallest overview '
'should be after its IFD']
else:
errors += [
'The offset of the first block of the image should '
'be after its IFD']
for i in range(len(data_offsets) - 2, 0, -1):
if data_offsets[i] < data_offsets[i + 1]:
errors += [
'The offset of the first block of overview of index %d should '
'be after the one of the overview of index %d' %
(i - 1, i)]
if len(data_offsets) >= 2 and data_offsets[0] < data_offsets[1]:
errors += [
'The offset of the first block of the main resolution image'
'should be after the one of the overview of index %d' %
(ovr_count - 1)]
return warnings, errors, details
def main():
"""Return 0 in case of success, 1 for failure."""
i = 1
filename = None
quiet = False
while i < len(sys.argv):
if sys.argv[i] == '-q':
quiet = True
elif sys.argv[i][0] == '-':
return Usage()
elif filename is None:
filename = sys.argv[i]
else:
return Usage()
i += 1
if filename is None:
return Usage()
try:
ret = 0
warnings, errors, _ = validate(filename)
if warnings:
if not quiet:
print('The following warnings were found:')
for warning in warnings:
print(' - ' + warning)
print('')
if errors:
if not quiet:
print('%s is NOT a valid cloud optimized GeoTIFF.' % filename)
print('The following errors were found:')
for error in errors:
print(' - ' + error)
print('')
ret = 1
else:
if not quiet:
print('%s is a valid cloud optimized GeoTIFF' % filename)
except ValidateCloudOptimizedGeoTIFFException as e:
if not quiet:
print('%s is NOT a valid cloud optimized GeoTIFF : %s' %
(filename, str(e)))
ret = 1
return ret
if __name__ == '__main__':
sys.exit(main())
|