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
//! Info command - Display file metadata and information

use crate::OutputFormat;
use crate::util;
use anyhow::{Context, Result};
use clap::Args;
use console::style;
use oxigdal_core::{io::FileDataSource, types::RasterDataType};
use oxigdal_geojson::GeoJsonReader;
use oxigdal_geotiff::GeoTiffReader;
use oxigdal_shapefile::ShapefileReader;
use serde::Serialize;
use std::fs::{self, File};
use std::io::BufReader;
use std::path::PathBuf;

/// Display information about a raster or vector file
#[derive(Args, Debug)]
pub struct InfoArgs {
    /// Input file path
    #[arg(value_name = "FILE")]
    input: PathBuf,

    /// Show detailed statistics
    #[arg(short, long)]
    stats: bool,

    /// Compute min/max values
    #[arg(long)]
    compute_minmax: bool,

    /// Show all metadata
    #[arg(short, long)]
    metadata: bool,

    /// Show coordinate reference system details
    #[arg(long)]
    crs: bool,

    /// Show band/layer information
    #[arg(short, long)]
    bands: bool,
}

#[derive(Serialize)]
struct FileInfo {
    file_path: String,
    file_size: String,
    format: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    raster_info: Option<RasterInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    vector_info: Option<VectorInfo>,
}

#[derive(Serialize)]
struct RasterInfo {
    width: u64,
    height: u64,
    bands: u32,
    data_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    geotransform: Option<Vec<f64>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    projection: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    bounds: Option<Bounds>,
}

#[derive(Serialize)]
struct VectorInfo {
    layer_count: usize,
    feature_count: usize,
    geometry_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    bounds: Option<Bounds>,
    #[serde(skip_serializing_if = "Option::is_none")]
    crs: Option<String>,
}

#[derive(Serialize)]
struct Bounds {
    min_x: f64,
    min_y: f64,
    max_x: f64,
    max_y: f64,
}

pub fn execute(args: InfoArgs, format: OutputFormat) -> Result<()> {
    // If the caller supplied a cloud URI as a PathBuf it won't have a filesystem
    // presence — detect and reject early with a helpful message.
    let input_str = args.input.to_str().unwrap_or_default();
    if crate::util::cloud::is_cloud_uri(input_str) {
        eprintln!("Note: cloud URI support is experimental; full metadata may not be available");
        anyhow::bail!(
            "cloud URI reading for raster info requires GeoTiffReader<DataSource>; \
             use a local file path for now (got: {})",
            input_str
        );
    }

    // Allow file:// URIs to be passed; strip the prefix for filesystem operations.
    let resolved_path = if let Some(stripped) = input_str.strip_prefix("file://") {
        std::path::PathBuf::from(stripped)
    } else {
        args.input.clone()
    };

    // Check if file exists
    if !resolved_path.exists() {
        anyhow::bail!("File not found: {}", resolved_path.display());
    }

    // Get file size
    let metadata = fs::metadata(&resolved_path)
        .with_context(|| format!("Failed to read file metadata: {}", resolved_path.display()))?;
    let file_size = util::format_size(metadata.len());

    // Detect format
    let detected_format = util::detect_format(&resolved_path)
        .ok_or_else(|| anyhow::anyhow!("Unknown file format"))?;

    // Build a resolved InfoArgs so sub-functions get the stripped path
    let resolved_args = InfoArgs {
        input: resolved_path.clone(),
        stats: args.stats,
        compute_minmax: args.compute_minmax,
        metadata: args.metadata,
        crs: args.crs,
        bands: args.bands,
    };

    // Try to read as raster or vector
    let file_info = match detected_format {
        "GeoTIFF" => {
            let raster_info = read_geotiff_info(&resolved_args)?;
            FileInfo {
                file_path: resolved_path.display().to_string(),
                file_size,
                format: detected_format.to_string(),
                raster_info: Some(raster_info),
                vector_info: None,
            }
        }
        "GeoJSON" => {
            let vector_info = read_geojson_info(&resolved_args)?;
            FileInfo {
                file_path: resolved_path.display().to_string(),
                file_size,
                format: detected_format.to_string(),
                raster_info: None,
                vector_info: Some(vector_info),
            }
        }
        "Shapefile" => {
            let vector_info = read_shapefile_info(&resolved_args)?;
            FileInfo {
                file_path: resolved_path.display().to_string(),
                file_size,
                format: detected_format.to_string(),
                raster_info: None,
                vector_info: Some(vector_info),
            }
        }
        _ => {
            anyhow::bail!(
                "Format detected but info display not yet implemented for: {}",
                detected_format
            );
        }
    };

    // Output results
    match format {
        OutputFormat::Json => {
            let json =
                serde_json::to_string_pretty(&file_info).context("Failed to serialize to JSON")?;
            println!("{}", json);
        }
        OutputFormat::Text => {
            print_text_info(&file_info, &args);
        }
    }

    Ok(())
}

