oxigdal-workflow 0.1.4

DAG-based workflow engine for complex geospatial processing pipelines
Documentation
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
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
//! Common geospatial workflow patterns and templates.
//!
//! This module provides predefined workflow templates for common geospatial
//! operations including:
//! - ETL (Extract, Transform, Load) workflows
//! - Mosaic operations
//! - Reprojection pipelines
//! - Raster calculations
//! - Vector processing
//! - Tile generation

use crate::dag::{ResourceRequirements, RetryPolicy, TaskNode, WorkflowDag};
use crate::engine::WorkflowDefinition;
use crate::error::{Result, WorkflowError};
use crate::templates::{
    Parameter, ParameterConstraints, ParameterType, ParameterValue, TemplateBuilder,
    TemplateCategory, WorkflowTemplate,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Geospatial workflow pattern types.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GeospatialPattern {
    /// Extract, Transform, Load pattern for geospatial data.
    Etl,
    /// Mosaic multiple rasters into a single output.
    Mosaic,
    /// Reproject data between coordinate reference systems.
    Reproject,
    /// Raster algebra and band calculations.
    RasterCalc,
    /// Vector data processing pipeline.
    VectorProcessing,
    /// Tile generation for web mapping.
    TileGeneration,
    /// Image classification pipeline.
    Classification,
    /// Time series analysis for multi-temporal data.
    TimeSeries,
    /// DEM-based terrain analysis.
    TerrainAnalysis,
    /// Point cloud processing pipeline.
    PointCloud,
}

/// Coordinate reference system specification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrsSpec {
    /// EPSG code or WKT definition.
    pub definition: String,
    /// Whether this is an EPSG code.
    pub is_epsg: bool,
    /// Axis order specification.
    pub axis_order: AxisOrder,
}

impl CrsSpec {
    /// Create a new CRS specification from an EPSG code.
    pub fn from_epsg(code: u32) -> Self {
        Self {
            definition: format!("EPSG:{}", code),
            is_epsg: true,
            axis_order: AxisOrder::EastNorth,
        }
    }

    /// Create a CRS specification from WKT.
    pub fn from_wkt(wkt: String) -> Self {
        Self {
            definition: wkt,
            is_epsg: false,
            axis_order: AxisOrder::EastNorth,
        }
    }

    /// Get the EPSG code if available.
    pub fn epsg_code(&self) -> Option<u32> {
        if self.is_epsg {
            self.definition
                .strip_prefix("EPSG:")
                .and_then(|s| s.parse().ok())
        } else {
            None
        }
    }
}

/// Axis order for coordinate systems.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AxisOrder {
    /// East-North (X-Y) ordering.
    EastNorth,
    /// North-East (Y-X) ordering.
    NorthEast,
    /// Custom axis ordering.
    Custom(Vec<String>),
}

/// Resampling method for raster operations.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ResamplingMethod {
    /// Nearest neighbor interpolation.
    NearestNeighbor,
    /// Bilinear interpolation.
    Bilinear,
    /// Bicubic interpolation.
    Bicubic,
    /// Cubic spline interpolation.
    CubicSpline,
    /// Lanczos windowed sinc.
    Lanczos,
    /// Average of contributing pixels.
    Average,
    /// Mode (most common value).
    Mode,
    /// Minimum value.
    Min,
    /// Maximum value.
    Max,
    /// Median value.
    Median,
    /// Sum of values.
    Sum,
}

impl ResamplingMethod {
    /// Get the GDAL algorithm name.
    pub fn gdal_name(&self) -> &'static str {
        match self {
            Self::NearestNeighbor => "near",
            Self::Bilinear => "bilinear",
            Self::Bicubic => "cubic",
            Self::CubicSpline => "cubicspline",
            Self::Lanczos => "lanczos",
            Self::Average => "average",
            Self::Mode => "mode",
            Self::Min => "min",
            Self::Max => "max",
            Self::Median => "med",
            Self::Sum => "sum",
        }
    }
}

/// Output format specification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputFormat {
    /// Format driver name (e.g., "GTiff", "COG", "GPKG").
    pub driver: String,
    /// Creation options.
    pub creation_options: HashMap<String, String>,
    /// Compression type.
    pub compression: Option<CompressionType>,
    /// Tile size for tiled formats.
    pub tile_size: Option<(u32, u32)>,
}

