mgrs 0.4.2

Bidirectional MGRS/lat-long coordinate conversion CLI with multi-format I/O: CSV, GeoJSON, KML, KMZ, GPX, WKT, TopoJSON, Shapefile, GeoPackage, FlatGeobuf
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
use std::io::{Cursor, Read, Write, BufReader};
use std::path::Path;
use anyhow::{Context, Result};
use crate::convert;
use crate::detect;
use crate::formats::{ConvertedRow, OutputFormat, InputFormat, PathOutputFormat};
use crate::formats::csv_format::CsvOutput;
use crate::formats::geojson::GeoJsonOutput;
use crate::formats::kml::KmlOutput;
use crate::formats::gpx::GpxOutput;
use crate::formats::wkt::WktOutput;
use crate::formats::topojson::TopoJsonOutput;
use crate::formats::kmz::KmzOutput;
#[cfg(feature = "flatgeobuf-format")]
use crate::formats::flatgeobuf_format::FlatGeobufOutput;

/// Format selection (input and output).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatKind {
    Csv,
    GeoJson,
    Kml,
    Gpx,
    Wkt,
    TopoJson,
    Kmz,
    Shapefile,
    GeoPackage,
    FlatGeobuf,
}

/// How to identify the source column.
#[derive(Debug, Clone)]
pub enum ColumnSpec {
    Name(String),
    Index(usize),
}

/// Configuration for stream processing.
pub struct ProcessConfig {
    pub column: Option<ColumnSpec>,
    pub strict: bool,
    pub name_column: Option<String>,
}

/// Statistics from processing.
pub struct ProcessStats {
    pub total_rows: usize,
    pub succeeded_rows: usize,
    pub failed_rows: usize,
}

/// Process a CSV input, converting MGRS to lat/lon, writing to the specified format.
pub fn process_to_latlon<R: Read, W: Write>(
    input: R,
    output: W,
    format: FormatKind,
    config: &ProcessConfig,
) -> Result<ProcessStats> {
    let mut reader = csv::Reader::from_reader(BufReader::new(input));
    let headers: Vec<String> = reader.headers()?.iter().map(|h| h.to_string()).collect();

    // Determine the MGRS column
    let mgrs_col = match &config.column {
        Some(ColumnSpec::Name(name)) => {
            headers.iter().position(|h| h == name)
                .with_context(|| format!("Column '{}' not found in headers", name))?
        }
        Some(ColumnSpec::Index(idx)) => *idx,
        None => {
            // Need to read some records for auto-detection
            let mut sample_records = Vec::new();
            for result in reader.records() {
                let record = result?;
                sample_records.push(record);
                if sample_records.len() >= 100 {
                    break;
                }
            }
            let col = detect::detect_mgrs_column(&sample_records)
                .with_context(|| "No MGRS-like column detected in the CSV file")?;

            // Process the buffered sample records, then continue with remaining
            let mut writer = create_writer(output, format, &config.name_column)?;
            writer.write_header(&headers)?;

            let mut stats = ProcessStats {
                total_rows: 0,
                succeeded_rows: 0,
                failed_rows: 0,
            };

            for record in &sample_records {
                process_record(record, &headers, col, &mut *writer, &mut stats, config.strict)?;
            }

            for result in reader.records() {
                let record = result?;
                process_record(&record, &headers, col, &mut *writer, &mut stats, config.strict)?;
            }

            writer.finish()?;
            return Ok(stats);
        }
    };

    // Explicit column path -- stream all records
    let mut writer = create_writer(output, format, &config.name_column)?;
    writer.write_header(&headers)?;

    let mut stats = ProcessStats {
        total_rows: 0,
        succeeded_rows: 0,
        failed_rows: 0,
    };

    for result in reader.records() {
        let record = result?;
        process_record(&record, &headers, mgrs_col, &mut *writer, &mut stats, config.strict)?;
    }

    writer.finish()?;
    Ok(stats)
}