fn read_geotiff_info(args: &InfoArgs) -> Result<RasterInfo> {
    let source = FileDataSource::open(&args.input)
        .with_context(|| format!("Failed to open file: {}", args.input.display()))?;

    let reader = GeoTiffReader::open(source)
        .with_context(|| format!("Failed to read GeoTIFF: {}", args.input.display()))?;

    let width = reader.width();
    let height = reader.height();
    let bands = reader.band_count();
    let data_type = reader
        .data_type()
        .ok_or_else(|| anyhow::anyhow!("Could not determine data type"))?;

    let geotransform = reader.geo_transform().map(|gt| {
        vec![
            gt.origin_x,
            gt.pixel_width,
            gt.row_rotation,
            gt.origin_y,
            gt.col_rotation,
            gt.pixel_height,
        ]
    });

    let projection = reader.epsg_code().map(|code| format!("EPSG:{}", code));

    // Calculate bounds from geotransform
    let bounds = geotransform.as_ref().map(|gt| {
        let min_x = gt[0];
        let max_y = gt[3];
        let max_x = min_x + gt[1] * width as f64;
        let min_y = max_y + gt[5] * height as f64;

        Bounds {
            min_x,
            min_y,
            max_x,
            max_y,
        }
    });

    Ok(RasterInfo {
        width,
        height,
        bands,
        data_type: format_data_type(data_type),
        geotransform,
        projection,
        bounds,
    })
}

fn read_geojson_info(args: &InfoArgs) -> Result<VectorInfo> {
    let file = File::open(&args.input)
        .with_context(|| format!("Failed to open file: {}", args.input.display()))?;
    let buf_reader = BufReader::new(file);
    let mut reader = GeoJsonReader::new(buf_reader);

    let feature_collection = reader
        .read_feature_collection()
        .context("Failed to read GeoJSON")?;

    let feature_count = feature_collection.features.len();

    // Determine geometry type from first feature
    let geometry_type = if let Some(first_feature) = feature_collection.features.first() {
        if let Some(ref geom) = first_feature.geometry {
            format!("{:?}", geom)
        } else {
            "Unknown".to_string()
        }
    } else {
        "Unknown".to_string()
    };

    // Get bounds from feature collection
    let bounds = feature_collection.bbox.as_ref().and_then(|bbox| {
        if bbox.len() >= 4 {
            Some(Bounds {
                min_x: bbox[0],
                min_y: bbox[1],
                max_x: bbox[2],
                max_y: bbox[3],
            })
        } else {
            None
        }
    });

    let crs = feature_collection
        .crs
        .as_ref()
        .map(|crs| format!("{:?}", crs));

    Ok(VectorInfo {
        layer_count: 1,
        feature_count,
        geometry_type,
        bounds,
        crs,
    })
}

