oxigdal-shapefile 0.1.4

Shapefile (ESRI) driver for OxiGDAL - Pure Rust GDAL reimplementation
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
//! Shapefile reader - coordinates reading from .shp, .dbf, and .shx files
//!
//! This module provides a high-level interface for reading Shapefiles,
//! combining geometry from .shp, attributes from .dbf, and spatial index from .shx.

use crate::dbf::{DbfReader, FieldDescriptor};
use crate::error::{Result, ShapefileError};
use crate::shp::{Shape, ShapefileHeader, ShpReader};
use crate::shx::{IndexEntry, ShxReader};
use oxigdal_core::vector::{
    Coordinate, Feature, FieldValue, Geometry, LineString as CoreLineString,
    MultiLineString as CoreMultiLineString, MultiPoint as CoreMultiPoint, Point as CorePoint,
    Polygon as CorePolygon,
};
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};

/// A complete Shapefile feature (geometry + attributes)
#[derive(Debug, Clone)]
pub struct ShapefileFeature {
    /// Record number (1-based)
    pub record_number: i32,
    /// Geometry
    pub geometry: Option<Geometry>,
    /// Attributes (field name -> value)
    pub attributes: HashMap<String, FieldValue>,
}

impl ShapefileFeature {
    /// Creates a new Shapefile feature
    pub fn new(
        record_number: i32,
        geometry: Option<Geometry>,
        attributes: HashMap<String, FieldValue>,
    ) -> Self {
        Self {
            record_number,
            geometry,
            attributes,
        }
    }

    /// Converts to an OxiGDAL Feature
    pub fn to_oxigdal_feature(&self) -> Result<Feature> {
        let geometry = self
            .geometry
            .clone()
            .ok_or_else(|| ShapefileError::invalid_geometry("feature has no geometry"))?;

        let mut feature = Feature::new(geometry);

        // Convert attributes
        for (key, value) in &self.attributes {
            feature.set_property(key, value.clone());
        }

        Ok(feature)
    }
}

/// Shapefile reader that coordinates .shp, .dbf, and optionally .shx files
pub struct ShapefileReader {
    /// Base path (without extension)
    base_path: PathBuf,
    /// .shp file header
    header: ShapefileHeader,
    /// Field descriptors from .dbf
    field_descriptors: Vec<FieldDescriptor>,
    /// Index entries from .shx (if available)
    index_entries: Option<Vec<IndexEntry>>,
    /// CRS as WKT string from .prj file (if present)
    pub crs: Option<String>,
    /// Character encoding from .cpg file (if present)
    pub encoding: Option<String>,
}

impl ShapefileReader {
    /// Opens a Shapefile from a base path (without extension)
    ///
    /// Reads the .shp, .dbf, and optionally .shx, .prj, and .cpg files.
    pub fn open<P: AsRef<Path>>(base_path: P) -> Result<Self> {
        let base_path = base_path.as_ref();

        // Construct file paths
        let shp_path = Self::with_extension(base_path, "shp");
        let dbf_path = Self::with_extension(base_path, "dbf");
        let shx_path = Self::with_extension(base_path, "shx");
        let prj_path = Self::with_extension(base_path, "prj");
        let cpg_path = Self::with_extension(base_path, "cpg");

        // Open .shp file
        let shp_file = File::open(&shp_path).map_err(|_| ShapefileError::MissingFile {
            file_type: ".shp".to_string(),
        })?;
        let shp_reader = BufReader::new(shp_file);
        let shp_reader = ShpReader::new(shp_reader)?;
        let header = shp_reader.header().clone();

        // Open .dbf file
        let dbf_file = File::open(&dbf_path).map_err(|_| ShapefileError::MissingFile {
            file_type: ".dbf".to_string(),
        })?;
        let dbf_reader = BufReader::new(dbf_file);
        let dbf_reader = DbfReader::new(dbf_reader)?;
        let field_descriptors = dbf_reader.field_descriptors().to_vec();

        // Open .shx file (optional)
        let index_entries = if shx_path.exists() {
            let shx_file = File::open(&shx_path).ok();
            if let Some(file) = shx_file {
                let shx_reader = BufReader::new(file);
                let mut shx_reader = ShxReader::new(shx_reader)?;
                Some(shx_reader.read_all_entries()?)
            } else {
                None
            }
        } else {
            None
        };

        // Read .prj file (optional) — contains CRS as WKT string
        let crs = if prj_path.exists() {
            std::fs::read_to_string(&prj_path)
                .ok()
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
        } else {
            None
        };

        // Read .cpg file (optional) — contains character encoding name
        // NOTE: Encoding information is stored but DBF string fields currently use
        // String::from_utf8_lossy as a fallback for non-UTF-8 data. Full encoding
        // transcoding via encoding_rs is a follow-up item.
        let encoding = if cpg_path.exists() {
            std::fs::read_to_string(&cpg_path)
                .ok()
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
        } else {
            None
        };

        Ok(Self {
            base_path: base_path.to_path_buf(),
            header,
            field_descriptors,
            index_entries,
            crs,
            encoding,
        })
    }