fn create_writer<'a, W: Write + 'a>(
    output: W,
    format: FormatKind,
    name_column: &Option<String>,
) -> Result<Box<dyn OutputFormat + 'a>> {
    Ok(match format {
        FormatKind::Csv => Box::new(CsvOutput::new(output)),
        FormatKind::GeoJson => Box::new(GeoJsonOutput::new(output)),
        FormatKind::Kml => Box::new(KmlOutput::new(output, name_column.clone())),
        FormatKind::Gpx => Box::new(GpxOutput::new(output, name_column.clone())),
        FormatKind::Wkt => Box::new(WktOutput::new(output)),
        FormatKind::TopoJson => Box::new(TopoJsonOutput::new(output)),
        FormatKind::Kmz => Box::new(KmzOutput::new(output, name_column.clone())),
        #[cfg(feature = "flatgeobuf-format")]
        FormatKind::FlatGeobuf => Box::new(FlatGeobufOutput::new(output)),
        #[cfg(not(feature = "flatgeobuf-format"))]
        FormatKind::FlatGeobuf => anyhow::bail!("FlatGeobuf support not compiled (enable 'flatgeobuf-format' feature)"),
        FormatKind::Shapefile | FormatKind::GeoPackage => {
            anyhow::bail!("Format {:?} requires --output flag (path-based format)", format)
        }
    })
}

/// Create a reader for the given input format.
pub fn create_reader(data: Vec<u8>, format: FormatKind) -> Result<Box<dyn InputFormat>> {
    use crate::formats::csv_input::CsvInput;
    use crate::formats::geojson_input::GeoJsonInput;
    use crate::formats::kml_input::KmlInput;
    use crate::formats::gpx_input::GpxInput;
    use crate::formats::wkt::WktInput;
    use crate::formats::topojson::TopoJsonInput;
    use crate::formats::kmz::KmzInput;
    #[cfg(feature = "flatgeobuf-format")]
    use crate::formats::flatgeobuf_format::FlatGeobufInput;

    Ok(match format {
        FormatKind::Csv => Box::new(CsvInput::new(Cursor::new(data))?),
        FormatKind::GeoJson => Box::new(GeoJsonInput::new(Cursor::new(data))?),
        FormatKind::Kml => Box::new(KmlInput::new(Cursor::new(data))?),
        FormatKind::Gpx => Box::new(GpxInput::new(Cursor::new(data))?),
        FormatKind::Wkt => Box::new(WktInput::new(Cursor::new(data))?),
        FormatKind::TopoJson => Box::new(TopoJsonInput::new(Cursor::new(data))?),
        FormatKind::Kmz => Box::new(KmzInput::new(Cursor::new(data))?),
        #[cfg(feature = "flatgeobuf-format")]
        FormatKind::FlatGeobuf => Box::new(FlatGeobufInput::new(Cursor::new(data))?),
        #[cfg(not(feature = "flatgeobuf-format"))]
        FormatKind::FlatGeobuf => anyhow::bail!("FlatGeobuf support not compiled (enable 'flatgeobuf-format' feature)"),
        FormatKind::Shapefile => {
            anyhow::bail!("Shapefile input requires a file path, use --input-format with a .shp file")
        }
        FormatKind::GeoPackage => {
            anyhow::bail!("GeoPackage input requires a file path, use --input-format with a .gpkg file")
        }
    })
}

/// Create a path-based reader for Shapefile or GeoPackage.
pub fn create_path_reader(path: &Path, format: FormatKind) -> Result<Box<dyn InputFormat>> {
    match format {
        #[cfg(feature = "shapefile-format")]
        FormatKind::Shapefile => {
            use crate::formats::shapefile_format::ShapefileInput;
            Ok(Box::new(ShapefileInput::new(path)?))
        }
        #[cfg(not(feature = "shapefile-format"))]
        FormatKind::Shapefile => anyhow::bail!("Shapefile support not compiled (enable 'shapefile-format' feature)"),
        #[cfg(feature = "geopackage")]
        FormatKind::GeoPackage => {
            use crate::formats::geopackage::GeoPackageInput;
            Ok(Box::new(GeoPackageInput::new(path)?))
        }
        #[cfg(not(feature = "geopackage"))]
        FormatKind::GeoPackage => anyhow::bail!("GeoPackage support not compiled (enable 'geopackage' feature)"),
        _ => anyhow::bail!("Format {:?} is not a path-based input format", format),
    }
}

