Newer
Older
from osgeo import gdal
import zipfile
import tempfile
import os
def is_geotiff(resource, file_name):
dataset = gdal.Open(file_name)
if not dataset:
resource["error"] = f"could not read file {file_name}"
return False
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
geotransform = dataset.GetGeoTransform()
default_transform = (0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
if geotransform == default_transform:
resource["error"] = "missing transformation"
return False
return True
def is_valid(resource, file):
"""Check if the content is a GeoTIFF file."""
# Some GeoTIFF files consist for two files in a ZIP file:
# - the TIFF image itself
# - a TFW world file with the transform information
if zipfile.is_zipfile(file.name):
with tempfile.TemporaryDirectory() as temp_dir:
with zipfile.ZipFile(file.name, "r") as zip_ref:
file_list = zip_ref.namelist()
relevant_files = [
file
for file in file_list
if file.lower().endswith(".tiff") or file.lower().endswith(".tif")
]
contains_at_least_one_relevant_file = len(relevant_files) > 0
if contains_at_least_one_relevant_file:
zip_ref.extractall(temp_dir)
for tif_name in relevant_files:
tif_path = os.path.join(temp_dir, tif_name)
if is_geotiff(resource, tif_path):
# the ZIP file contains at least one valid GeoTIFF
return True
else:
resource["error"] = "ZIP file contains not TIFF image"
return False
else:
return is_geotiff(resource, file.name)