-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress_data.py
More file actions
303 lines (256 loc) · 10.8 KB
/
Copy pathcompress_data.py
File metadata and controls
303 lines (256 loc) · 10.8 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""
Landsat Raster Downscaler & GeoTIFF Compressor
==============================================
This script recursively scans the 'data' folder, resamples/downscales raster (.TIF)
files to a lower resolution (reducing size quadratically), applies high-performance
internal GeoTIFF compression, and replicates the layout in the 'compressed' folder.
Non-raster files (.txt, .xml) are copied directly to maintain complete directory consistency.
Uses multiprocessing to compress files in parallel across all CPU cores.
"""
import os
import sys
import time
import shutil
import concurrent.futures
from pathlib import Path
# Try importing rasterio
try:
import rasterio
from rasterio.enums import Resampling
except ImportError:
print("Error: 'rasterio' is not installed in the current Python environment.")
print("Please install it using: pip install rasterio")
sys.exit(1)
# Configuration
SOURCE_DIR = Path("data")
TARGET_DIR = Path("compressed")
# Downscale factor (0.5 means width/height is reduced by 50%, i.e. 25% of the total pixel area)
# e.g., 30m resolution -> 60m resolution
SCALE_FACTOR = 0.5
def format_size(bytes_size):
"""Formats bytes into human-readable sizes."""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if bytes_size < 1024:
return f"{bytes_size:.2f} {unit}"
bytes_size /= 1024
return f"{bytes_size:.2f} PB"
def compress_single_raster(task_info):
"""
Downscales and internally compresses a single GeoTIFF file.
Runs in a separate process.
"""
src_path, dst_path = task_info
try:
# Create parent directories for the destination file
dst_path.parent.mkdir(parents=True, exist_ok=True)
orig_size = src_path.stat().st_size
start_time = time.time()
# Open source GeoTIFF
with rasterio.open(src_path) as src:
# 1. Determine new resampled dimensions
new_width = max(1, int(src.width * SCALE_FACTOR))
new_height = max(1, int(src.height * SCALE_FACTOR))
# 2. Select appropriate resampling algorithm
# Mask / QA bands have discrete categories/bitmasks and MUST use Nearest Neighbor
# to prevent intermediate values from being interpolated.
# Spectral bands (SR_B1 to SR_B7) can use Bilinear for smooth transitions.
is_qa_or_mask = "QA" in src_path.name or "mask" in src_path.name.lower()
resampling_method = Resampling.nearest if is_qa_or_mask else Resampling.bilinear
# 3. Read and resample the data
data = src.read(
out_shape=(src.count, new_height, new_width),
resampling=resampling_method
)
# 4. Update the affine transform matrix for the new cell resolution
transform = src.transform * src.transform.scale(
(src.width / new_width),
(src.height / new_height)
)
# 5. Build optimized compression profile
profile = src.profile.copy()
# Choose optimal spatial predictor (highly effective for increasing compression ratios)
# Predictor=2 is horizontal difference for integers; Predictor=3 is for floating points
dtype_name = src.dtypes[0]
if "int" in dtype_name:
predictor = 2
elif "float" in dtype_name:
predictor = 3
else:
predictor = 1 # No predictor
profile.update({
'height': new_height,
'width': new_width,
'transform': transform,
'compress': 'deflate', # High efficiency standard compression
'compresslevel': 6, # Level 6 is the sweet spot for deflate speed/ratio
'predictor': predictor,
'tiled': True,
'blockxsize': min(256, new_width),
'blockysize': min(256, new_height)
})
# Write optimized raster
with rasterio.open(dst_path, 'w', **profile) as dst:
dst.write(data)
comp_size = dst_path.stat().st_size
duration = time.time() - start_time
return {
"success": True,
"type": "raster",
"src": str(src_path),
"dst": str(dst_path),
"orig_size": orig_size,
"comp_size": comp_size,
"duration": duration,
"error": None
}
except Exception as e:
return {
"success": False,
"type": "raster",
"src": str(src_path),
"dst": str(dst_path),
"orig_size": 0,
"comp_size": 0,
"duration": 0,
"error": str(e)
}
def copy_metadata_file(task_info):
"""Copies non-raster metadata files directly to target."""
src_path, dst_path = task_info
try:
dst_path.parent.mkdir(parents=True, exist_ok=True)
orig_size = src_path.stat().st_size
# Copy file
shutil.copy2(src_path, dst_path)
comp_size = dst_path.stat().st_size
return {
"success": True,
"type": "metadata",
"src": str(src_path),
"dst": str(dst_path),
"orig_size": orig_size,
"comp_size": comp_size,
"duration": 0,
"error": None
}
except Exception as e:
return {
"success": False,
"type": "metadata",
"src": str(src_path),
"dst": str(dst_path),
"orig_size": 0,
"comp_size": 0,
"duration": 0,
"error": str(e)
}
def show_progress_bar(completed, total, bar_length=40):
"""Displays a clean console progress bar."""
if total == 0:
return
percent = float(completed) / total
filled_len = int(round(percent * bar_length))
arrow = '#' * filled_len
spaces = '-' * (bar_length - filled_len)
sys.stdout.write(f"\rProgress: [{arrow}{spaces}] {completed}/{total} files ({percent*100:.1f}%)")
sys.stdout.flush()
def main():
print("=" * 60)
print(" Landsat Raster Downscaler & GeoTIFF Compressor")
print("=" * 60)
print(f"Scale Factor: {SCALE_FACTOR} (Width/Height reduced by 50% -> Area by 75%)")
print(f"Source Folder: '{SOURCE_DIR}'")
print(f"Target Folder: '{TARGET_DIR}'")
print("-" * 60)
if not SOURCE_DIR.exists():
print(f"Error: Source directory '{SOURCE_DIR}' does not exist.")
sys.exit(1)
print("Scanning source directory for files...")
raster_tasks = []
metadata_tasks = []
for root, _, files in os.walk(SOURCE_DIR):
for file in files:
src_file = Path(root) / file
rel_path = src_file.relative_to(SOURCE_DIR)
dst_file = TARGET_DIR / rel_path
# Separate TIF rasters from other metadata files (txt, xml)
if src_file.suffix.lower() in [".tif", ".tiff"]:
raster_tasks.append((src_file, dst_file))
else:
metadata_tasks.append((src_file, dst_file))
total_rasters = len(raster_tasks)
total_metadata = len(metadata_tasks)
total_files = total_rasters + total_metadata
if total_files == 0:
print("No files found to process.")
sys.exit(0)
print(f"Found {total_files} files in total:")
print(f" - {total_rasters} GeoTIFF rasters (.TIF) to downscale & compress")
print(f" - {total_metadata} metadata files (.txt, .xml) to copy as-is")
print("-" * 60)
# Process metadata files first (very fast copy)
print("1. Processing metadata files...")
total_orig_size = 0
total_comp_size = 0
completed_count = 0
failed_count = 0
failed_files = []
for task in metadata_tasks:
res = copy_metadata_file(task)
if res["success"]:
total_orig_size += res["orig_size"]
total_comp_size += res["comp_size"]
else:
failed_count += 1
failed_files.append((res["src"], res["error"]))
completed_count += 1
show_progress_bar(completed_count, total_files)
print(f"\nMetadata copy finished. Handled {total_metadata} files.")
print("-" * 60)
# Process rasters in parallel
print("2. Starting parallel raster downscaling and compression...")
start_time = time.time()
max_workers = os.cpu_count() or 4
print(f"Spawning {max_workers} worker processes for CPU-bound resampling...")
# Show initial progress with completed metadata
show_progress_bar(completed_count, total_files)
with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) as executor:
# Submit raster downscale tasks to process pool
future_to_raster = {executor.submit(compress_single_raster, task): task for task in raster_tasks}
for future in concurrent.futures.as_completed(future_to_raster):
result = future.result()
completed_count += 1
if result["success"]:
total_orig_size += result["orig_size"]
total_comp_size += result["comp_size"]
else:
failed_count += 1
failed_files.append((result["src"], result["error"]))
# Update console progress bar
show_progress_bar(completed_count, total_files)
total_duration = time.time() - start_time
print("\n" + "-" * 60)
print("Process complete!")
print("=" * 60)
print(" SUMMARY REPORT")
print("=" * 60)
print(f"Time Taken: {total_duration:.2f} seconds")
print(f"Files Processed: {completed_count - failed_count} / {total_files}")
if failed_count > 0:
print(f"Files Failed: {failed_count}")
print("\nErrors encountered:")
for src, err in failed_files:
print(f" - {src}: {err}")
print("-" * 60)
if completed_count - failed_count > 0:
saved_bytes = total_orig_size - total_comp_size
savings_pct = (saved_bytes / total_orig_size * 100) if total_orig_size > 0 else 0
print(f"Original Size: {format_size(total_orig_size)}")
print(f"Compressed Size: {format_size(total_comp_size)}")
print(f"Total Disk Space Saved: {format_size(saved_bytes)} ({savings_pct:.1f}% reduction!)")
print("\nNote: GeoTIFF metadata & coordinate reference systems have been fully preserved.")
print("Your classification pipeline can use the 'compressed' folder directly.")
print("=" * 60)
if __name__ == "__main__":
main()