oxigdal-cli 0.1.4

Command-line interface for OxiGDAL geospatial operations
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
//! DEM (Digital Elevation Model) analysis command
//!
//! Provides terrain analysis operations:
//! - Hillshade generation
//! - Slope calculation
//! - Aspect calculation
//! - Terrain Ruggedness Index (TRI)
//! - Topographic Position Index (TPI)
//! - Roughness calculation
//!
//! Examples:
//! ```bash
//! # Generate hillshade
//! oxigdal dem hillshade input.tif hillshade.tif -az 315 -alt 45
//!
//! # Calculate slope
//! oxigdal dem slope input.tif slope.tif --slope-format degree
//!
//! # Calculate aspect
//! oxigdal dem aspect input.tif aspect.tif
//! ```

use crate::OutputFormat;
use crate::util::{progress, raster};
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use console::style;
use oxigdal_algorithms::raster::{
    CombinedHillshadeParams, aspect, combined_hillshade, compute_roughness as roughness,
    compute_tpi as topographic_position_index, compute_tri as terrain_ruggedness_index, hillshade,
    slope,
};
use serde::Serialize;
use std::path::{Path, PathBuf};

/// DEM analysis operations
#[derive(Args, Debug)]
pub struct DemArgs {
    #[command(subcommand)]
    operation: DemOperation,
}

#[derive(Subcommand, Debug)]
enum DemOperation {
    /// Generate hillshade from DEM
    Hillshade(HillshadeArgs),

    /// Calculate slope
    Slope(SlopeArgs),

    /// Calculate aspect
    Aspect(AspectArgs),

    /// Calculate Terrain Ruggedness Index
    Tri(TriArgs),

    /// Calculate Topographic Position Index
    Tpi(TpiArgs),

    /// Calculate roughness
    Roughness(RoughnessArgs),
}

#[derive(Args, Debug)]
struct HillshadeArgs {
    /// Input DEM file
    #[arg(value_name = "INPUT")]
    input: PathBuf,

    /// Output hillshade file
    #[arg(value_name = "OUTPUT")]
    output: PathBuf,

    /// Azimuth of light source (0-360 degrees)
    #[arg(short = 'A', long, default_value = "315.0")]
    azimuth: f64,

    /// Altitude of light source (0-90 degrees)
    #[arg(short, long, default_value = "45.0")]
    altitude: f64,

    /// Z factor (vertical exaggeration)
    #[arg(short, long, default_value = "1.0")]
    z_factor: f64,

    /// Scale (ratio of vertical to horizontal units)
    #[arg(short, long, default_value = "1.0")]
    scale: f64,

    /// Combined shading (Multidirectional oblique weighted)
    #[arg(long)]
    combined: bool,

    /// Overwrite existing output file
    #[arg(long)]
    overwrite: bool,
}

#[derive(Args, Debug)]
struct SlopeArgs {
    /// Input DEM file
    #[arg(value_name = "INPUT")]
    input: PathBuf,

    /// Output slope file
    #[arg(value_name = "OUTPUT")]
    output: PathBuf,

    /// Slope format: degree or percent
    #[arg(long, default_value = "degree")]
    slope_format: SlopeFormat,

    /// Scale (ratio of vertical to horizontal units)
    #[arg(short, long, default_value = "1.0")]
    scale: f64,

    /// Overwrite existing output file
    #[arg(long)]
    overwrite: bool,
}

#[derive(Debug, Clone, Copy)]
enum SlopeFormat {
    Degree,
    Percent,
}

impl std::str::FromStr for SlopeFormat {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "degree" => Ok(SlopeFormat::Degree),
            "percent" => Ok(SlopeFormat::Percent),
            _ => Err(format!(
                "Invalid slope format: {}. Use 'degree' or 'percent'",
                s
            )),
        }
    }
}

#[derive(Args, Debug)]
struct AspectArgs {
    /// Input DEM file
    #[arg(value_name = "INPUT")]
    input: PathBuf,

    /// Output aspect file
    #[arg(value_name = "OUTPUT")]
    output: PathBuf,

    /// Return zero for flat areas instead of -9999
    #[arg(long)]
    zero_for_flat: bool,

    /// Overwrite existing output file
    #[arg(long)]
    overwrite: bool,
}

#[derive(Args, Debug)]
struct TriArgs {
    /// Input DEM file
    #[arg(value_name = "INPUT")]
    input: PathBuf,

    /// Output TRI file
    #[arg(value_name = "OUTPUT")]
    output: PathBuf,

    /// Overwrite existing output file
    #[arg(long)]
    overwrite: bool,
}

#[derive(Args, Debug)]
struct TpiArgs {
    /// Input DEM file
    #[arg(value_name = "INPUT")]
    input: PathBuf,

    /// Output TPI file
    #[arg(value_name = "OUTPUT")]
    output: PathBuf,