impl OutputFormat {
    /// Create a Cloud-Optimized GeoTIFF format specification.
    pub fn cog() -> Self {
        let mut options = HashMap::new();
        options.insert("COMPRESS".to_string(), "LZW".to_string());
        options.insert("TILED".to_string(), "YES".to_string());
        options.insert("COPY_SRC_OVERVIEWS".to_string(), "YES".to_string());

        Self {
            driver: "COG".to_string(),
            creation_options: options,
            compression: Some(CompressionType::Lzw),
            tile_size: Some((512, 512)),
        }
    }

    /// Create a standard GeoTIFF format specification.
    pub fn geotiff() -> Self {
        let mut options = HashMap::new();
        options.insert("COMPRESS".to_string(), "DEFLATE".to_string());
        options.insert("TILED".to_string(), "YES".to_string());

        Self {
            driver: "GTiff".to_string(),
            creation_options: options,
            compression: Some(CompressionType::Deflate),
            tile_size: Some((256, 256)),
        }
    }

    /// Create a GeoPackage format specification.
    pub fn geopackage() -> Self {
        Self {
            driver: "GPKG".to_string(),
            creation_options: HashMap::new(),
            compression: None,
            tile_size: None,
        }
    }
}

/// Compression types for raster data.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompressionType {
    /// No compression.
    None,
    /// LZW compression.
    Lzw,
    /// DEFLATE compression.
    Deflate,
    /// ZSTD compression.
    Zstd,
    /// JPEG compression.
    Jpeg,
    /// JPEG 2000 compression.
    Jp2,
    /// WebP compression.
    Webp,
}

/// ETL workflow configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EtlConfig {
    /// Source data paths or patterns.
    pub sources: Vec<String>,
    /// Destination path.
    pub destination: String,
    /// Transformation steps.
    pub transforms: Vec<TransformStep>,
    /// Output format.
    pub output_format: OutputFormat,
    /// Whether to validate input data.
    pub validate_input: bool,
    /// Whether to create overviews.
    pub create_overviews: bool,
    /// Parallel processing level.
    pub parallelism: u32,
}

/// Transformation step in an ETL pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformStep {
    /// Step name.
    pub name: String,
    /// Step type.
    pub step_type: TransformType,
    /// Step parameters.
    pub params: HashMap<String, serde_json::Value>,
}

/// Types of transformation operations.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransformType {
    /// Reproject to a different CRS.
    Reproject,
    /// Clip to a boundary.
    Clip,
    /// Apply a raster calculation.
    Calculate,
    /// Resample to different resolution.
    Resample,
    /// Apply a color map.
    ColorMap,
    /// Filter data by attribute or value.
    Filter,
    /// Convert data type.
    Convert,
    /// Apply a buffer.
    Buffer,
    /// Simplify geometry.
    Simplify,
}

/// Mosaic configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MosaicConfig {
    /// Input raster paths.
    pub inputs: Vec<String>,
    /// Output path.
    pub output: String,
    /// NoData value.
    pub nodata: Option<f64>,
    /// Resampling method.
    pub resampling: ResamplingMethod,
    /// Output resolution (optional, uses input resolution if not specified).
    pub target_resolution: Option<(f64, f64)>,
    /// Target CRS (optional, uses first input CRS if not specified).
    pub target_crs: Option<CrsSpec>,
    /// Blend mode for overlapping areas.
    pub blend_mode: BlendMode,
    /// Output format.
    pub output_format: OutputFormat,
}

/// Blend mode for mosaic operations.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BlendMode {
    /// First valid value (from input order).
    First,
    /// Last valid value (from input order).
    Last,
    /// Average of overlapping values.
    Average,
    /// Maximum value.
    Max,
    /// Minimum value.
    Min,
}

/// Reprojection configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReprojectConfig {
    /// Source CRS.
    pub source_crs: Option<CrsSpec>,
    /// Target CRS.
    pub target_crs: CrsSpec,
    /// Resampling method.
    pub resampling: ResamplingMethod,
    /// Target resolution (optional).
    pub target_resolution: Option<(f64, f64)>,
    /// Target extent (optional, [xmin, ymin, xmax, ymax]).
    pub target_extent: Option<[f64; 4]>,
    /// Error threshold for approximation.
    pub error_threshold: f64,
    /// Memory limit in MB.
    pub memory_limit_mb: u32,
}

