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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627 | def pipeline(
shared_input_images: Universal.SearchFolderOrListFiles,
shared_output_image_path: Universal.CreateInFolderOrListFiles,
*,
shared_temp_dir: str | None = None,
delete_temp_dir: bool = True,
delete_previous_step: bool = False,
shared_resume_from_steps: Literal["no", "yes", "validate"] = "no",
shared_debug_logs: Universal.DebugLogs = False,
shared_cache: AutoCache = "auto",
shared_custom_nodata_value: Universal.CustomNodataValue = None,
shared_window_size: Universal.WindowSize = 1024,
shared_window_scales: tuple[int, ...] | None = (2, 4, 8, 16, 32),
shared_image_threads: AutoThreads = "auto",
shared_io_threads: AutoThreads = "auto",
shared_tile_threads: AutoThreads = "auto",
shared_concurrent_processing_backend: Universal.ConcurrentProcessingBackend = "process_pool",
shared_dask_scheduler: Universal.DaskScheduler = None,
shared_calculation_dtype: Universal.CalculationDtype = "float32",
shared_output_dtype: Universal.CustomOutputDtype = None,
shared_save_as_cog: Universal.SaveAsCog = False,
steps: list[PipelineStep] | tuple[PipelineStep, ...] = DEFAULT_PIPELINE_STEPS,
joint_coregistration_global_model: Literal["none", "translation", "similarity", "affine"] = "translation",
joint_coregistration_global_image_position_preservation_weights: dict[str, float] | None = None,
joint_coregistration_global_tie_point_alignment_strength: float = 1.0,
joint_coregistration_local_model: Literal["none", "bilinear", "piecewise_affine"] = "piecewise_affine",
joint_coregistration_local_image_position_preservation_weights: dict[str, float] | None = None,
joint_coregistration_local_tie_point_alignment_strength: float = 1.0,
joint_coregistration_local_grid_spacing: float = 500.0,
joint_coregistration_local_smoothness_weight: float = 1.0,
joint_coregistration_local_bending_weight: float = 1.0,
joint_coregistration_local_anchor_falloff_distance: float = 500.0,
joint_coregistration_feature_method: Literal["orb"] = "orb",
joint_coregistration_maximum_tie_point_displacement: float | None = None,
joint_coregistration_ransac_reprojection_threshold: float | None = None,
joint_coregistration_robust_loss: Literal["none", "huber", "soft_l1", "cauchy"] = "huber",
joint_coregistration_robust_loss_scale: float | None = None,
joint_coregistration_save_adjustments: str | None = None,
joint_coregistration_load_adjustments: str | None = None,
joint_coregistration_resampling_method: Literal["nearest", "bilinear", "cubic", "lanczos"] = "bilinear",
joint_coregistration_tap: bool = False,
joint_coregistration_resolution: Universal.Resolution = None,
joint_coregistration_build_overviews: bool = False,
align_rasters_resampling_method: Literal["nearest", "bilinear", "cubic"] = "bilinear",
align_rasters_tap: bool = False,
align_rasters_resolution: Universal.Resolution = None,
global_regression_vector_mask: Universal.VectorMask = None,
global_regression_estimate_stats: bool = True,
global_regression_specify_model_images: MatchValidation.SpecifyModelImages = None,
global_regression_custom_mean_factor: float = 1.0,
global_regression_custom_std_factor: float = 1.0,
global_regression_save_adjustments: str | None = None,
global_regression_load_adjustments: str | None = None,
global_regression_pif_method: Literal["entire", "flood_from_match_points"] = "flood_from_match_points",
global_regression_pif_red_band_index: int | None = None,
global_regression_pif_nir_band_index: int | None = None,
global_regression_pif_vegetation_threshold: float = 0.2,
global_regression_pif_inz_threshold: float = 0.25,
global_regression_pif_region_radius: int = 5,
global_regression_pif_max_samples: int | None = 10000,
global_regression_pif_min_samples: int | None = 10,
global_regression_pif_feature_method: Literal["orb"] = "orb",
global_regression_pif_load_tie_points: str | None = None,
global_regression_pif_save_inz: str | None = None,
global_regression_build_overviews: bool = False,
local_block_adjustment_vector_mask: Universal.VectorMask = None,
local_block_adjustment_number_of_blocks: int | Tuple[int, int] | Literal["coefficient_of_variation"] = 100,
local_block_adjustment_alpha: float = 1.0,
local_block_adjustment_correction_method: Literal["gamma", "linear", "offset"] = "offset",
local_block_adjustment_save_block_maps: Tuple[str, str] | None = None,
local_block_adjustment_load_block_maps: Tuple[str, List[str]] | Tuple[str, None] | Tuple[None, List[str]] | None = None,
local_block_adjustment_override_bounds_canvas_coords: Tuple[float, float, float, float] | None = None,
local_block_adjustment_build_overviews: bool = False,
voronoi_center_seamline_aoi_path: str | None = None,
voronoi_center_seamline_vector_mask: tuple[str, str] | None = None,
voronoi_center_seamline_image_field_name: str = "image",
voronoi_center_seamline_min_point_spacing: float = 10,
voronoi_center_seamline_min_cut_length: float = 0,
voronoi_center_seamline_debug_vectors_path: str | None = None,
weighted_seamline_input_polygons: str | None = None,
weighted_seamline_rank_function: str | None = None,
weighted_seamline_image_field_name: str = "image",
weighted_seamline_input_layer: str | None = None,
weighted_seamline_output_layer: str = "seamlines",
weighted_seamline_rank_descending: bool = True,
mask_rasters_vector_mask: Universal.VectorMask = None,
mask_rasters_include_touched_pixels: bool = False,
merge_rasters_output_tiles: bool = False,
merge_rasters_resolution: Literal["highest", "average", "lowest"] | int | float = "highest",
merge_rasters_overlap: int = 0,
merge_rasters_build_overviews: bool = False,
merge_rasters_resampling_method: Literal["nearest", "near", "bilinear", "cubic", "cubicspline", "lanczos"] = "nearest",
merge_rasters_custom_tiles_csv: str | None = None,
merge_rasters_create_vrts: str = "MergedImage.vrt",
) -> dict[str, Any]:
"""
Run the spectral matching workflow as an ordered pipeline.
``steps`` defines the exact step order. Intermediate outputs are written inside the pipeline temp directory. The final step writes to ``shared_output_image_path``:
- If the final step writes multiple rasters, ``shared_output_image_path`` must be a folder, a template containing ``$``, or a list of paths.
- If the final step is merge with merge_rasters_output_tiles=True, ``shared_output_image_path`` must be a folder without ``$``; the result's output is that folder.
- If the final step writes a single raster or vector, it must be a single file path without ``$``.
Step-specific options use the underlying function's types and defaults. Shared cache and worker settings additionally support "auto" and default to it; shared_window_size defaults to 1024. Required inputs for optional steps are required only when selecting those steps.
Args:
shared_window_scales: Overview factors shared by all steps with build_overviews enabled, default (2, 4, 8, 16, 32); None or an empty tuple disables overview creation for those steps.
shared_window_size: Processing window size; for tiled merge, also the output tile width and height in pixels, default 1024.
shared_image_threads: Workers across images in other steps and across output tiles in tiled merge; single-file merge does not use this setting.
shared_concurrent_processing_backend: Shared process_pool or dask backend; forwarded to merge only with merge_rasters_output_tiles=True.
shared_dask_scheduler: Shared Dask scheduler; forwarded to merge only with merge_rasters_output_tiles=True.
shared_resume_from_steps: Shared resume mode: "no" overwrites, "yes" skips existing outputs, and "validate" checks existing outputs before reuse; tiled merge applies this to existing tiles in the output folder.
global_regression_pif_method: PIF selection method, default "flood_from_match_points", matching Match.global_regression.
global_regression_pif_max_samples: Maximum number of PIF samples, default 10000; None disables the cap.
global_regression_pif_min_samples: Minimum number of PIF samples, default 10.
weighted_seamline_input_polygons: Input polygon path, required when steps includes weighted_seamline.
weighted_seamline_rank_function: Ranking expression, required when steps includes weighted_seamline.
merge_rasters_output_tiles: Create GeoTIFF tiles with gdal_retile in shared_output_image_path instead of a single GeoTIFF, default False.
joint_coregistration_resolution: Shared pixel size strategy (highest, average, lowest), positive int or float pixel size in CRS units, or None to preserve native resolution.
align_rasters_resolution: Shared pixel size strategy (highest, average, lowest), positive int or float pixel size in CRS units, or None to preserve native resolution.
merge_rasters_resolution: Merge resolution strategy (highest, average, lowest) or a positive int or float specifying square output pixels in CRS units for either merge mode; default highest.
merge_rasters_overlap: Overlap in pixels between output tiles, default 0; nonzero values require tiled merge and must be smaller than shared_window_size.
merge_rasters_build_overviews: Build overviews using shared_window_scales, default False; tiled merge creates pyramid tiles in numbered subfolders and requires consecutive powers of two starting at 2.
merge_rasters_resampling_method: Resampling for both merge modes: nearest (or near), bilinear, cubic, cubicspline, or lanczos, default nearest.
merge_rasters_custom_tiles_csv: Optional .csv filename within the tile folder; GDAL writes a headerless, semicolon-delimited index with columns tilename;minx;maxx;miny;maxy in the output CRS, also in each pyramid subfolder; requires tiled merge.
merge_rasters_create_vrts: VRT filename within the tile folder, default "MergedImage.vrt"; also creates and links pyramid VRTs as overviews; a custom name requires tiled merge.
"""
_print_step_start("pipeline")
Universal._validate(window_scales=shared_window_scales)
resolved_steps = _validate_pipeline_steps(steps)
last_step = resolved_steps[-1] if resolved_steps else None
_validate_shared_output_for_last_step(
shared_output_image_path=shared_output_image_path,
last_step=last_step,
merge_rasters_output_tiles=merge_rasters_output_tiles,
)
if "merge" in resolved_steps:
UtilsValidation._validate_merge_rasters(
output_tiles=merge_rasters_output_tiles,
output_image_path=shared_output_image_path,
resolution=merge_rasters_resolution,
overlap=merge_rasters_overlap,
window_size=shared_window_size,
window_scales=shared_window_scales,
build_overviews=merge_rasters_build_overviews,
resampling_method=merge_rasters_resampling_method,
custom_tiles_csv=merge_rasters_custom_tiles_csv,
create_vrts=merge_rasters_create_vrts,
resume_from_outputs=shared_resume_from_steps,
)
temp_dir = shared_temp_dir or tempfile.mkdtemp(prefix="spectralmatch_pipeline_")
if delete_temp_dir and os.path.isdir(temp_dir):
shutil.rmtree(temp_dir, ignore_errors=True)
os.makedirs(temp_dir, exist_ok=True)
input_image_paths = _resolve_paths(
"search", shared_input_images, kwargs={"default_file_pattern": "*.tif"}
)
shared_cache, shared_image_threads, shared_io_threads, shared_tile_threads = (
_resolve_auto_shared_settings(
shared_input_images=shared_input_images,
shared_cache=shared_cache,
shared_image_threads=shared_image_threads,
shared_io_threads=shared_io_threads,
shared_tile_threads=shared_tile_threads,
shared_debug_logs=shared_debug_logs,
)
)
PipelineValidation._validate_shared_pipeline(
shared_output_image_path=shared_output_image_path,
shared_temp_dir=shared_temp_dir,
delete_temp_dir=delete_temp_dir,
delete_previous_step=delete_previous_step,
shared_resume_from_steps=shared_resume_from_steps,
)
Universal._validate(
input_images=shared_input_images,
debug_logs=shared_debug_logs,
window_size=shared_window_size,
custom_nodata_value=shared_custom_nodata_value,
calculation_dtype=shared_calculation_dtype,
output_dtype=shared_output_dtype,
cache=shared_cache,
image_threads=shared_image_threads,
io_threads=shared_io_threads,
tile_threads=shared_tile_threads,
save_as_cog=shared_save_as_cog,
concurrent_processing_backend=shared_concurrent_processing_backend,
dask_scheduler=shared_dask_scheduler,
)
start_dt = datetime.now()
start_perf = time.perf_counter()
print(f"Pipeline start: {start_dt.isoformat(timespec='seconds')}")
print(f"Pipeline temp dir: {temp_dir}")
print(f"Number of input images: {len(input_image_paths)}")
current_images: Universal.SearchFolderOrListFiles = shared_input_images
seamline_mask_path: str | None = None
seamline_mask_image_field_name: str | None = None
previous_cleanup_paths: list[str] = []
results: dict[str, Any] = {
"temp_dir": temp_dir,
"input_images": shared_input_images,
"resolved_shared_cache": shared_cache,
"resolved_shared_image_threads": shared_image_threads,
"resolved_shared_io_threads": shared_io_threads,
"resolved_shared_tile_threads": shared_tile_threads,
"shared_concurrent_processing_backend": shared_concurrent_processing_backend,
"shared_dask_scheduler": shared_dask_scheduler,
"num_input_images": len(input_image_paths),
"start_time": start_dt.isoformat(timespec="seconds"),
"steps": resolved_steps,
"shared_resume_from_steps": shared_resume_from_steps,
}
try:
for step_index, step_name in enumerate(resolved_steps):
is_last_step = step_index == len(resolved_steps) - 1
step_cleanup_paths: list[str] = []
if step_name == "joint_coregistration":
output_images = (
shared_output_image_path
if is_last_step
else _step_temp_output(step_name, temp_dir)
)
current_images = joint_coregistration(
input_images=current_images,
output_images=output_images,
global_model=joint_coregistration_global_model,
global_image_position_preservation_weights=joint_coregistration_global_image_position_preservation_weights,
global_tie_point_alignment_strength=joint_coregistration_global_tie_point_alignment_strength,
local_model=joint_coregistration_local_model,
local_image_position_preservation_weights=joint_coregistration_local_image_position_preservation_weights,
local_tie_point_alignment_strength=joint_coregistration_local_tie_point_alignment_strength,
local_grid_spacing=joint_coregistration_local_grid_spacing,
local_smoothness_weight=joint_coregistration_local_smoothness_weight,
local_bending_weight=joint_coregistration_local_bending_weight,
local_anchor_falloff_distance=joint_coregistration_local_anchor_falloff_distance,
feature_method=joint_coregistration_feature_method,
maximum_tie_point_displacement=joint_coregistration_maximum_tie_point_displacement,
ransac_reprojection_threshold=joint_coregistration_ransac_reprojection_threshold,
robust_loss=joint_coregistration_robust_loss,
robust_loss_scale=joint_coregistration_robust_loss_scale,
save_adjustments=joint_coregistration_save_adjustments,
load_adjustments=joint_coregistration_load_adjustments,
resampling_method=joint_coregistration_resampling_method,
tap=joint_coregistration_tap,
resolution=joint_coregistration_resolution,
output_dtype=shared_output_dtype,
custom_nodata_value=shared_custom_nodata_value,
window_size=shared_window_size,
save_as_cog=shared_save_as_cog,
build_overviews=joint_coregistration_build_overviews,
window_scales=shared_window_scales,
cache=shared_cache,
image_threads=shared_image_threads,
io_threads=shared_io_threads,
tile_threads=shared_tile_threads,
concurrent_processing_backend=shared_concurrent_processing_backend,
dask_scheduler=shared_dask_scheduler,
debug_logs=shared_debug_logs,
resume_from_outputs=shared_resume_from_steps,
)
results["joint_coregistration"] = current_images
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, current_images, temp_dir
)
elif step_name == "global_regression":
output_images = (
shared_output_image_path
if is_last_step
else _step_temp_output(step_name, temp_dir)
)
current_images = Match.global_regression(
input_images=current_images,
output_images=output_images,
calculation_dtype=shared_calculation_dtype,
output_dtype=shared_output_dtype,
vector_mask=global_regression_vector_mask,
debug_logs=shared_debug_logs,
custom_nodata_value=shared_custom_nodata_value,
cache=shared_cache,
image_threads=shared_image_threads,
io_threads=shared_io_threads,
tile_threads=shared_tile_threads,
concurrent_processing_backend=shared_concurrent_processing_backend,
dask_scheduler=shared_dask_scheduler,
window_size=shared_window_size,
save_as_cog=shared_save_as_cog,
estimate_stats=global_regression_estimate_stats,
specify_model_images=global_regression_specify_model_images,
custom_mean_factor=global_regression_custom_mean_factor,
custom_std_factor=global_regression_custom_std_factor,
save_adjustments=global_regression_save_adjustments,
load_adjustments=global_regression_load_adjustments,
pif_method=global_regression_pif_method,
pif_red_band_index=global_regression_pif_red_band_index,
pif_nir_band_index=global_regression_pif_nir_band_index,
pif_vegetation_threshold=global_regression_pif_vegetation_threshold,
pif_inz_threshold=global_regression_pif_inz_threshold,
pif_region_radius=global_regression_pif_region_radius,
pif_max_samples=global_regression_pif_max_samples,
pif_min_samples=global_regression_pif_min_samples,
pif_feature_method=global_regression_pif_feature_method,
pif_load_tie_points=global_regression_pif_load_tie_points,
pif_save_inz=global_regression_pif_save_inz,
build_overviews=global_regression_build_overviews,
window_scales=shared_window_scales,
resume_from_outputs=shared_resume_from_steps,
)
results["global_regression"] = current_images
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, current_images, temp_dir
)
elif step_name == "local_block_adjustment":
output_images = (
shared_output_image_path
if is_last_step
else _step_temp_output(step_name, temp_dir)
)
current_images = Match.local_block_adjustment(
input_images=current_images,
output_images=output_images,
calculation_dtype=shared_calculation_dtype,
output_dtype=shared_output_dtype,
vector_mask=local_block_adjustment_vector_mask,
debug_logs=shared_debug_logs,
custom_nodata_value=shared_custom_nodata_value,
cache=shared_cache,
image_threads=shared_image_threads,
io_threads=shared_io_threads,
tile_threads=shared_tile_threads,
concurrent_processing_backend=shared_concurrent_processing_backend,
dask_scheduler=shared_dask_scheduler,
window_size=shared_window_size,
save_as_cog=shared_save_as_cog,
number_of_blocks=local_block_adjustment_number_of_blocks,
alpha=local_block_adjustment_alpha,
correction_method=local_block_adjustment_correction_method,
save_block_maps=local_block_adjustment_save_block_maps,
load_block_maps=local_block_adjustment_load_block_maps,
override_bounds_canvas_coords=local_block_adjustment_override_bounds_canvas_coords,
build_overviews=local_block_adjustment_build_overviews,
window_scales=shared_window_scales,
resume_from_outputs=shared_resume_from_steps,
)
results["local_block_adjustment"] = current_images
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, current_images, temp_dir
)
elif step_name == "align":
output_images = (
shared_output_image_path
if is_last_step
else _step_temp_output(step_name, temp_dir)
)
current_images = align_rasters(
input_images=current_images,
output_images=output_images,
resampling_method=align_rasters_resampling_method,
tap=align_rasters_tap,
resolution=align_rasters_resolution,
window_size=shared_window_size,
debug_logs=shared_debug_logs,
cache=shared_cache,
image_threads=shared_image_threads,
io_threads=shared_io_threads,
tile_threads=shared_tile_threads,
concurrent_processing_backend=shared_concurrent_processing_backend,
dask_scheduler=shared_dask_scheduler,
resume_from_outputs=shared_resume_from_steps,
)
results["align"] = current_images
results["align_rasters"] = current_images
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, current_images, temp_dir
)
elif step_name == "voronoi_center_seamline":
seamline_mask_path = (
shared_output_image_path
if is_last_step
else _step_temp_output(step_name, temp_dir)
)
if not isinstance(seamline_mask_path, str):
raise ValueError(
"shared_output_image_path must be a single file path when the final step is a seamline."
)
seamline_mask_image_field_name = voronoi_center_seamline_image_field_name
Seamline.voronoi(
input_images=current_images,
output_mask=seamline_mask_path,
aoi_path=voronoi_center_seamline_aoi_path,
vector_mask=voronoi_center_seamline_vector_mask,
image_field_name=voronoi_center_seamline_image_field_name,
min_point_spacing=voronoi_center_seamline_min_point_spacing,
min_cut_length=voronoi_center_seamline_min_cut_length,
debug_logs=shared_debug_logs,
debug_vectors_path=voronoi_center_seamline_debug_vectors_path,
resume_from_outputs=shared_resume_from_steps,
)
results["voronoi_center_seamline"] = seamline_mask_path
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, seamline_mask_path, temp_dir
)
elif step_name == "weighted_seamline":
seamline_mask_path = (
shared_output_image_path
if is_last_step
else _step_temp_output(step_name, temp_dir)
)
if not isinstance(seamline_mask_path, str):
raise ValueError(
"shared_output_image_path must be a single file path when the final step is a seamline."
)
seamline_mask_image_field_name = weighted_seamline_image_field_name
Seamline.weighted(
input_polygons=weighted_seamline_input_polygons,
output_mask=seamline_mask_path,
rank_function=weighted_seamline_rank_function,
image_field_name=weighted_seamline_image_field_name,
input_layer=weighted_seamline_input_layer,
output_layer=weighted_seamline_output_layer,
rank_descending=weighted_seamline_rank_descending,
debug_logs=shared_debug_logs,
resume_from_outputs=shared_resume_from_steps,
)
results["weighted_seamline"] = seamline_mask_path
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, seamline_mask_path, temp_dir
)
elif step_name == "mask":
output_images = (
shared_output_image_path
if is_last_step
else _step_temp_output(step_name, temp_dir)
)
clip_vector_mask = mask_rasters_vector_mask
if clip_vector_mask is None and seamline_mask_path is not None:
clip_vector_mask = (
"include",
seamline_mask_path,
seamline_mask_image_field_name,
)
if clip_vector_mask is None:
raise ValueError(
"mask_rasters requires a vector mask. Set mask_rasters_vector_mask "
"or run a seamline step earlier in the pipeline."
)
current_images = mask_rasters(
input_images=current_images,
output_images=output_images,
vector_mask=clip_vector_mask,
window_size=shared_window_size,
debug_logs=shared_debug_logs,
cache=shared_cache,
image_threads=shared_image_threads,
io_threads=shared_io_threads,
tile_threads=shared_tile_threads,
concurrent_processing_backend=shared_concurrent_processing_backend,
dask_scheduler=shared_dask_scheduler,
include_touched_pixels=mask_rasters_include_touched_pixels,
custom_nodata_value=shared_custom_nodata_value,
resume_from_outputs=shared_resume_from_steps,
)
results["mask"] = current_images
results["mask_rasters"] = current_images
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, current_images, temp_dir
)
elif step_name == "merge":
if not isinstance(shared_output_image_path, str):
raise ValueError(
"shared_output_image_path must be a path string for merge (a folder when merge_rasters_output_tiles=True)."
)
merged_output = merge_rasters(
input_images=current_images,
output_image_path=shared_output_image_path,
output_tiles=merge_rasters_output_tiles,
cache=shared_cache,
image_threads=shared_image_threads if merge_rasters_output_tiles else None,
concurrent_processing_backend=shared_concurrent_processing_backend if merge_rasters_output_tiles else None,
dask_scheduler=shared_dask_scheduler if merge_rasters_output_tiles else None,
io_threads=shared_io_threads,
tile_threads=shared_tile_threads,
debug_logs=shared_debug_logs,
output_dtype=shared_output_dtype,
custom_nodata_value=shared_custom_nodata_value,
resolution=merge_rasters_resolution,
overlap=merge_rasters_overlap,
resampling_method=merge_rasters_resampling_method,
custom_tiles_csv=merge_rasters_custom_tiles_csv,
create_vrts=merge_rasters_create_vrts,
window_size=shared_window_size,
build_overviews=merge_rasters_build_overviews,
window_scales=shared_window_scales,
resume_from_outputs=shared_resume_from_steps,
)
current_images = merged_output
results["merge"] = merged_output
results["merge_rasters"] = merged_output
step_cleanup_paths = _collect_step_cleanup_paths(
step_name, merged_output, temp_dir
)
else:
raise ValueError(f"Unsupported pipeline step: {step_name}")
if delete_previous_step and previous_cleanup_paths:
_delete_step_outputs_if_inactive(
previous_cleanup_paths=previous_cleanup_paths,
current_images=current_images,
seamline_mask_path=seamline_mask_path,
temp_dir=temp_dir,
debug_logs=shared_debug_logs,
)
previous_cleanup_paths = step_cleanup_paths
if not resolved_steps:
results["output"] = current_images
elif last_step in SEAMLINE_STEPS:
results["output"] = seamline_mask_path
else:
results["output"] = current_images
end_dt = datetime.now()
duration_seconds = round(time.perf_counter() - start_perf, 2)
final_results = {
"output": results["output"],
"temp_dir": temp_dir,
"num_input_images": len(input_image_paths),
"start_time": start_dt.isoformat(timespec="seconds"),
"end_time": end_dt.isoformat(timespec="seconds"),
"duration_seconds": duration_seconds,
"resolved_shared_cache": shared_cache,
"resolved_shared_image_threads": shared_image_threads,
"resolved_shared_io_threads": shared_io_threads,
"resolved_shared_tile_threads": shared_tile_threads,
}
for key, value in results.items():
if key not in final_results and value is not None:
final_results[key] = value
return final_results
finally:
if delete_temp_dir and os.path.isdir(temp_dir):
shutil.rmtree(temp_dir, ignore_errors=True)
|