    /// Overwrite existing output file
    #[arg(long)]
    overwrite: bool,
}

#[derive(Args, Debug)]
struct RoughnessArgs {
    /// Input DEM file
    #[arg(value_name = "INPUT")]
    input: PathBuf,

    /// Output roughness file
    #[arg(value_name = "OUTPUT")]
    output: PathBuf,

    /// Overwrite existing output file
    #[arg(long)]
    overwrite: bool,
}

#[derive(Serialize)]
struct DemResult {
    operation: String,
    input_file: String,
    output_file: String,
    width: u64,
    height: u64,
    processing_time_ms: u128,
}

pub fn execute(args: DemArgs, format: OutputFormat) -> Result<()> {
    match args.operation {
        DemOperation::Hillshade(hs_args) => execute_hillshade(hs_args, format),
        DemOperation::Slope(slope_args) => execute_slope(slope_args, format),
        DemOperation::Aspect(aspect_args) => execute_aspect(aspect_args, format),
        DemOperation::Tri(tri_args) => execute_tri(tri_args, format),
        DemOperation::Tpi(tpi_args) => execute_tpi(tpi_args, format),
        DemOperation::Roughness(rough_args) => execute_roughness(rough_args, format),
    }
}

fn execute_hillshade(args: HillshadeArgs, format: OutputFormat) -> Result<()> {
    let start = std::time::Instant::now();

    // Validate inputs
    if !args.input.exists() {
        anyhow::bail!("Input file not found: {}", args.input.display());
    }

    if args.output.exists() && !args.overwrite {
        anyhow::bail!(
            "Output file already exists: {}. Use --overwrite to replace.",
            args.output.display()
        );
    }

    if !(0.0..=360.0).contains(&args.azimuth) {
        anyhow::bail!("Azimuth must be between 0 and 360 degrees");
    }

    if !(0.0..=90.0).contains(&args.altitude) {
        anyhow::bail!("Altitude must be between 0 and 90 degrees");
    }

    // Read DEM
    let pb = progress::create_spinner("Reading DEM");
    let raster_info =
        raster::read_raster_info(&args.input).context("Failed to read DEM metadata")?;

    let dem_data =
        raster::read_band_region(&args.input, 0, 0, 0, raster_info.width, raster_info.height)
            .context("Failed to read DEM data")?;
    pb.finish_and_clear();

    // Calculate hillshade
    let pb = progress::create_spinner(if args.combined {
        "Calculating combined hillshade"
    } else {
        "Calculating hillshade"
    });

    let hillshade_band = if args.combined {
        // Use combined/multidirectional hillshade with GDAL-style weights
        let combined_params = CombinedHillshadeParams::gdal_multidirectional()
            .with_altitude(args.altitude)
            .with_z_factor(args.z_factor)
            .with_pixel_size(1.0)
            .with_scale(args.scale);

        combined_hillshade(&dem_data, combined_params)
            .context("Failed to calculate combined hillshade")?
    } else {
        // Standard single-direction hillshade
        use oxigdal_algorithms::raster::HillshadeParams;
        let params = HillshadeParams {
            azimuth: args.azimuth,
            altitude: args.altitude,
            z_factor: args.z_factor,
            pixel_size: 1.0,
            scale: args.scale,
        };
        hillshade(&dem_data, params).context("Failed to calculate hillshade")?
    };
    pb.finish_and_clear();

    // Write output
    let pb = progress::create_spinner("Writing output");
    raster::write_single_band(
        &args.output,
        &hillshade_band,
        raster_info.geo_transform,
        raster_info.epsg_code,
        None,
    )
    .context("Failed to write hillshade output")?;
    pb.finish_with_message("Hillshade generation complete");

    output_result(
        "Hillshade",
        &args.input,
        &args.output,
        raster_info.width,
        raster_info.height,
        start.elapsed().as_millis(),
        format,
    )
}