fn read_shapefile_info(args: &InfoArgs) -> Result<VectorInfo> {
    let reader = ShapefileReader::open(&args.input)
        .with_context(|| format!("Failed to open Shapefile: {}", args.input.display()))?;

    let header = reader.header();

    // Get geometry type from shapefile header
    let geometry_type = format!("{:?}", header.shape_type);

    // Get bounding box from header
    let bbox = &header.bbox;
    let bounds = Some(Bounds {
        min_x: bbox.x_min,
        min_y: bbox.y_min,
        max_x: bbox.x_max,
        max_y: bbox.y_max,
    });

    // Get feature count from index entries or by reading features
    let feature_count = if let Some(entries) = reader.index_entries() {
        entries.len()
    } else {
        // Fall back to reading features to count them
        reader
            .read_features()
            .map(|f| f.len())
            .with_context(|| "Failed to read Shapefile features for counting")?
    };

    // Get field information
    let fields = reader.field_descriptors();
    let field_names: Vec<String> = fields.iter().map(|f| f.name.clone()).collect();

    // Check for .prj file for CRS info
    let prj_path = args.input.with_extension("prj");
    let crs = if prj_path.exists() {
        fs::read_to_string(&prj_path)
            .ok()
            .map(|s| s.trim().to_string())
    } else {
        None
    };

    // Log field info if metadata requested (displayed in text output)
    if args.metadata && !field_names.is_empty() {
        println!("\n{}", console::style("Attribute Fields").bold().cyan());
        for field in fields {
            println!(
                "  {} ({:?}, length: {}, decimals: {})",
                field.name, field.field_type, field.length, field.decimal_count
            );
        }
    }

    Ok(VectorInfo {
        layer_count: 1,
        feature_count,
        geometry_type,
        bounds,
        crs,
    })
}

fn format_data_type(dt: RasterDataType) -> String {
    match dt {
        RasterDataType::UInt8 => "UInt8".to_string(),
        RasterDataType::UInt16 => "UInt16".to_string(),
        RasterDataType::UInt32 => "UInt32".to_string(),
        RasterDataType::UInt64 => "UInt64".to_string(),
        RasterDataType::Int8 => "Int8".to_string(),
        RasterDataType::Int16 => "Int16".to_string(),
        RasterDataType::Int32 => "Int32".to_string(),
        RasterDataType::Int64 => "Int64".to_string(),
        RasterDataType::Float32 => "Float32".to_string(),
        RasterDataType::Float64 => "Float64".to_string(),
        RasterDataType::CFloat32 => "CFloat32".to_string(),
        RasterDataType::CFloat64 => "CFloat64".to_string(),
    }
}

fn print_text_info(info: &FileInfo, args: &InfoArgs) {
    println!("{}", style("File Information").bold().cyan());
    println!("  Path:   {}", info.file_path);
    println!("  Size:   {}", info.file_size);
    println!("  Format: {}", info.format);
    println!();

    if let Some(ref raster) = info.raster_info {
        println!("{}", style("Raster Information").bold().cyan());
        println!("  Dimensions: {} x {}", raster.width, raster.height);
        println!("  Bands:      {}", raster.bands);
        println!("  Data Type:  {}", raster.data_type);

        if args.crs || args.metadata {
            if let Some(ref proj) = raster.projection {
                println!("\n{}", style("Coordinate Reference System").bold().cyan());
                println!("  {}", proj);
            }
        }

        if let Some(ref gt) = raster.geotransform {
            println!("\n{}", style("Geotransform").bold().cyan());
            println!("  Origin:    ({}, {})", gt[0], gt[3]);
            println!("  Pixel Size: ({}, {})", gt[1], gt[5]);
        }

        if let Some(ref bounds) = raster.bounds {
            println!("\n{}", style("Bounds").bold().cyan());
            println!("  Min X: {}", bounds.min_x);
            println!("  Min Y: {}", bounds.min_y);
            println!("  Max X: {}", bounds.max_x);
            println!("  Max Y: {}", bounds.max_y);
        }
    }

    if let Some(ref vector) = info.vector_info {
        println!("{}", style("Vector Information").bold().cyan());
        println!("  Layers:   {}", vector.layer_count);
        println!("  Features: {}", vector.feature_count);
        println!("  Geometry: {}", vector.geometry_type);

        if args.crs || args.metadata {
            if let Some(ref crs) = vector.crs {
                println!("\n{}", style("Coordinate Reference System").bold().cyan());
                println!("  {}", crs);
            }
        }

        if let Some(ref bounds) = vector.bounds {
            println!("\n{}", style("Bounds").bold().cyan());
            println!("  Min X: {}", bounds.min_x);
            println!("  Min Y: {}", bounds.min_y);
            println!("  Max X: {}", bounds.max_x);
            println!("  Max Y: {}", bounds.max_y);
        }
    }
}

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

    #[test]
    fn test_format_data_type() {
        assert_eq!(format_data_type(RasterDataType::UInt8), "UInt8");
        assert_eq!(format_data_type(RasterDataType::Float32), "Float32");
        assert_eq!(format_data_type(RasterDataType::CFloat32), "CFloat32");
    }
}