    /// Returns the Shapefile header
    pub fn header(&self) -> &ShapefileHeader {
        &self.header
    }

    /// Returns the field descriptors
    pub fn field_descriptors(&self) -> &[FieldDescriptor] {
        &self.field_descriptors
    }

    /// Returns the index entries (if .shx was loaded)
    pub fn index_entries(&self) -> Option<&[IndexEntry]> {
        self.index_entries.as_deref()
    }

    /// Returns the CRS as a WKT string, if a .prj file was present
    pub fn crs(&self) -> Option<&str> {
        self.crs.as_deref()
    }

    /// Returns the character encoding name from the .cpg file, if present
    ///
    /// Common values: `"UTF-8"`, `"CP1252"`, `"ISO-8859-1"`.
    /// NOTE: Non-UTF-8 encodings are not yet transcoded; DBF fields use
    /// `String::from_utf8_lossy` as a fallback.
    pub fn encoding(&self) -> Option<&str> {
        self.encoding.as_deref()
    }

    /// Returns features whose bounding box intersects the given query bbox.
    ///
    /// Reads all shape records from `.shp` and `.dbf`, then filters to those
    /// whose bounding box overlaps `[min_x, min_y, max_x, max_y]` (inclusive).
    /// Point shapes use the point coordinate as a degenerate bbox.
    /// Null shapes are excluded.
    ///
    /// For large shapefiles this reads all geometry upfront; a full R-tree
    /// spatial index backed by lazy `.shx` seeks is a follow-up item.
    pub fn features_in_bbox(
        &mut self,
        min_x: f64,
        min_y: f64,
        max_x: f64,
        max_y: f64,
    ) -> Result<Vec<ShapefileFeature>> {
        let all_features = self.read_features()?;

        let filtered = all_features
            .into_iter()
            .filter(|feature| {
                let Some(ref geom) = feature.geometry else {
                    return false;
                };
                if let Some((fx_min, fy_min, fx_max, fy_max)) = Self::geometry_bbox(geom) {
                    // Standard AABB intersection test (inclusive on edges)
                    !(fx_max < min_x || fx_min > max_x || fy_max < min_y || fy_min > max_y)
                } else {
                    false
                }
            })
            .collect();

        Ok(filtered)
    }

    /// Extracts a 2-D bounding box `(x_min, y_min, x_max, y_max)` from a geometry.
    ///
    /// Delegates to the `Geometry::bounds()` method defined in `oxigdal-core`,
    /// which returns `None` for degenerate or empty geometries.
    fn geometry_bbox(geom: &Geometry) -> Option<(f64, f64, f64, f64)> {
        geom.bounds()
    }

    /// Returns a streaming iterator over the Shapefile's features.
    ///
    /// Unlike [`read_features`], which loads everything into memory, this opens
    /// fresh buffered readers and reads one SHP record + one DBF record per
    /// [`Iterator::next`] call.  Memory usage is therefore O(1) with respect
    /// to the number of features.
    ///
    /// # Errors
    ///
    /// Returns an error if the `.shp` or `.dbf` files cannot be opened, or if
    /// the header/field-descriptor section cannot be read.  Individual record
    /// errors are surfaced as `Err` items from the iterator.
    ///
    /// [`read_features`]: ShapefileReader::read_features
    pub fn iter_features(&self) -> Result<FeatureIter<'_>> {
        let shp_path = Self::with_extension(&self.base_path, "shp");
        let dbf_path = Self::with_extension(&self.base_path, "dbf");