fn execute_slope(args: SlopeArgs, format: OutputFormat) -> Result<()> {
    let start = std::time::Instant::now();

    // Validate inputs
    if !args.input.exists() {
        anyhow::bail!("Input file not found: {}", args.input.display());
    }

    if args.output.exists() && !args.overwrite {
        anyhow::bail!(
            "Output file already exists: {}. Use --overwrite to replace.",
            args.output.display()
        );
    }

    // Read DEM
    let pb = progress::create_spinner("Reading DEM");
    let raster_info =
        raster::read_raster_info(&args.input).context("Failed to read DEM metadata")?;

    let dem_data =
        raster::read_band_region(&args.input, 0, 0, 0, raster_info.width, raster_info.height)
            .context("Failed to read DEM data")?;
    pb.finish_and_clear();

    // Calculate slope
    let pb = progress::create_spinner("Calculating slope");
    // slope(dem, pixel_size, z_factor) — z_factor=1.0, scale applies as pixel_size
    let slope_band = slope(&dem_data, args.scale, 1.0).context("Failed to calculate slope")?;

    // Convert to percent if requested (slope is returned in degrees, convert per-pixel)
    let slope_band = if matches!(args.slope_format, SlopeFormat::Percent) {
        use oxigdal_algorithms::raster::{SlopeUnits, convert_slope_degrees};
        let mut pct_band = oxigdal_core::buffer::RasterBuffer::zeros(
            slope_band.width(),
            slope_band.height(),
            slope_band.data_type(),
        );
        for y in 0..slope_band.height() {
            for x in 0..slope_band.width() {
                let deg = slope_band
                    .get_pixel(x, y)
                    .context("Failed to read slope pixel")?;
                let pct = convert_slope_degrees(deg, SlopeUnits::Percent);
                pct_band
                    .set_pixel(x, y, pct)
                    .context("Failed to set slope percent pixel")?;
            }
        }
        pct_band
    } else {
        slope_band
    };
    pb.finish_and_clear();

    // Write output
    let pb = progress::create_spinner("Writing output");
    raster::write_single_band(
        &args.output,
        &slope_band,
        raster_info.geo_transform,
        raster_info.epsg_code,
        None,
    )
    .context("Failed to write slope output")?;
    pb.finish_with_message("Slope calculation complete");

    output_result(
        "Slope",
        &args.input,
        &args.output,
        raster_info.width,
        raster_info.height,
        start.elapsed().as_millis(),
        format,
    )
}

fn execute_aspect(args: AspectArgs, format: OutputFormat) -> Result<()> {
    let start = std::time::Instant::now();

    // Validate inputs
    if !args.input.exists() {
        anyhow::bail!("Input file not found: {}", args.input.display());
    }

    if args.output.exists() && !args.overwrite {
        anyhow::bail!(
            "Output file already exists: {}. Use --overwrite to replace.",
            args.output.display()
        );
    }

    // Read DEM
    let pb = progress::create_spinner("Reading DEM");
    let raster_info =
        raster::read_raster_info(&args.input).context("Failed to read DEM metadata")?;

    let dem_data =
        raster::read_band_region(&args.input, 0, 0, 0, raster_info.width, raster_info.height)
            .context("Failed to read DEM data")?;
    pb.finish_and_clear();

    // Calculate aspect — aspect(dem, pixel_size, z_factor)
    let pb = progress::create_spinner("Calculating aspect");
    let mut aspect_band = aspect(&dem_data, 1.0, 1.0).context("Failed to calculate aspect")?;

    // Optionally map flat areas (-9999) to 0
    if args.zero_for_flat {
        let flat_sentinel = -9999.0_f64;
        for y in 0..aspect_band.height() {
            for x in 0..aspect_band.width() {
                if let Ok(v) = aspect_band.get_pixel(x, y) {
                    if (v - flat_sentinel).abs() < 1.0 {
                        aspect_band
                            .set_pixel(x, y, 0.0)
                            .context("Failed to set pixel")?;
                    }
                }
            }
        }
    }
    pb.finish_and_clear();

    // Write output
    let pb = progress::create_spinner("Writing output");
    raster::write_single_band(
        &args.output,
        &aspect_band,
        raster_info.geo_transform,
        raster_info.epsg_code,
        None,
    )
    .context("Failed to write aspect output")?;
    pb.finish_with_message("Aspect calculation complete");

    output_result(
        "Aspect",
        &args.input,
        &args.output,
        raster_info.width,
        raster_info.height,
        start.elapsed().as_millis(),
        format,
    )
}

fn execute_tri(args: TriArgs, format: OutputFormat) -> Result<()> {
    let start = std::time::Instant::now();

    // Validate inputs
    if !args.input.exists() {
        anyhow::bail!("Input file not found: {}", args.input.display());
    }

    if args.output.exists() && !args.overwrite {
        anyhow::bail!(
            "Output file already exists: {}. Use --overwrite to replace.",
            args.output.display()
        );
    }

    // Read DEM
    let pb = progress::create_spinner("Reading DEM");
    let raster_info =
        raster::read_raster_info(&args.input).context("Failed to read DEM metadata")?;

    let dem_data =
        raster::read_band_region(&args.input, 0, 0, 0, raster_info.width, raster_info.height)
            .context("Failed to read DEM data")?;
    pb.finish_and_clear();

    // Calculate TRI — terrain_ruggedness_index(dem, cell_size)
    let pb = progress::create_spinner("Calculating TRI");
    let tri_band = terrain_ruggedness_index(&dem_data, 1.0).context("Failed to calculate TRI")?;
    pb.finish_and_clear();

    // Write output
    let pb = progress::create_spinner("Writing output");
    raster::write_single_band(
        &args.output,
        &tri_band,
        raster_info.geo_transform,
        raster_info.epsg_code,
        None,
    )
    .context("Failed to write TRI output")?;
    pb.finish_with_message("TRI calculation complete");

    output_result(
        "TRI",
        &args.input,
        &args.output,
        raster_info.width,
        raster_info.height,
        start.elapsed().as_millis(),
        format,
    )
}