/// Create a path-based writer for Shapefile or GeoPackage.
fn create_path_writer(path: &Path, format: FormatKind, headers: &[String]) -> Result<Box<dyn PathOutputFormat>> {
    match format {
        #[cfg(feature = "shapefile-format")]
        FormatKind::Shapefile => {
            use crate::formats::shapefile_format::ShapefileOutput;
            Ok(Box::new(ShapefileOutput::new(path, headers)?))
        }
        #[cfg(not(feature = "shapefile-format"))]
        FormatKind::Shapefile => anyhow::bail!("Shapefile support not compiled (enable 'shapefile-format' feature)"),
        #[cfg(feature = "geopackage")]
        FormatKind::GeoPackage => {
            use crate::formats::geopackage::GeoPackageOutput;
            Ok(Box::new(GeoPackageOutput::new(path, headers)?))
        }
        #[cfg(not(feature = "geopackage"))]
        FormatKind::GeoPackage => anyhow::bail!("GeoPackage support not compiled (enable 'geopackage' feature)"),
        _ => anyhow::bail!("Format {:?} is not a path-based output format", format),
    }
}

/// Process input from any supported format, converting to any output format.
/// Used when input is a non-CSV format.
pub fn process_format_to_format<W: Write>(
    mut reader: Box<dyn InputFormat>,
    output: W,
    out_format: FormatKind,
    config: &ProcessConfig,
) -> Result<ProcessStats> {
    let headers = reader.headers();
    let mut writer = create_writer(output, out_format, &config.name_column)?;
    writer.write_header(&headers)?;

    let mut stats = ProcessStats { total_rows: 0, succeeded_rows: 0, failed_rows: 0 };

    while let Some(record) = reader.next_record()? {
        stats.total_rows += 1;
        let fields: Vec<String> = headers.iter().map(|h| {
            record.fields.iter().find(|(k,_)| k == h).map(|(_,v)| v.clone()).unwrap_or_default()
        }).collect();

        if record.latitude.is_some() && record.longitude.is_some() {
            stats.succeeded_rows += 1;
        } else {
            stats.failed_rows += 1;
        }

        writer.write_row(&ConvertedRow {
            fields,
            headers: headers.clone(),
            latitude: record.latitude,
            longitude: record.longitude,
            mgrs_source: None,
        })?;
    }

    writer.finish()?;
    Ok(stats)
}

/// Process input from any format to a path-based output (Shapefile, GeoPackage).
pub fn process_format_to_path(
    mut reader: Box<dyn InputFormat>,
    output_path: &Path,
    out_format: FormatKind,
) -> Result<ProcessStats> {
    let headers = reader.headers();

    let mut writer: Box<dyn PathOutputFormat> = create_path_writer(output_path, out_format, &headers)?;

    let mut stats = ProcessStats { total_rows: 0, succeeded_rows: 0, failed_rows: 0 };

    while let Some(record) = reader.next_record()? {
        stats.total_rows += 1;
        let fields: Vec<String> = headers.iter().map(|h| {
            record.fields.iter().find(|(k,_)| k == h).map(|(_,v)| v.clone()).unwrap_or_default()
        }).collect();

        if record.latitude.is_some() && record.longitude.is_some() {
            stats.succeeded_rows += 1;
        } else {
            stats.failed_rows += 1;
        }

        writer.write_row(&ConvertedRow {
            fields,
            headers: headers.clone(),
            latitude: record.latitude,
            longitude: record.longitude,
            mgrs_source: None,
        })?;
    }

    writer.finish()?;
    Ok(stats)
}