        let shp_file = File::open(&shp_path)?;
        let shp_reader = BufReader::new(shp_file);
        let shp_reader = ShpReader::new(shp_reader)?;

        let dbf_file = File::open(&dbf_path)?;
        let dbf_reader = BufReader::new(dbf_file);
        let dbf_reader = DbfReader::new(dbf_reader)?;

        Ok(FeatureIter {
            shp_reader,
            dbf_reader,
            field_descriptors: &self.field_descriptors,
            done: false,
        })
    }

    /// Reads features that satisfy an arbitrary predicate closure.
    ///
    /// This is a convenience wrapper over [`ShapefileReader::read_features`] that filters the
    /// result set in-place without a second allocation.  The predicate receives
    /// a shared reference to each [`ShapefileFeature`] and returns `true` for
    /// features that should be included in the output.
    ///
    /// For structured attribute comparisons prefer
    /// [`read_features_filtered`](ShapefileReader::read_features_filtered),
    /// which accepts a [`crate::filter::FieldFilter`] directly.
    ///
    /// # Errors
    ///
    /// Propagates any I/O or parse errors from the underlying read.
    pub fn read_features_where<F>(&self, predicate: F) -> Result<Vec<ShapefileFeature>>
    where
        F: Fn(&ShapefileFeature) -> bool,
    {
        let all = self.read_features()?;
        Ok(all.into_iter().filter(|f| predicate(f)).collect())
    }

    /// Reads features that match a structured [`crate::filter::FieldFilter`].
    ///
    /// This is a thin convenience method over
    /// [`read_features_where`](ShapefileReader::read_features_where) that
    /// accepts a [`crate::filter::FieldFilter`] directly.
    ///
    /// # Errors
    ///
    /// Propagates any I/O or parse errors from the underlying read.
    pub fn read_features_filtered(
        &self,
        filter: &crate::filter::FieldFilter,
    ) -> Result<Vec<ShapefileFeature>> {
        self.read_features_where(|f| filter.matches(f))
    }

    /// Reads all features from the Shapefile
    pub fn read_features(&self) -> Result<Vec<ShapefileFeature>> {
        // Open files
        let shp_path = Self::with_extension(&self.base_path, "shp");
        let dbf_path = Self::with_extension(&self.base_path, "dbf");

        let shp_file = File::open(&shp_path)?;
        let shp_reader = BufReader::new(shp_file);
        let mut shp_reader = ShpReader::new(shp_reader)?;

        let dbf_file = File::open(&dbf_path)?;
        let dbf_reader = BufReader::new(dbf_file);
        let mut dbf_reader = DbfReader::new(dbf_reader)?;

        // Read all shape records
        let shape_records = shp_reader.read_all_records()?;

        // Read all DBF records
        let dbf_records = dbf_reader.read_all_records()?;

        // Verify record counts match
        if shape_records.len() != dbf_records.len() {
            return Err(ShapefileError::RecordMismatch {
                shp_count: shape_records.len(),
                dbf_count: dbf_records.len(),
            });
        }

        // Combine into features
        let mut features = Vec::with_capacity(shape_records.len());
        for (shape_record, dbf_record) in shape_records.iter().zip(dbf_records.iter()) {
            let geometry = Self::shape_to_geometry(&shape_record.shape)?;

            // Convert DBF record to attributes
            let attributes = Self::dbf_to_attributes(dbf_record, &self.field_descriptors);

            features.push(ShapefileFeature::new(
                shape_record.record_number,
                geometry,
                attributes,
            ));
        }

        Ok(features)
    }

    /// Converts a Shape to an OxiGDAL Geometry
    fn shape_to_geometry(shape: &Shape) -> Result<Option<Geometry>> {
        match shape {
            Shape::Null => Ok(None),
            Shape::Point(point) => {
                let oxigdal_point = CorePoint::new(point.x, point.y);
                Ok(Some(Geometry::Point(oxigdal_point)))
            }
            Shape::PointZ(point) => {
                use oxigdal_core::vector::Coordinate;
                let coord = if let Some(m) = point.m {
                    Coordinate::new_3dm(point.x, point.y, point.z, m)
                } else {
                    Coordinate::new_3d(point.x, point.y, point.z)
                };
                Ok(Some(Geometry::Point(CorePoint::from_coord(coord))))
            }
            Shape::PointM(point) => {
                use oxigdal_core::vector::Coordinate;
                let coord = Coordinate::new_2dm(point.x, point.y, point.m);
                Ok(Some(Geometry::Point(CorePoint::from_coord(coord))))
            }
            Shape::PolyLine(multi_part) => {
                if multi_part.parts.len() == 1 {
                    // Single part - convert to LineString
                    let coords: Vec<Coordinate> = multi_part
                        .points
                        .iter()
                        .map(|p| Coordinate::new_2d(p.x, p.y))
                        .collect();

                    if coords.len() < 2 {
                        return Ok(None);
                    }

                    let linestring = CoreLineString::new(coords).map_err(|e| {
                        ShapefileError::invalid_geometry(format!("Invalid LineString: {}", e))
                    })?;
                    Ok(Some(Geometry::LineString(linestring)))
                } else {
                    // Multiple parts - convert to MultiLineString
                    let mut linestrings = Vec::new();

                    for i in 0..multi_part.parts.len() {
                        let start_idx = multi_part.parts[i] as usize;
                        let end_idx = if i + 1 < multi_part.parts.len() {
                            multi_part.parts[i + 1] as usize
                        } else {
                            multi_part.points.len()
                        };

                        let coords: Vec<Coordinate> = multi_part.points[start_idx..end_idx]
                            .iter()
                            .map(|p| Coordinate::new_2d(p.x, p.y))
                            .collect();

                        if coords.len() >= 2 {
                            if let Ok(linestring) = CoreLineString::new(coords) {
                                linestrings.push(linestring);
                            }
                        }
                    }

                    if linestrings.is_empty() {
                        Ok(None)
                    } else {
                        Ok(Some(Geometry::MultiLineString(CoreMultiLineString::new(
                            linestrings,
                        ))))
                    }
                }
            }
            Shape::Polygon(multi_part) => {
                if multi_part.parts.is_empty() {
                    return Ok(None);
                }

                // First part is exterior ring
                let exterior_start = multi_part.parts[0] as usize;
                let exterior_end = if multi_part.parts.len() > 1 {
                    multi_part.parts[1] as usize
                } else {
                    multi_part.points.len()
                };

                let exterior_coords: Vec<Coordinate> = multi_part.points
                    [exterior_start..exterior_end]
                    .iter()
                    .map(|p| Coordinate::new_2d(p.x, p.y))
                    .collect();

                if exterior_coords.len() < 4 {
                    return Ok(None);
                }

                let exterior = CoreLineString::new(exterior_coords).map_err(|e| {
                    ShapefileError::invalid_geometry(format!("Invalid exterior ring: {}", e))
                })?;

                // Remaining parts are interior rings (holes)
                let mut interiors = Vec::new();
                for i in 1..multi_part.parts.len() {
                    let start_idx = multi_part.parts[i] as usize;
                    let end_idx = if i + 1 < multi_part.parts.len() {
                        multi_part.parts[i + 1] as usize
                    } else {
                        multi_part.points.len()
                    };

                    let interior_coords: Vec<Coordinate> = multi_part.points[start_idx..end_idx]
                        .iter()
                        .map(|p| Coordinate::new_2d(p.x, p.y))
                        .collect();

                    if interior_coords.len() >= 4 {
                        if let Ok(interior) = CoreLineString::new(interior_coords) {
                            interiors.push(interior);
                        }
                    }
                }

                let polygon = CorePolygon::new(exterior, interiors).map_err(|e| {
                    ShapefileError::invalid_geometry(format!("Invalid polygon: {}", e))
                })?;

                Ok(Some(Geometry::Polygon(polygon)))
            }
            Shape::MultiPoint(multi_part) => {
                let points: Vec<CorePoint> = multi_part
                    .points
                    .iter()
                    .map(|p| CorePoint::new(p.x, p.y))
                    .collect();

                if points.is_empty() {
                    Ok(None)
                } else {
                    Ok(Some(Geometry::MultiPoint(CoreMultiPoint::new(points))))
                }
            }
            // Z variants: reconstruct with Z (and optionally M) coordinates
            Shape::PolyLineZ(shape_z) => Self::multipart_z_to_linestring_geometry(
                &shape_z.base,
                &shape_z.z_values,
                shape_z.m_values.as_deref(),
            ),
            Shape::PolygonZ(shape_z) => Self::multipart_z_to_polygon_geometry(
                &shape_z.base,
                &shape_z.z_values,
                shape_z.m_values.as_deref(),
            ),
            Shape::MultiPointZ(shape_z) => Self::multipart_z_to_multipoint_geometry(
                &shape_z.base,
                &shape_z.z_values,
                shape_z.m_values.as_deref(),
            ),
            // M variants: reconstruct with M coordinates
            Shape::PolyLineM(shape_m) => {
                Self::multipart_m_to_linestring_geometry(&shape_m.base, &shape_m.m_values)
            }
            Shape::PolygonM(shape_m) => {
                Self::multipart_m_to_polygon_geometry(&shape_m.base, &shape_m.m_values)
            }
            Shape::MultiPointM(shape_m) => {
                Self::multipart_m_to_multipoint_geometry(&shape_m.base, &shape_m.m_values)
            }
            // MultiPatch: expose as a MultiPolygon using ring parts (outer/inner ring types)
            // or fall back to a point collection of the vertices.
            Shape::MultiPatch(mp_shape) => {
                // Represent patch vertices as a MultiPoint carrying Z coordinates.
                use oxigdal_core::vector::Coordinate;
                let points: Vec<CorePoint> = mp_shape
                    .base
                    .points
                    .iter()
                    .zip(mp_shape.z_values.iter())
                    .map(|(p, z)| CorePoint::from_coord(Coordinate::new_3d(p.x, p.y, *z)))
                    .collect();

                if points.is_empty() {
                    Ok(None)
                } else {
                    Ok(Some(Geometry::MultiPoint(CoreMultiPoint::new(points))))
                }
            }
        }
    }

    /// Converts a multi-part Z shape into a LineString or MultiLineString Geometry
    /// with Z (and optionally M) coordinates preserved.
    fn multipart_z_to_linestring_geometry(
        base: &crate::shp::MultiPartShape,
        z_values: &[f64],
        m_values: Option<&[f64]>,
    ) -> Result<Option<Geometry>> {
        use oxigdal_core::vector::Coordinate;

        let make_coord = |i: usize, p: &crate::shp::shapes::Point| -> Coordinate {
            let z = z_values.get(i).copied().unwrap_or(0.0);
            if let Some(mv) = m_values {
                Coordinate::new_3dm(p.x, p.y, z, mv.get(i).copied().unwrap_or(0.0))
            } else {
                Coordinate::new_3d(p.x, p.y, z)
            }
        };

        if base.parts.len() == 1 {
            let coords: Vec<Coordinate> = base
                .points
                .iter()
                .enumerate()
                .map(|(i, p)| make_coord(i, p))
                .collect();
            if coords.len() < 2 {
                return Ok(None);
            }
            let linestring = CoreLineString::new(coords).map_err(|e| {
                ShapefileError::invalid_geometry(format!("Invalid LineString: {}", e))
            })?;
            Ok(Some(Geometry::LineString(linestring)))
        } else {
            let mut linestrings = Vec::new();
            for i in 0..base.parts.len() {
                let start = base.parts[i] as usize;
                let end = if i + 1 < base.parts.len() {
                    base.parts[i + 1] as usize
                } else {
                    base.points.len()
                };
                let coords: Vec<Coordinate> = base.points[start..end]
                    .iter()
                    .enumerate()
                    .map(|(j, p)| make_coord(start + j, p))
                    .collect();
                if coords.len() >= 2 {
                    if let Ok(ls) = CoreLineString::new(coords) {
                        linestrings.push(ls);
                    }
                }
            }
            if linestrings.is_empty() {
                Ok(None)
            } else {
                Ok(Some(Geometry::MultiLineString(CoreMultiLineString::new(
                    linestrings,
                ))))
            }
        }
    }

    /// Converts a multi-part Z shape into a Polygon Geometry with Z coordinates.
    fn multipart_z_to_polygon_geometry(
        base: &crate::shp::MultiPartShape,
        z_values: &[f64],
        m_values: Option<&[f64]>,
    ) -> Result<Option<Geometry>> {
        use oxigdal_core::vector::Coordinate;

        if base.parts.is_empty() {
            return Ok(None);
        }

        let make_coord = |i: usize, p: &crate::shp::shapes::Point| -> Coordinate {
            let z = z_values.get(i).copied().unwrap_or(0.0);
            if let Some(mv) = m_values {
                Coordinate::new_3dm(p.x, p.y, z, mv.get(i).copied().unwrap_or(0.0))
            } else {
                Coordinate::new_3d(p.x, p.y, z)
            }
        };

        let ext_start = base.parts[0] as usize;
        let ext_end = if base.parts.len() > 1 {
            base.parts[1] as usize
        } else {
            base.points.len()
        };
        let ext_coords: Vec<Coordinate> = base.points[ext_start..ext_end]
            .iter()
            .enumerate()
            .map(|(j, p)| make_coord(ext_start + j, p))
            .collect();
        if ext_coords.len() < 4 {
            return Ok(None);
        }
        let exterior = CoreLineString::new(ext_coords).map_err(|e| {
            ShapefileError::invalid_geometry(format!("Invalid exterior Z ring: {}", e))
        })?;

        let mut interiors = Vec::new();
        for i in 1..base.parts.len() {
            let start = base.parts[i] as usize;
            let end = if i + 1 < base.parts.len() {
                base.parts[i + 1] as usize
            } else {
                base.points.len()
            };
            let coords: Vec<Coordinate> = base.points[start..end]
                .iter()
                .enumerate()
                .map(|(j, p)| make_coord(start + j, p))
                .collect();
            if coords.len() >= 4 {
                if let Ok(ring) = CoreLineString::new(coords) {
                    interiors.push(ring);
                }
            }
        }
        let polygon = CorePolygon::new(exterior, interiors)
            .map_err(|e| ShapefileError::invalid_geometry(format!("Invalid polygon Z: {}", e)))?;
        Ok(Some(Geometry::Polygon(polygon)))
    }

    /// Converts a multi-part Z shape into a MultiPoint Geometry with Z coordinates.
    fn multipart_z_to_multipoint_geometry(
        base: &crate::shp::MultiPartShape,
        z_values: &[f64],
        m_values: Option<&[f64]>,
    ) -> Result<Option<Geometry>> {
        use oxigdal_core::vector::Coordinate;
        let points: Vec<CorePoint> = base
            .points
            .iter()
            .enumerate()
            .map(|(i, p)| {
                let z = z_values.get(i).copied().unwrap_or(0.0);
                let coord = if let Some(mv) = m_values {
                    Coordinate::new_3dm(p.x, p.y, z, mv.get(i).copied().unwrap_or(0.0))
                } else {
                    Coordinate::new_3d(p.x, p.y, z)
                };
                CorePoint::from_coord(coord)
            })
            .collect();
        if points.is_empty() {
            Ok(None)
        } else {
            Ok(Some(Geometry::MultiPoint(CoreMultiPoint::new(points))))
        }
    }

    /// Converts a multi-part M shape into a LineString/MultiLineString with M.
    fn multipart_m_to_linestring_geometry(
        base: &crate::shp::MultiPartShape,
        m_values: &[f64],
    ) -> Result<Option<Geometry>> {
        use oxigdal_core::vector::Coordinate;

        let make_coord = |i: usize, p: &crate::shp::shapes::Point| -> Coordinate {
            Coordinate::new_2dm(p.x, p.y, m_values.get(i).copied().unwrap_or(0.0))
        };

        if base.parts.len() == 1 {
            let coords: Vec<Coordinate> = base
                .points
                .iter()
                .enumerate()
                .map(|(i, p)| make_coord(i, p))
                .collect();
            if coords.len() < 2 {
                return Ok(None);
            }
            let linestring = CoreLineString::new(coords).map_err(|e| {
                ShapefileError::invalid_geometry(format!("Invalid LineStringM: {}", e))
            })?;
            Ok(Some(Geometry::LineString(linestring)))
        } else {
            let mut linestrings = Vec::new();
            for i in 0..base.parts.len() {
                let start = base.parts[i] as usize;
                let end = if i + 1 < base.parts.len() {
                    base.parts[i + 1] as usize
                } else {
                    base.points.len()
                };
                let coords: Vec<Coordinate> = base.points[start..end]
                    .iter()
                    .enumerate()
                    .map(|(j, p)| make_coord(start + j, p))
                    .collect();
                if coords.len() >= 2 {
                    if let Ok(ls) = CoreLineString::new(coords) {
                        linestrings.push(ls);
                    }
                }
            }
            if linestrings.is_empty() {
                Ok(None)
            } else {
                Ok(Some(Geometry::MultiLineString(CoreMultiLineString::new(
                    linestrings,
                ))))
            }
        }
    }

    /// Converts a multi-part M shape into a Polygon with M.
    fn multipart_m_to_polygon_geometry(
        base: &crate::shp::MultiPartShape,
        m_values: &[f64],
    ) -> Result<Option<Geometry>> {
        use oxigdal_core::vector::Coordinate;

        if base.parts.is_empty() {
            return Ok(None);
        }

        let make_coord = |i: usize, p: &crate::shp::shapes::Point| -> Coordinate {
            Coordinate::new_2dm(p.x, p.y, m_values.get(i).copied().unwrap_or(0.0))
        };

        let ext_start = base.parts[0] as usize;
        let ext_end = if base.parts.len() > 1 {
            base.parts[1] as usize
        } else {
            base.points.len()
        };
        let ext_coords: Vec<Coordinate> = base.points[ext_start..ext_end]
            .iter()
            .enumerate()
            .map(|(j, p)| make_coord(ext_start + j, p))
            .collect();
        if ext_coords.len() < 4 {
            return Ok(None);
        }
        let exterior = CoreLineString::new(ext_coords).map_err(|e| {
            ShapefileError::invalid_geometry(format!("Invalid exterior M ring: {}", e))
        })?;

        let mut interiors = Vec::new();
        for i in 1..base.parts.len() {
            let start = base.parts[i] as usize;
            let end = if i + 1 < base.parts.len() {
                base.parts[i + 1] as usize
            } else {
                base.points.len()
            };
            let coords: Vec<Coordinate> = base.points[start..end]
                .iter()
                .enumerate()
                .map(|(j, p)| make_coord(start + j, p))
                .collect();
            if coords.len() >= 4 {
                if let Ok(ring) = CoreLineString::new(coords) {
                    interiors.push(ring);
                }
            }
        }
        let polygon = CorePolygon::new(exterior, interiors)
            .map_err(|e| ShapefileError::invalid_geometry(format!("Invalid polygon M: {}", e)))?;
        Ok(Some(Geometry::Polygon(polygon)))
    }

    /// Converts a multi-part M shape into a MultiPoint with M.
    fn multipart_m_to_multipoint_geometry(
        base: &crate::shp::MultiPartShape,
        m_values: &[f64],
    ) -> Result<Option<Geometry>> {
        use oxigdal_core::vector::Coordinate;
        let points: Vec<CorePoint> = base
            .points
            .iter()
            .enumerate()
            .map(|(i, p)| {
                CorePoint::from_coord(Coordinate::new_2dm(
                    p.x,
                    p.y,
                    m_values.get(i).copied().unwrap_or(0.0),
                ))
            })
            .collect();
        if points.is_empty() {
            Ok(None)
        } else {
            Ok(Some(Geometry::MultiPoint(CoreMultiPoint::new(points))))
        }
    }

    /// Converts a DBF record to FieldValue attributes
    fn dbf_to_attributes(
        dbf_record: &crate::dbf::DbfRecord,
        field_descriptors: &[FieldDescriptor],
    ) -> HashMap<String, FieldValue> {
        let mut attributes = HashMap::new();

        for (field, value) in field_descriptors.iter().zip(&dbf_record.values) {
            let property_value = match value {
                crate::dbf::FieldValue::String(s) => FieldValue::String(s.clone()),
                crate::dbf::FieldValue::Integer(i) => FieldValue::Integer(*i),
                crate::dbf::FieldValue::Float(f) => FieldValue::Float(*f),
                crate::dbf::FieldValue::Boolean(b) => FieldValue::Bool(*b),
                crate::dbf::FieldValue::Date(d) => FieldValue::String(d.clone()),
                crate::dbf::FieldValue::Null => FieldValue::Null,
            };

            attributes.insert(field.name.clone(), property_value);
        }

        attributes
    }

    /// Converts a DBF record to FieldValue attributes (static version for FeatureIter)
    pub(crate) fn dbf_to_attributes_pub(
        dbf_record: &crate::dbf::DbfRecord,
        field_descriptors: &[FieldDescriptor],
    ) -> HashMap<String, FieldValue> {
        Self::dbf_to_attributes(dbf_record, field_descriptors)
    }

    /// Converts a Shape to an OxiGDAL Geometry (public version for FeatureIter)
    pub(crate) fn shape_to_geometry_pub(shape: &Shape) -> Result<Option<Geometry>> {
        Self::shape_to_geometry(shape)
    }

    /// Helper to add extension to base path
    fn with_extension<P: AsRef<Path>>(base_path: P, ext: &str) -> PathBuf {
        let base = base_path.as_ref();

        // If base already has an extension, replace it
        if base.extension().is_some() {
            base.with_extension(ext)
        } else {
            // Otherwise, add the extension
            let mut path = base.to_path_buf();
            path.set_extension(ext);
            path
        }
    }
}