fn execute_tpi(args: TpiArgs, format: OutputFormat) -> Result<()> {
    let start = std::time::Instant::now();

    // Validate inputs
    if !args.input.exists() {
        anyhow::bail!("Input file not found: {}", args.input.display());
    }

    if args.output.exists() && !args.overwrite {
        anyhow::bail!(
            "Output file already exists: {}. Use --overwrite to replace.",
            args.output.display()
        );
    }

    // Read DEM
    let pb = progress::create_spinner("Reading DEM");
    let raster_info =
        raster::read_raster_info(&args.input).context("Failed to read DEM metadata")?;

    let dem_data =
        raster::read_band_region(&args.input, 0, 0, 0, raster_info.width, raster_info.height)
            .context("Failed to read DEM data")?;
    pb.finish_and_clear();

    // Calculate TPI — topographic_position_index(dem, neighborhood_size, cell_size)
    let pb = progress::create_spinner("Calculating TPI");
    let tpi_band =
        topographic_position_index(&dem_data, 3, 1.0).context("Failed to calculate TPI")?;
    pb.finish_and_clear();

    // Write output
    let pb = progress::create_spinner("Writing output");
    raster::write_single_band(
        &args.output,
        &tpi_band,
        raster_info.geo_transform,
        raster_info.epsg_code,
        None,
    )
    .context("Failed to write TPI output")?;
    pb.finish_with_message("TPI calculation complete");

    output_result(
        "TPI",
        &args.input,
        &args.output,
        raster_info.width,
        raster_info.height,
        start.elapsed().as_millis(),
        format,
    )
}

fn execute_roughness(args: RoughnessArgs, format: OutputFormat) -> Result<()> {
    let start = std::time::Instant::now();

    // Validate inputs
    if !args.input.exists() {
        anyhow::bail!("Input file not found: {}", args.input.display());
    }

    if args.output.exists() && !args.overwrite {
        anyhow::bail!(
            "Output file already exists: {}. Use --overwrite to replace.",
            args.output.display()
        );
    }

    // Read DEM
    let pb = progress::create_spinner("Reading DEM");
    let raster_info =
        raster::read_raster_info(&args.input).context("Failed to read DEM metadata")?;

    let dem_data =
        raster::read_band_region(&args.input, 0, 0, 0, raster_info.width, raster_info.height)
            .context("Failed to read DEM data")?;
    pb.finish_and_clear();

    // Calculate roughness — roughness(dem, neighborhood_size)
    let pb = progress::create_spinner("Calculating roughness");
    let roughness_band = roughness(&dem_data, 3).context("Failed to calculate roughness")?;
    pb.finish_and_clear();

    // Write output
    let pb = progress::create_spinner("Writing output");
    raster::write_single_band(
        &args.output,
        &roughness_band,
        raster_info.geo_transform,
        raster_info.epsg_code,
        None,
    )
    .context("Failed to write roughness output")?;
    pb.finish_with_message("Roughness calculation complete");

    output_result(
        "Roughness",
        &args.input,
        &args.output,
        raster_info.width,
        raster_info.height,
        start.elapsed().as_millis(),
        format,
    )
}

fn output_result(
    operation: &str,
    input: &Path,
    output: &Path,
    width: u64,
    height: u64,
    processing_time_ms: u128,
    format: OutputFormat,
) -> Result<()> {
    let result = DemResult {
        operation: operation.to_string(),
        input_file: input.display().to_string(),
        output_file: output.display().to_string(),
        width,
        height,
        processing_time_ms,
    };

    match format {
        OutputFormat::Json => {
            let json =
                serde_json::to_string_pretty(&result).context("Failed to serialize to JSON")?;
            println!("{}", json);
        }
        OutputFormat::Text => {
            println!(
                "{}",
                style(format!("{} complete", operation)).green().bold()
            );
            println!("  Input:      {}", result.input_file);
            println!("  Output:     {}", result.output_file);
            println!("  Dimensions: {} x {}", result.width, result.height);
            println!("  Time:       {} ms", result.processing_time_ms);
        }
    }

    Ok(())
}

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

    #[test]
    fn test_slope_format_parsing() {
        use std::str::FromStr;

        assert!(matches!(
            SlopeFormat::from_str("degree"),
            Ok(SlopeFormat::Degree)
        ));
        assert!(matches!(
            SlopeFormat::from_str("percent"),
            Ok(SlopeFormat::Percent)
        ));
        assert!(SlopeFormat::from_str("invalid").is_err());
    }
}