/// Process CSV to a path-based output format (Shapefile, GeoPackage).
pub fn process_csv_to_path<R: Read>(
    input: R,
    output_path: &Path,
    out_format: FormatKind,
    config: &ProcessConfig,
) -> Result<ProcessStats> {
    let mut csv_reader = csv::Reader::from_reader(BufReader::new(input));
    let headers: Vec<String> = csv_reader.headers()?.iter().map(|h| h.to_string()).collect();

    let mgrs_col = match &config.column {
        Some(ColumnSpec::Name(name)) => {
            headers.iter().position(|h| h == name)
                .with_context(|| format!("Column '{}' not found in headers", name))?
        }
        Some(ColumnSpec::Index(idx)) => *idx,
        None => {
            let mut sample_records = Vec::new();
            for result in csv_reader.records() {
                let record = result?;
                sample_records.push(record);
                if sample_records.len() >= 100 { break; }
            }
            let col = detect::detect_mgrs_column(&sample_records)
                .with_context(|| "No MGRS-like column detected")?;

            let mut writer = create_path_writer(output_path, out_format, &headers)?;

            let mut stats = ProcessStats { total_rows: 0, succeeded_rows: 0, failed_rows: 0 };
            for record in &sample_records {
                process_record_path(record, &headers, col, &mut *writer, &mut stats, config.strict)?;
            }
            for result in csv_reader.records() {
                let record = result?;
                process_record_path(&record, &headers, col, &mut *writer, &mut stats, config.strict)?;
            }
            writer.finish()?;
            return Ok(stats);
        }
    };

    let mut writer = create_path_writer(output_path, out_format, &headers)?;

    let mut stats = ProcessStats { total_rows: 0, succeeded_rows: 0, failed_rows: 0 };
    for result in csv_reader.records() {
        let record = result?;
        process_record_path(&record, &headers, mgrs_col, &mut *writer, &mut stats, config.strict)?;
    }
    writer.finish()?;
    Ok(stats)
}

fn process_record_path(
    record: &csv::StringRecord,
    headers: &[String],
    mgrs_col: usize,
    writer: &mut dyn PathOutputFormat,
    stats: &mut ProcessStats,
    strict: bool,
) -> Result<()> {
    stats.total_rows += 1;
    let mgrs_value = record.get(mgrs_col).unwrap_or("").trim();

    let (lat, lon, mgrs_src) = if !mgrs_value.is_empty() && detect::is_likely_mgrs(mgrs_value) {
        match convert::mgrs_to_latlon(mgrs_value) {
            Ok(coord) => (Some(coord.latitude), Some(coord.longitude), Some(mgrs_value.to_string())),
            Err(e) => {
                stats.failed_rows += 1;
                eprintln!("Warning: row {}: failed to convert '{}': {}", stats.total_rows, mgrs_value, e);
                if strict {
                    return Err(e.context(format!("Strict mode: aborting at row {}", stats.total_rows)));
                }
                (None, None, Some(mgrs_value.to_string()))
            }
        }
    } else {
        stats.failed_rows += 1;
        (None, None, None)
    };

    if lat.is_some() { stats.succeeded_rows += 1; }

    let fields: Vec<String> = record.iter().map(|f| f.to_string()).collect();
    writer.write_row(&ConvertedRow {
        fields,
        headers: headers.to_vec(),
        latitude: lat,
        longitude: lon,
        mgrs_source: mgrs_src,
    })?;

    Ok(())
}

