-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetFileStructure.py
More file actions
99 lines (86 loc) · 4.79 KB
/
getFileStructure.py
File metadata and controls
99 lines (86 loc) · 4.79 KB
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
import tifffile
from datetime import datetime
import textwrap
import numpy as np
import importlib
import sys
import traceback
def dump_tiff_structure_to_txt(filepath, output_txt="tiff_structure.txt"):
# Vérifie si imagecodecs est disponible
has_imagecodecs = importlib.util.find_spec("imagecodecs") is not None
if not has_imagecodecs:
print("⚠️ Le module 'imagecodecs' n'est pas installé.")
print(" → Certaines compressions (ex: LZW) ne pourront pas être décompressées.")
print(" → Le script va continuer en ignorant la lecture des pixels.\n")
with open(output_txt, "w", encoding="utf-8") as out:
out.write(f"=== ANALYSE COMPLÈTE DU FICHIER TIFF ===\n")
out.write(f"Fichier : {filepath}\n")
out.write(f"Date d'analyse : {datetime.now()}\n")
out.write("="*100 + "\n\n")
with tifffile.TiffFile(filepath) as tif:
out.write(f"Nombre total d’IFDs (images/pages) : {len(tif.pages)}\n")
out.write(f"Ordre des octets : {'Little endian' if tif.byteorder == '<' else 'Big endian'}\n")
out.write(f"BigTIFF : {'Oui' if tif.is_bigtiff else 'Non'}\n\n")
out.write("="*100 + "\n")
for i, page in enumerate(tif.pages):
out.write(f"\n📄 IFD {i}\n")
out.write("-"*100 + "\n")
out.write(f"Dimensions : {page.shape}\n")
out.write(f"Dtype : {page.dtype}\n")
out.write(f"Compression : {getattr(page.compression, 'name', 'None')}\n")
out.write(f"PhotometricInterpretation: {getattr(page.photometric, 'name', 'N/A')}\n")
out.write(f"Samples per pixel : {page.samplesperpixel}\n")
out.write(f"Offset IFD : {page.offset}\n")
out.write(f"Planar configuration : {getattr(page, 'planarconfig', 'N/A')}\n")
out.write("\n--- TAGS INTERNES ---\n")
# --- Lecture de tous les tags ---
for tag in page.tags.values():
name = tag.name
dtype = tag.dtype.name if hasattr(tag, "dtype") else str(type(tag.value))
value = tag.value
# Tronquer les longues valeurs
if isinstance(value, (list, tuple, np.ndarray)) and len(value) > 15:
display_value = f"{value[:15]} ... ({len(value)} valeurs)"
else:
display_value = value
wrapped_value = "\n ".join(textwrap.wrap(str(display_value), width=100))
out.write(f"{name:30s} | Type: {dtype:10s} | Valeur: {wrapped_value}\n")
# --- Tags géospatiaux ---
geo_tags = [
"ModelPixelScaleTag", "ModelTiepointTag",
"GeoKeyDirectoryTag", "GeoDoubleParamsTag", "GeoASCIIParamsTag"
]
geo_present = [t for t in geo_tags if t in page.tags]
if geo_present:
out.write("\n--- TAGS GÉOSPATIAUX ---\n")
for t in geo_present:
wrapped_geo = "\n ".join(textwrap.wrap(str(page.tags[t].value), width=100))
out.write(f"{t:30s} : {wrapped_geo}\n")
# --- Aperçu des données raster si possible ---
out.write("\n--- EXTRAIT DES DONNÉES RASTER ---\n")
if has_imagecodecs:
try:
data = page.asarray()
sample = data
if data.ndim == 2:
sample = data[:10, :10]
elif data.ndim == 3:
sample = data[0, :10, :10]
out.write(np.array2string(sample, threshold=100, max_line_width=120))
out.write("\n✅ Données raster extraites avec succès\n")
except Exception as e:
out.write(f"⚠️ Erreur lors de la lecture des données raster : {e}\n")
out.write(traceback.format_exc())
else:
out.write("⏩ Lecture des pixels ignorée (module 'imagecodecs' non installé)\n")
out.write("\n" + "-"*100 + "\n")
out.write("\n=== FIN DE L'EXPLORATION ===\n")
print(f"\n✅ Structure complète exportée dans : {output_txt}")
# ==============================
# Exécution principale
# ==============================
if __name__ == "__main__":
dump_tiff_structure_to_txt(
"data/2000-2022/GLC_FCS30D_20002022_W70S55_Annual.tif",
"structure_tif_complete3.txt"
)