impl Default for ReprojectConfig {
    fn default() -> Self {
        Self {
            source_crs: None,
            target_crs: CrsSpec::from_epsg(4326),
            resampling: ResamplingMethod::Bilinear,
            target_resolution: None,
            target_extent: None,
            error_threshold: 0.125,
            memory_limit_mb: 500,
        }
    }
}

/// Factory for creating geospatial workflow templates.
pub struct GeospatialTemplateFactory;

impl GeospatialTemplateFactory {
    /// Create an ETL workflow template.
    pub fn create_etl_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "geospatial-etl",
            "Geospatial ETL Pipeline",
            "Extract, transform, and load geospatial data with validation and optimization",
        );

        template.set_category(TemplateCategory::Etl);
        template.metadata.complexity = 3;
        template.add_tag("etl");
        template.add_tag("geospatial");
        template.add_tag("pipeline");

        // Source parameters
        template.add_parameter(Parameter {
            name: "source_paths".to_string(),
            param_type: ParameterType::Array,
            description: "List of source data paths or patterns".to_string(),
            required: true,
            default_value: None,
            constraints: Some(ParameterConstraints {
                min: None,
                max: None,
                min_length: Some(1),
                max_length: Some(1000),
                pattern: None,
            }),
        });

        // Destination parameter
        template.add_parameter(Parameter {
            name: "destination".to_string(),
            param_type: ParameterType::FilePath,
            description: "Output destination path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Output format
        template.add_parameter(Parameter {
            name: "output_format".to_string(),
            param_type: ParameterType::Enum {
                allowed_values: vec![
                    "COG".to_string(),
                    "GTiff".to_string(),
                    "GPKG".to_string(),
                    "Parquet".to_string(),
                ],
            },
            description: "Output format driver".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("COG".to_string())),
            constraints: None,
        });

        // Validation flag
        template.add_parameter(Parameter {
            name: "validate_input".to_string(),
            param_type: ParameterType::Boolean,
            description: "Validate input data before processing".to_string(),
            required: false,
            default_value: Some(ParameterValue::Boolean(true)),
            constraints: None,
        });

        // Parallelism
        template.add_parameter(Parameter {
            name: "parallelism".to_string(),
            param_type: ParameterType::Integer,
            description: "Number of parallel workers".to_string(),
            required: false,
            default_value: Some(ParameterValue::Integer(4)),
            constraints: Some(ParameterConstraints {
                min: Some(1.0),
                max: Some(64.0),
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        template.set_template(Self::etl_template_json());

        Ok(template)
    }

    /// Create a mosaic workflow template.
    pub fn create_mosaic_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "geospatial-mosaic",
            "Raster Mosaic Pipeline",
            "Combine multiple rasters into a seamless mosaic with configurable blending",
        );

        template.set_category(TemplateCategory::BatchProcessing);
        template.metadata.complexity = 3;
        template.add_tag("mosaic");
        template.add_tag("raster");
        template.add_tag("merge");

        // Input files
        template.add_parameter(Parameter {
            name: "input_files".to_string(),
            param_type: ParameterType::Array,
            description: "List of input raster files to mosaic".to_string(),
            required: true,
            default_value: None,
            constraints: Some(ParameterConstraints {
                min: None,
                max: None,
                min_length: Some(2),
                max_length: Some(10000),
                pattern: None,
            }),
        });

        // Output path
        template.add_parameter(Parameter {
            name: "output_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Output mosaic file path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // NoData value
        template.add_parameter(Parameter {
            name: "nodata".to_string(),
            param_type: ParameterType::Float,
            description: "NoData value for output".to_string(),
            required: false,
            default_value: Some(ParameterValue::Float(-9999.0)),
            constraints: None,
        });

        // Resampling method
        template.add_parameter(Parameter {
            name: "resampling".to_string(),
            param_type: ParameterType::Enum {
                allowed_values: vec![
                    "near".to_string(),
                    "bilinear".to_string(),
                    "cubic".to_string(),
                    "cubicspline".to_string(),
                    "lanczos".to_string(),
                    "average".to_string(),
                ],
            },
            description: "Resampling method for warping".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("bilinear".to_string())),
            constraints: None,
        });

        // Blend mode
        template.add_parameter(Parameter {
            name: "blend_mode".to_string(),
            param_type: ParameterType::Enum {
                allowed_values: vec![
                    "first".to_string(),
                    "last".to_string(),
                    "average".to_string(),
                    "max".to_string(),
                    "min".to_string(),
                ],
            },
            description: "Blend mode for overlapping areas".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("first".to_string())),
            constraints: None,
        });

        // Target CRS
        template.add_parameter(Parameter {
            name: "target_crs".to_string(),
            param_type: ParameterType::String,
            description: "Target coordinate reference system (EPSG code or WKT)".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("EPSG:4326".to_string())),
            constraints: None,
        });

        template.set_template(Self::mosaic_template_json());

        Ok(template)
    }

    /// Create a reprojection workflow template.
    pub fn create_reproject_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "geospatial-reproject",
            "Coordinate Reprojection Pipeline",
            "Transform geospatial data between coordinate reference systems",
        );

        template.set_category(TemplateCategory::SatelliteProcessing);
        template.metadata.complexity = 2;
        template.add_tag("reproject");
        template.add_tag("crs");
        template.add_tag("transform");

        // Input path
        template.add_parameter(Parameter {
            name: "input_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Input file or directory path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Output path
        template.add_parameter(Parameter {
            name: "output_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Output file or directory path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Source CRS
        template.add_parameter(Parameter {
            name: "source_crs".to_string(),
            param_type: ParameterType::String,
            description: "Source CRS (optional, auto-detect if not provided)".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("auto".to_string())),
            constraints: None,
        });

        // Target CRS
        template.add_parameter(Parameter {
            name: "target_crs".to_string(),
            param_type: ParameterType::String,
            description: "Target CRS (EPSG code or WKT)".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Resampling method
        template.add_parameter(Parameter {
            name: "resampling".to_string(),
            param_type: ParameterType::Enum {
                allowed_values: vec![
                    "near".to_string(),
                    "bilinear".to_string(),
                    "cubic".to_string(),
                    "cubicspline".to_string(),
                    "lanczos".to_string(),
                ],
            },
            description: "Resampling algorithm".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("bilinear".to_string())),
            constraints: None,
        });

        // Target resolution
        template.add_parameter(Parameter {
            name: "target_resolution_x".to_string(),
            param_type: ParameterType::Float,
            description: "Target X resolution (optional)".to_string(),
            required: false,
            default_value: None,
            constraints: Some(ParameterConstraints {
                min: Some(0.0),
                max: None,
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        template.add_parameter(Parameter {
            name: "target_resolution_y".to_string(),
            param_type: ParameterType::Float,
            description: "Target Y resolution (optional)".to_string(),
            required: false,
            default_value: None,
            constraints: Some(ParameterConstraints {
                min: Some(0.0),
                max: None,
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        template.set_template(Self::reproject_template_json());

        Ok(template)
    }

    /// Create a tile generation workflow template.
    pub fn create_tile_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "geospatial-tiles",
            "Map Tile Generation Pipeline",
            "Generate XYZ/TMS map tiles from raster data",
        );

        template.set_category(TemplateCategory::BatchProcessing);
        template.metadata.complexity = 3;
        template.add_tag("tiles");
        template.add_tag("xyz");
        template.add_tag("web-mapping");

        // Input path
        template.add_parameter(Parameter {
            name: "input_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Input raster file path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Output directory
        template.add_parameter(Parameter {
            name: "output_directory".to_string(),
            param_type: ParameterType::FilePath,
            description: "Output tile directory".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Min zoom
        template.add_parameter(Parameter {
            name: "min_zoom".to_string(),
            param_type: ParameterType::Integer,
            description: "Minimum zoom level".to_string(),
            required: false,
            default_value: Some(ParameterValue::Integer(0)),
            constraints: Some(ParameterConstraints {
                min: Some(0.0),
                max: Some(24.0),
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        // Max zoom
        template.add_parameter(Parameter {
            name: "max_zoom".to_string(),
            param_type: ParameterType::Integer,
            description: "Maximum zoom level".to_string(),
            required: false,
            default_value: Some(ParameterValue::Integer(18)),
            constraints: Some(ParameterConstraints {
                min: Some(0.0),
                max: Some(24.0),
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        // Tile format
        template.add_parameter(Parameter {
            name: "tile_format".to_string(),
            param_type: ParameterType::Enum {
                allowed_values: vec![
                    "png".to_string(),
                    "jpeg".to_string(),
                    "webp".to_string(),
                ],
            },
            description: "Output tile format".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("png".to_string())),
            constraints: None,
        });

        // Tile size
        template.add_parameter(Parameter {
            name: "tile_size".to_string(),
            param_type: ParameterType::Integer,
            description: "Tile size in pixels".to_string(),
            required: false,
            default_value: Some(ParameterValue::Integer(256)),
            constraints: Some(ParameterConstraints {
                min: Some(64.0),
                max: Some(4096.0),
                min_length: None,
                max_length: None,
                pattern: None,
            }),
        });

        template.set_template(Self::tile_template_json());

        Ok(template)
    }

    /// Create a raster calculation workflow template.
    pub fn create_raster_calc_template() -> Result<WorkflowTemplate> {
        let mut template = WorkflowTemplate::new(
            "geospatial-raster-calc",
            "Raster Calculator Pipeline",
            "Perform band algebra and raster calculations",
        );

        template.set_category(TemplateCategory::SatelliteProcessing);
        template.metadata.complexity = 2;
        template.add_tag("raster");
        template.add_tag("calculation");
        template.add_tag("band-math");

        // Input path
        template.add_parameter(Parameter {
            name: "input_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Input raster file path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Expression
        template.add_parameter(Parameter {
            name: "expression".to_string(),
            param_type: ParameterType::String,
            description: "Calculation expression (e.g., '(B4-B3)/(B4+B3)' for NDVI)".to_string(),
            required: true,
            default_value: None,
            constraints: Some(ParameterConstraints {
                min: None,
                max: None,
                min_length: Some(1),
                max_length: Some(1000),
                pattern: None,
            }),
        });

        // Output path
        template.add_parameter(Parameter {
            name: "output_path".to_string(),
            param_type: ParameterType::FilePath,
            description: "Output raster file path".to_string(),
            required: true,
            default_value: None,
            constraints: None,
        });

        // Output data type
        template.add_parameter(Parameter {
            name: "output_dtype".to_string(),
            param_type: ParameterType::Enum {
                allowed_values: vec![
                    "Float32".to_string(),
                    "Float64".to_string(),
                    "Int16".to_string(),
                    "Int32".to_string(),
                    "UInt8".to_string(),
                    "UInt16".to_string(),
                ],
            },
            description: "Output data type".to_string(),
            required: false,
            default_value: Some(ParameterValue::String("Float32".to_string())),
            constraints: None,
        });

        template.set_template(Self::raster_calc_template_json());

        Ok(template)
    }

    /// ETL template JSON.
    fn etl_template_json() -> &'static str {
        r#"{
            "id": "etl-{{workflow_id}}",
            "name": "{{workflow_name}}",
            "version": "1.0.0",
            "dag": {
                "nodes": [
                    {
                        "id": "validate",
                        "name": "Validate Input",
                        "config": {
                            "operation": "validate",
                            "sources": "{{source_paths}}"
                        }
                    },
                    {
                        "id": "extract",
                        "name": "Extract Data",
                        "config": {
                            "operation": "extract",
                            "sources": "{{source_paths}}"
                        }
                    },
                    {
                        "id": "transform",
                        "name": "Transform Data",
                        "config": {
                            "operation": "transform",
                            "parallelism": "{{parallelism}}"
                        }
                    },
                    {
                        "id": "load",
                        "name": "Load Data",
                        "config": {
                            "operation": "load",
                            "destination": "{{destination}}",
                            "format": "{{output_format}}"
                        }
                    }
                ],
                "edges": [
                    {"from": "validate", "to": "extract"},
                    {"from": "extract", "to": "transform"},
                    {"from": "transform", "to": "load"}
                ]
            },
            "description": "ETL workflow for geospatial data processing"
        }"#
    }

    /// Mosaic template JSON.
    fn mosaic_template_json() -> &'static str {
        r#"{
            "id": "mosaic-{{workflow_id}}",
            "name": "{{workflow_name}}",
            "version": "1.0.0",
            "dag": {
                "nodes": [
                    {
                        "id": "collect",
                        "name": "Collect Inputs",
                        "config": {
                            "operation": "collect",
                            "inputs": "{{input_files}}"
                        }
                    },
                    {
                        "id": "analyze",
                        "name": "Analyze Extents",
                        "config": {
                            "operation": "analyze_extents",
                            "target_crs": "{{target_crs}}"
                        }
                    },
                    {
                        "id": "mosaic",
                        "name": "Create Mosaic",
                        "config": {
                            "operation": "mosaic",
                            "nodata": "{{nodata}}",
                            "resampling": "{{resampling}}",
                            "blend_mode": "{{blend_mode}}"
                        }
                    },
                    {
                        "id": "output",
                        "name": "Write Output",
                        "config": {
                            "operation": "write",
                            "output_path": "{{output_path}}"
                        }
                    }
                ],
                "edges": [
                    {"from": "collect", "to": "analyze"},
                    {"from": "analyze", "to": "mosaic"},
                    {"from": "mosaic", "to": "output"}
                ]
            },
            "description": "Mosaic workflow for combining multiple rasters"
        }"#
    }

    /// Reproject template JSON.
    fn reproject_template_json() -> &'static str {
        r#"{
            "id": "reproject-{{workflow_id}}",
            "name": "{{workflow_name}}",
            "version": "1.0.0",
            "dag": {
                "nodes": [
                    {
                        "id": "detect_crs",
                        "name": "Detect Source CRS",
                        "config": {
                            "operation": "detect_crs",
                            "input_path": "{{input_path}}",
                            "source_crs": "{{source_crs}}"
                        }
                    },
                    {
                        "id": "reproject",
                        "name": "Reproject Data",
                        "config": {
                            "operation": "reproject",
                            "target_crs": "{{target_crs}}",
                            "resampling": "{{resampling}}"
                        }
                    },
                    {
                        "id": "write",
                        "name": "Write Output",
                        "config": {
                            "operation": "write",
                            "output_path": "{{output_path}}"
                        }
                    }
                ],
                "edges": [
                    {"from": "detect_crs", "to": "reproject"},
                    {"from": "reproject", "to": "write"}
                ]
            },
            "description": "Reprojection workflow for CRS transformation"
        }"#
    }

    /// Tile generation template JSON.
    fn tile_template_json() -> &'static str {
        r#"{
            "id": "tiles-{{workflow_id}}",
            "name": "{{workflow_name}}",
            "version": "1.0.0",
            "dag": {
                "nodes": [
                    {
                        "id": "prepare",
                        "name": "Prepare Input",
                        "config": {
                            "operation": "prepare_for_tiling",
                            "input_path": "{{input_path}}"
                        }
                    },
                    {
                        "id": "tile",
                        "name": "Generate Tiles",
                        "config": {
                            "operation": "generate_tiles",
                            "min_zoom": "{{min_zoom}}",
                            "max_zoom": "{{max_zoom}}",
                            "tile_format": "{{tile_format}}",
                            "tile_size": "{{tile_size}}"
                        }
                    },
                    {
                        "id": "output",
                        "name": "Write Tiles",
                        "config": {
                            "operation": "write_tiles",
                            "output_directory": "{{output_directory}}"
                        }
                    }
                ],
                "edges": [
                    {"from": "prepare", "to": "tile"},
                    {"from": "tile", "to": "output"}
                ]
            },
            "description": "Tile generation workflow for web mapping"
        }"#
    }

    /// Raster calculation template JSON.
    fn raster_calc_template_json() -> &'static str {
        r#"{
            "id": "raster-calc-{{workflow_id}}",
            "name": "{{workflow_name}}",
            "version": "1.0.0",
            "dag": {
                "nodes": [
                    {
                        "id": "read",
                        "name": "Read Input",
                        "config": {
                            "operation": "read_raster",
                            "input_path": "{{input_path}}"
                        }
                    },
                    {
                        "id": "calculate",
                        "name": "Calculate",
                        "config": {
                            "operation": "raster_calc",
                            "expression": "{{expression}}"
                        }
                    },
                    {
                        "id": "write",
                        "name": "Write Output",
                        "config": {
                            "operation": "write_raster",
                            "output_path": "{{output_path}}",
                            "dtype": "{{output_dtype}}"
                        }
                    }
                ],
                "edges": [
                    {"from": "read", "to": "calculate"},
                    {"from": "calculate", "to": "write"}
                ]
            },
            "description": "Raster calculation workflow"
        }"#
    }

    /// Create all geospatial templates and return them.
    pub fn create_all_templates() -> Vec<Result<WorkflowTemplate>> {
        vec![
            Self::create_etl_template(),
            Self::create_mosaic_template(),
            Self::create_reproject_template(),
            Self::create_tile_template(),
            Self::create_raster_calc_template(),
        ]
    }
}

/// Instantiation helper for geospatial templates.
pub struct GeospatialInstantiator {
    /// Default output format.
    default_format: OutputFormat,
    /// Default CRS.
    default_crs: CrsSpec,
    /// Default resampling method.
    default_resampling: ResamplingMethod,
}

impl GeospatialInstantiator {
    /// Create a new geospatial instantiator with defaults.
    pub fn new() -> Self {
        Self {
            default_format: OutputFormat::cog(),
            default_crs: CrsSpec::from_epsg(4326),
            default_resampling: ResamplingMethod::Bilinear,
        }
    }

    /// Set the default output format.
    pub fn with_format(mut self, format: OutputFormat) -> Self {
        self.default_format = format;
        self
    }

    /// Set the default CRS.
    pub fn with_crs(mut self, crs: CrsSpec) -> Self {
        self.default_crs = crs;
        self
    }

    /// Set the default resampling method.
    pub fn with_resampling(mut self, resampling: ResamplingMethod) -> Self {
        self.default_resampling = resampling;
        self
    }

    /// Instantiate an ETL workflow.
    pub fn instantiate_etl(
        &self,
        template: &WorkflowTemplate,
        config: &EtlConfig,
    ) -> Result<WorkflowDefinition> {
        let mut params = HashMap::new();

        let sources_json = serde_json::to_value(&config.sources).map_err(|e| {
            WorkflowError::template(format!("Failed to serialize sources: {}", e))
        })?;

        params.insert(
            "source_paths".to_string(),
            ParameterValue::Array(
                config
                    .sources
                    .iter()
                    .map(|s| ParameterValue::String(s.clone()))
                    .collect(),
            ),
        );
        params.insert(
            "destination".to_string(),
            ParameterValue::String(config.destination.clone()),
        );
        params.insert(
            "output_format".to_string(),
            ParameterValue::String(config.output_format.driver.clone()),
        );
        params.insert(
            "validate_input".to_string(),
            ParameterValue::Boolean(config.validate_input),
        );
        params.insert(
            "parallelism".to_string(),
            ParameterValue::Integer(config.parallelism as i64),
        );

        template.instantiate(params)
    }

    /// Instantiate a mosaic workflow.
    pub fn instantiate_mosaic(
        &self,
        template: &WorkflowTemplate,
        config: &MosaicConfig,
    ) -> Result<WorkflowDefinition> {
        let mut params = HashMap::new();

        params.insert(
            "input_files".to_string(),
            ParameterValue::Array(
                config
                    .inputs
                    .iter()
                    .map(|s| ParameterValue::String(s.clone()))
                    .collect(),
            ),
        );
        params.insert(
            "output_path".to_string(),
            ParameterValue::String(config.output.clone()),
        );
        params.insert(
            "nodata".to_string(),
            ParameterValue::Float(config.nodata.unwrap_or(-9999.0)),
        );
        params.insert(
            "resampling".to_string(),
            ParameterValue::String(config.resampling.gdal_name().to_string()),
        );

        let blend_mode_str = match &config.blend_mode {
            BlendMode::First => "first",
            BlendMode::Last => "last",
            BlendMode::Average => "average",
            BlendMode::Max => "max",
            BlendMode::Min => "min",
        };
        params.insert(
            "blend_mode".to_string(),
            ParameterValue::String(blend_mode_str.to_string()),
        );

        if let Some(crs) = &config.target_crs {
            params.insert(
                "target_crs".to_string(),
                ParameterValue::String(crs.definition.clone()),
            );
        } else {
            params.insert(
                "target_crs".to_string(),
                ParameterValue::String(self.default_crs.definition.clone()),
            );
        }

        template.instantiate(params)
    }

    /// Instantiate a reprojection workflow.
    pub fn instantiate_reproject(
        &self,
        template: &WorkflowTemplate,
        input_path: &str,
        output_path: &str,
        config: &ReprojectConfig,
    ) -> Result<WorkflowDefinition> {
        let mut params = HashMap::new();

        params.insert(
            "input_path".to_string(),
            ParameterValue::String(input_path.to_string()),
        );
        params.insert(
            "output_path".to_string(),
            ParameterValue::String(output_path.to_string()),
        );

        if let Some(source_crs) = &config.source_crs {
            params.insert(
                "source_crs".to_string(),
                ParameterValue::String(source_crs.definition.clone()),
            );
        } else {
            params.insert(
                "source_crs".to_string(),
                ParameterValue::String("auto".to_string()),
            );
        }

        params.insert(
            "target_crs".to_string(),
            ParameterValue::String(config.target_crs.definition.clone()),
        );
        params.insert(
            "resampling".to_string(),
            ParameterValue::String(config.resampling.gdal_name().to_string()),
        );

        template.instantiate(params)
    }
}

impl Default for GeospatialInstantiator {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_crs_spec_from_epsg() {
        let crs = CrsSpec::from_epsg(4326);
        assert_eq!(crs.definition, "EPSG:4326");
        assert!(crs.is_epsg);
        assert_eq!(crs.epsg_code(), Some(4326));
    }

    #[test]
    fn test_crs_spec_from_wkt() {
        let wkt = r#"GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]]]"#;
        let crs = CrsSpec::from_wkt(wkt.to_string());
        assert!(!crs.is_epsg);
        assert_eq!(crs.epsg_code(), None);
    }

    #[test]
    fn test_resampling_gdal_name() {
        assert_eq!(ResamplingMethod::NearestNeighbor.gdal_name(), "near");
        assert_eq!(ResamplingMethod::Bilinear.gdal_name(), "bilinear");
        assert_eq!(ResamplingMethod::Lanczos.gdal_name(), "lanczos");
    }

    #[test]
    fn test_output_format_cog() {
        let format = OutputFormat::cog();
        assert_eq!(format.driver, "COG");
        assert_eq!(format.tile_size, Some((512, 512)));
    }

    #[test]
    fn test_create_etl_template() {
        let template = GeospatialTemplateFactory::create_etl_template();
        assert!(template.is_ok());
        let template = template.expect("template creation failed");
        assert_eq!(template.id, "geospatial-etl");
    }

    #[test]
    fn test_create_mosaic_template() {
        let template = GeospatialTemplateFactory::create_mosaic_template();
        assert!(template.is_ok());
        let template = template.expect("template creation failed");
        assert_eq!(template.id, "geospatial-mosaic");
    }

    #[test]
    fn test_create_reproject_template() {
        let template = GeospatialTemplateFactory::create_reproject_template();
        assert!(template.is_ok());
        let template = template.expect("template creation failed");
        assert_eq!(template.id, "geospatial-reproject");
    }

    #[test]
    fn test_geospatial_instantiator_default() {
        let instantiator = GeospatialInstantiator::new();
        assert_eq!(instantiator.default_crs.definition, "EPSG:4326");
    }

    #[test]
    fn test_geospatial_instantiator_with_crs() {
        let instantiator = GeospatialInstantiator::new().with_crs(CrsSpec::from_epsg(32632));
        assert_eq!(instantiator.default_crs.definition, "EPSG:32632");
    }

    #[test]
    fn test_create_all_templates() {
        let templates = GeospatialTemplateFactory::create_all_templates();
        assert_eq!(templates.len(), 5);

        for template_result in templates {
            assert!(template_result.is_ok());
        }
    }

    #[test]
    fn test_reproject_config_default() {
        let config = ReprojectConfig::default();
        assert_eq!(config.target_crs.definition, "EPSG:4326");
        assert_eq!(config.resampling, ResamplingMethod::Bilinear);
    }
}