fn process_record(
    record: &csv::StringRecord,
    headers: &[String],
    mgrs_col: usize,
    writer: &mut dyn OutputFormat,
    stats: &mut ProcessStats,
    strict: bool,
) -> Result<()> {
    stats.total_rows += 1;
    let mgrs_value = record.get(mgrs_col).unwrap_or("").trim();

    let (lat, lon, mgrs_src) = if !mgrs_value.is_empty() && detect::is_likely_mgrs(mgrs_value) {
        match convert::mgrs_to_latlon(mgrs_value) {
            Ok(coord) => (Some(coord.latitude), Some(coord.longitude), Some(mgrs_value.to_string())),
            Err(e) => {
                stats.failed_rows += 1;
                eprintln!("Warning: row {}: failed to convert '{}': {}", stats.total_rows, mgrs_value, e);
                if strict {
                    return Err(e.context(format!("Strict mode: aborting at row {}", stats.total_rows)));
                }
                (None, None, Some(mgrs_value.to_string()))
            }
        }
    } else {
        stats.failed_rows += 1;
        if !mgrs_value.is_empty() {
            eprintln!("Warning: row {}: '{}' does not look like MGRS", stats.total_rows, mgrs_value);
        }
        if strict && !mgrs_value.is_empty() {
            anyhow::bail!("Strict mode: non-MGRS value '{}' at row {}", mgrs_value, stats.total_rows);
        }
        (None, None, None)
    };

    if lat.is_some() {
        stats.succeeded_rows += 1;
    }

    let fields: Vec<String> = record.iter().map(|f| f.to_string()).collect();
    writer.write_row(&ConvertedRow {
        fields,
        headers: headers.to_vec(),
        latitude: lat,
        longitude: lon,
        mgrs_source: mgrs_src,
    })?;

    Ok(())
}