// ─── Streaming feature iterator ──────────────────────────────────────────────

/// A lazy, streaming iterator over the features in a Shapefile.
///
/// Created by [`ShapefileReader::iter_features`].  Reads one SHP record and one
/// DBF record per call to [`Iterator::next`], keeping memory usage at O(1) with
/// respect to the number of features.
///
/// The iterator is exhausted as soon as either the `.shp` or the `.dbf` reader
/// returns `None`, or a record-level I/O error occurs (which is surfaced as an
/// `Err` item).
pub struct FeatureIter<'a> {
    shp_reader: ShpReader<BufReader<File>>,
    dbf_reader: DbfReader<BufReader<File>>,
    /// Reference to the field descriptors owned by the parent `ShapefileReader`.
    field_descriptors: &'a [FieldDescriptor],
    /// Set to `true` after the first `None` or error to make the iterator fused.
    done: bool,
}

impl<'a> Iterator for FeatureIter<'a> {
    type Item = Result<ShapefileFeature>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }

        // Read one SHP record
        let shp_record = match self.shp_reader.read_record() {
            Ok(Some(r)) => r,
            Ok(None) => {
                self.done = true;
                return None;
            }
            Err(e) => {
                self.done = true;
                return Some(Err(e));
            }
        };

        // Read one DBF record
        let dbf_record = match self.dbf_reader.read_record() {
            Ok(Some(r)) => r,
            Ok(None) => {
                self.done = true;
                return None;
            }
            Err(e) => {
                self.done = true;
                return Some(Err(e));
            }
        };

        // Convert shape to geometry
        let geometry = match ShapefileReader::shape_to_geometry_pub(&shp_record.shape) {
            Ok(g) => g,
            Err(e) => {
                self.done = true;
                return Some(Err(e));
            }
        };

        // Convert DBF record to attributes
        let attributes =
            ShapefileReader::dbf_to_attributes_pub(&dbf_record, self.field_descriptors);

        Some(Ok(ShapefileFeature::new(
            shp_record.record_number,
            geometry,
            attributes,
        )))
    }
}

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

    #[test]
    fn test_path_extension_helper() {
        let base = std::env::temp_dir().join("oxigdal_shapefile_test");
        let expected_shp = std::env::temp_dir().join("oxigdal_shapefile_test.shp");
        assert_eq!(ShapefileReader::with_extension(&base, "shp"), expected_shp);

        let base_shp = std::env::temp_dir().join("oxigdal_shapefile_test.shp");
        let expected_dbf = std::env::temp_dir().join("oxigdal_shapefile_test.dbf");
        assert_eq!(
            ShapefileReader::with_extension(&base_shp, "dbf"),
            expected_dbf
        );
    }

    #[test]
    fn test_shapefile_feature_creation() {
        let mut attributes = HashMap::new();
        attributes.insert("name".to_string(), FieldValue::String("Test".to_string()));
        attributes.insert("value".to_string(), FieldValue::Integer(42));

        let geometry = Some(Geometry::Point(CorePoint::new(10.0, 20.0)));

        let feature = ShapefileFeature::new(1, geometry, attributes);
        assert_eq!(feature.record_number, 1);
        assert!(feature.geometry.is_some());
        assert_eq!(feature.attributes.len(), 2);
    }
}