/// Process a CSV input, converting lat/lon columns to MGRS, writing CSV output.
/// The input CSV must have columns that look like latitude and longitude values.
pub fn process_to_mgrs<R: Read, W: Write>(
    input: R,
    output: W,
    _format: FormatKind, // Currently only CSV output for to-mgrs
    config: &ProcessConfig,
    precision: u8,
) -> Result<ProcessStats> {
    let mut reader = csv::Reader::from_reader(BufReader::new(input));
    let headers: Vec<String> = reader.headers()?.iter().map(|h| h.to_string()).collect();

    // Find lat/lon columns
    let (lat_col, lon_col) = match &config.column {
        Some(ColumnSpec::Name(name)) => {
            // If user specifies a column, use it as lat; find lon nearby
            let lat_idx = headers.iter().position(|h| h.to_lowercase().contains(&name.to_lowercase()))
                .with_context(|| format!("Column '{}' not found", name))?;
            let lon_idx = headers.iter().position(|h| {
                let lower = h.to_lowercase();
                lower.contains("lon") || lower.contains("lng")
            }).with_context(|| "Could not find a longitude column")?;
            (lat_idx, lon_idx)
        }
        Some(ColumnSpec::Index(idx)) => {
            // Assume idx is lat, idx+1 is lon
            (*idx, idx + 1)
        }
        None => {
            // Auto-detect: find columns named like "lat" and "lon"/"lng"
            let lat_idx = headers.iter().position(|h| {
                let lower = h.to_lowercase();
                lower.contains("lat")
            }).with_context(|| "Could not find a latitude column. Use --column to specify.")?;

            let lon_idx = headers.iter().position(|h| {
                let lower = h.to_lowercase();
                lower.contains("lon") || lower.contains("lng")
            }).with_context(|| "Could not find a longitude column.")?;

            (lat_idx, lon_idx)
        }
    };

    // Write output as CSV with MGRS column appended
    let mut csv_writer = csv::Writer::from_writer(output);

    let mut out_headers: Vec<&str> = headers.iter().map(|h| h.as_str()).collect();
    out_headers.push("MGRS");
    csv_writer.write_record(&out_headers)?;

    let mut stats = ProcessStats {
        total_rows: 0,
        succeeded_rows: 0,
        failed_rows: 0,
    };

    for result in reader.records() {
        let record = result?;
        stats.total_rows += 1;

        let lat_str = record.get(lat_col).unwrap_or("").trim();
        let lon_str = record.get(lon_col).unwrap_or("").trim();

        let mgrs_value = match (lat_str.parse::<f64>(), lon_str.parse::<f64>()) {
            (Ok(lat), Ok(lon)) => {
                match convert::latlon_to_mgrs(lat, lon, precision) {
                    Ok(mgrs) => {
                        stats.succeeded_rows += 1;
                        mgrs.0
                    }
                    Err(e) => {
                        stats.failed_rows += 1;
                        eprintln!("Warning: row {}: failed to convert ({}, {}): {}", stats.total_rows, lat_str, lon_str, e);
                        if config.strict {
                            return Err(e.context(format!("Strict mode: aborting at row {}", stats.total_rows)));
                        }
                        String::new()
                    }
                }
            }
            _ => {
                stats.failed_rows += 1;
                if !lat_str.is_empty() || !lon_str.is_empty() {
                    eprintln!("Warning: row {}: invalid lat/lon values '{}', '{}'", stats.total_rows, lat_str, lon_str);
                }
                if config.strict {
                    anyhow::bail!("Strict mode: invalid coordinates at row {}", stats.total_rows);
                }
                String::new()
            }
        };

        let mut out_record: Vec<String> = record.iter().map(|f| f.to_string()).collect();
        out_record.push(mgrs_value);
        csv_writer.write_record(&out_record)?;
    }

    csv_writer.flush()?;
    Ok(stats)
}

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

    fn sample_csv() -> &'static str {
        "Name,MGRS,Notes\nWhite House,18SUJ2337006519,DC landmark\nInvalid,NOTMGRS,Bad data\n"
    }

    #[test]
    fn test_stream_processor_csv_output() {
        let input = std::io::Cursor::new(sample_csv());
        let mut output = Vec::new();
        let config = ProcessConfig {
            column: None,
            strict: false,
            name_column: None,
        };
        let stats = process_to_latlon(input, &mut output, FormatKind::Csv, &config).unwrap();
        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("Latitude"));
        assert!(result.contains("Longitude"));
        assert!(result.contains("White House"));
        assert_eq!(stats.total_rows, 2);
        assert_eq!(stats.failed_rows, 1); // "NOTMGRS" fails
    }

    #[test]
    fn test_stream_processor_auto_detects_column() {
        let input = std::io::Cursor::new(sample_csv());
        let mut output = Vec::new();
        let config = ProcessConfig {
            column: None,
            strict: false,
            name_column: None,
        };
        let stats = process_to_latlon(input, &mut output, FormatKind::Csv, &config).unwrap();
        assert!(stats.succeeded_rows >= 1);
    }

    #[test]
    fn test_stream_processor_explicit_column_by_name() {
        let input = std::io::Cursor::new(sample_csv());
        let mut output = Vec::new();
        let config = ProcessConfig {
            column: Some(ColumnSpec::Name("MGRS".to_string())),
            strict: false,
            name_column: None,
        };
        let stats = process_to_latlon(input, &mut output, FormatKind::Csv, &config).unwrap();
        assert!(stats.total_rows > 0);
    }

    #[test]
    fn test_stream_processor_strict_mode_fails_on_error() {
        let input = std::io::Cursor::new(sample_csv());
        let mut output = Vec::new();
        let config = ProcessConfig {
            column: None,
            strict: true,
            name_column: None,
        };
        let result = process_to_latlon(input, &mut output, FormatKind::Csv, &config);
        assert!(result.is_err());
    }

    #[test]
    fn test_process_to_mgrs() {
        let csv_data = "Name,Latitude,Longitude\nDC,38.8977,-77.0365\n";
        let input = std::io::Cursor::new(csv_data);
        let mut output = Vec::new();
        let config = ProcessConfig {
            column: None,
            strict: false,
            name_column: None,
        };
        let stats = process_to_mgrs(input, &mut output, FormatKind::Csv, &config, 5).unwrap();
        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("MGRS"), "Output should have MGRS header: {}", result);
        assert!(result.contains("18S"), "Output should contain MGRS grid zone: {}", result);
        assert_eq!(stats.total_rows, 1);
        assert_eq!(stats.succeeded_rows, 1);
    }

    #[test]
    fn test_stream_processor_geojson_output() {
        let input = std::io::Cursor::new(sample_csv());
        let mut output = Vec::new();
        let config = ProcessConfig {
            column: None,
            strict: false,
            name_column: None,
        };
        let _stats = process_to_latlon(input, &mut output, FormatKind::GeoJson, &config).unwrap();
        let result = String::from_utf8(output).unwrap();
        let json: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(json["type"], "FeatureCollection");
    }
}