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
//! Shapefile writer - coordinates writing to .shp, .dbf, and .shx files
//!
//! This module provides a high-level interface for writing Shapefiles,
//! creating geometry in .shp, attributes in .dbf, and spatial index in .shx.
//! Supports 2D, Z (3D), and M (measured) geometry variants.

pub mod multipatch;
pub mod point_z_m;
pub mod polygon_z_m;
pub mod polyline_z_m;

use crate::dbf::{DbfRecord, DbfWriter, FieldDescriptor, FieldType, FieldValue as DbfFieldValue};
use crate::error::{Result, ShapefileError};
use crate::reader::ShapefileFeature;
use crate::shp::header::BoundingBox;
use crate::shp::shapes::ShapeType;
use crate::shp::{Shape, ShpWriter};
use crate::shx::ShxWriter;
use oxigdal_core::vector::{Feature, FieldValue, Geometry};
use std::fs::File;
use std::path::{Path, PathBuf};

/// Shapefile writer that coordinates .shp, .dbf, and .shx files
pub struct ShapefileWriter {
    /// Base path (without extension)
    base_path: PathBuf,
    /// Shape type for the Shapefile
    shape_type: ShapeType,
    /// Field descriptors for attributes
    field_descriptors: Vec<FieldDescriptor>,
    /// Bounding box (will be updated as features are added)
    bbox: BoundingBox,
    /// Optional CRS WKT to write to .prj file
    crs: Option<String>,
}

impl ShapefileWriter {
    /// Creates a new Shapefile writer
    pub fn new<P: AsRef<Path>>(
        base_path: P,
        shape_type: ShapeType,
        field_descriptors: Vec<FieldDescriptor>,
    ) -> Result<Self> {
        // Initialize with empty bounding box (will be updated)
        let bbox = BoundingBox::new_2d(0.0, 0.0, 0.0, 0.0)?;

        Ok(Self {
            base_path: base_path.as_ref().to_path_buf(),
            shape_type,
            field_descriptors,
            bbox,
            crs: None,
        })
    }

    /// Sets the CRS as a WKT string.
    ///
    /// When set, `write_features` will write a `<base>.prj` file alongside the
    /// `.shp`/`.dbf`/`.shx` files containing the WKT string.
    pub fn set_crs(&mut self, wkt: &str) {
        self.crs = Some(wkt.to_string());
    }

    /// Writes features to the Shapefile
    pub fn write_features(&mut self, features: &[ShapefileFeature]) -> Result<()> {
        if features.is_empty() {
            return Err(ShapefileError::invalid_geometry(
                "cannot write empty feature collection",
            ));
        }

        // Calculate bounding box from all features
        self.bbox = Self::calculate_bbox(features)?;

        // Open output files
        let shp_path = Self::with_extension(&self.base_path, "shp");
        let dbf_path = Self::with_extension(&self.base_path, "dbf");
        let shx_path = Self::with_extension(&self.base_path, "shx");

        let shp_file = File::create(&shp_path)?;
        let mut shp_writer = ShpWriter::new(shp_file, self.shape_type, self.bbox.clone());

        let dbf_file = File::create(&dbf_path)?;
        let mut dbf_writer = DbfWriter::new(dbf_file, self.field_descriptors.clone())?;

        let shx_file = File::create(&shx_path)?;
        let mut shx_writer = ShxWriter::new(shx_file, self.shape_type, self.bbox.clone());

        // Write headers
        shp_writer.write_header()?;
        dbf_writer.write_header()?;

        // Write features
        let mut current_offset = 50; // Header is 100 bytes = 50 words

        for feature in features {
            // Convert geometry to Shape
            let shape = Self::geometry_to_shape(&feature.geometry)?;

            // Calculate content length (includes shape type)
            let content_length = 2 + shape.content_length(); // +2 for shape type

            // Add to index (content_length should match .shp record header)
            shx_writer.add_entry(current_offset, content_length);

            // Write shape
            shp_writer.write_record(shape)?;

            // Convert attributes to DBF record
            let dbf_record = Self::attributes_to_dbf(&feature.attributes, &self.field_descriptors)?;
            dbf_writer.write_record(&dbf_record)?;

            // Update offset (record header is 8 bytes = 4 words)
            current_offset += 4 + content_length;
        }

        // Flush all writers before updating headers
        shp_writer.flush()?;
        dbf_writer.flush()?;

        // Update file lengths in headers
        shp_writer.update_file_length()?;
        dbf_writer.update_record_count()?;

        // Finalize and flush remaining files
        shx_writer.write_all()?;
        shx_writer.flush()?;

        // DBF finalize consumes the writer
        dbf_writer.finalize()?;

        // Explicitly drop remaining writers to ensure all data is written
        drop(shp_writer);
        drop(shx_writer);

        // Write .prj file if a CRS was set
        if let Some(ref wkt) = self.crs {
            let prj_path = Self::with_extension(&self.base_path, "prj");
            std::fs::write(&prj_path, wkt).map_err(ShapefileError::Io)?;
        }

        Ok(())
    }

    /// Writes OxiGDAL features to the Shapefile
    pub fn write_oxigdal_features(&mut self, features: &[Feature]) -> Result<()> {
        let shapefile_features: Vec<ShapefileFeature> = features
            .iter()
            .enumerate()
            .map(|(i, feature)| {
                let geometry = feature.geometry.clone();
                let attributes: std::collections::HashMap<String, FieldValue> = feature
                    .properties
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect();

                ShapefileFeature::new((i + 1) as i32, geometry, attributes)
            })
            .collect();

        self.write_features(&shapefile_features)
    }

    /// Calculates the bounding box from features
    fn calculate_bbox(features: &[ShapefileFeature]) -> Result<BoundingBox> {
        let mut x_min = f64::INFINITY;
        let mut y_min = f64::INFINITY;
        let mut x_max = f64::NEG_INFINITY;
        let mut y_max = f64::NEG_INFINITY;
        let mut z_min = f64::INFINITY;
        let mut z_max = f64::NEG_INFINITY;
        let mut m_min = f64::INFINITY;
        let mut m_max = f64::NEG_INFINITY;
        let mut has_z = false;
        let mut has_m = false;

        for feature in features {
            if let Some(geometry) = &feature.geometry {
                if geometry.has_z() {
                    has_z = true;
                }
                if geometry.has_m() {
                    has_m = true;
                }
                match geometry {
                    Geometry::Point(point) => {
                        x_min = x_min.min(point.coord.x);
                        y_min = y_min.min(point.coord.y);
                        x_max = x_max.max(point.coord.x);
                        y_max = y_max.max(point.coord.y);
                        if let Some(z) = point.coord.z {
                            z_min = z_min.min(z);
                            z_max = z_max.max(z);
                        }
                        if let Some(m) = point.coord.m {
                            m_min = m_min.min(m);
                            m_max = m_max.max(m);
                        }
                    }
                    Geometry::LineString(linestring) => {
                        for coord in &linestring.coords {
                            x_min = x_min.min(coord.x);
                            y_min = y_min.min(coord.y);
                            x_max = x_max.max(coord.x);
                            y_max = y_max.max(coord.y);
                            if let Some(z) = coord.z {
                                z_min = z_min.min(z);
                                z_max = z_max.max(z);
                            }
                            if let Some(m) = coord.m {
                                m_min = m_min.min(m);
                                m_max = m_max.max(m);
                            }
                        }
                    }
                    Geometry::Polygon(polygon) => {
                        for coord in polygon
                            .exterior
                            .coords
                            .iter()
                            .chain(polygon.interiors.iter().flat_map(|r| r.coords.iter()))
                        {
                            x_min = x_min.min(coord.x);
                            y_min = y_min.min(coord.y);
                            x_max = x_max.max(coord.x);
                            y_max = y_max.max(coord.y);
                            if let Some(z) = coord.z {
                                z_min = z_min.min(z);
                                z_max = z_max.max(z);
                            }
                            if let Some(m) = coord.m {
                                m_min = m_min.min(m);
                                m_max = m_max.max(m);
                            }
                        }
                    }
                    Geometry::MultiPoint(multipoint) => {
                        for point in &multipoint.points {
                            x_min = x_min.min(point.coord.x);
                            y_min = y_min.min(point.coord.y);
                            x_max = x_max.max(point.coord.x);
                            y_max = y_max.max(point.coord.y);
                            if let Some(z) = point.coord.z {
                                z_min = z_min.min(z);
                                z_max = z_max.max(z);
                            }
                            if let Some(m) = point.coord.m {
                                m_min = m_min.min(m);
                                m_max = m_max.max(m);
                            }
                        }
                    }
                    Geometry::MultiLineString(multilinestring) => {
                        for coord in multilinestring
                            .line_strings
                            .iter()
                            .flat_map(|ls| ls.coords.iter())
                        {
                            x_min = x_min.min(coord.x);
                            y_min = y_min.min(coord.y);
                            x_max = x_max.max(coord.x);
                            y_max = y_max.max(coord.y);
                            if let Some(z) = coord.z {
                                z_min = z_min.min(z);
                                z_max = z_max.max(z);
                            }
                            if let Some(m) = coord.m {
                                m_min = m_min.min(m);
                                m_max = m_max.max(m);
                            }
                        }
                    }
                    Geometry::MultiPolygon(multipolygon) => {
                        for coord in multipolygon.polygons.iter().flat_map(|poly| {
                            poly.exterior
                                .coords
                                .iter()
                                .chain(poly.interiors.iter().flat_map(|r| r.coords.iter()))
                        }) {
                            x_min = x_min.min(coord.x);
                            y_min = y_min.min(coord.y);
                            x_max = x_max.max(coord.x);
                            y_max = y_max.max(coord.y);
                            if let Some(z) = coord.z {
                                z_min = z_min.min(z);
                                z_max = z_max.max(z);
                            }
                            if let Some(m) = coord.m {
                                m_min = m_min.min(m);
                                m_max = m_max.max(m);
                            }
                        }
                    }
                    Geometry::GeometryCollection(collection) => {
                        for geom in &collection.geometries {
                            if let Some((gx_min, gy_min, gx_max, gy_max)) = geom.bounds() {
                                x_min = x_min.min(gx_min);
                                y_min = y_min.min(gy_min);
                                x_max = x_max.max(gx_max);
                                y_max = y_max.max(gy_max);
                            }
                        }
                    }
                }
            }
        }

        if x_min.is_infinite() {
            return Err(ShapefileError::invalid_geometry(
                "could not calculate bounding box",
            ));
        }

        let mut bbox = BoundingBox::new_2d(x_min, y_min, x_max, y_max)?;

        if has_z && !z_min.is_infinite() {
            bbox.z_min = Some(z_min);
            bbox.z_max = Some(z_max);
        }
        if has_m && !m_min.is_infinite() {
            bbox.m_min = Some(m_min);
            bbox.m_max = Some(m_max);
        }

        Ok(bbox)
    }

    /// Converts an OxiGDAL Geometry to a Shape, dispatching to Z/M variants
    /// when the geometry carries Z or M coordinates.
    fn geometry_to_shape(geometry: &Option<Geometry>) -> Result<Shape> {
        match geometry {
            None => Ok(Shape::Null),
            Some(geom) => dispatch_geometry(geom),
        }
    }

    /// Converts attributes to a DBF record
    fn attributes_to_dbf(
        attributes: &std::collections::HashMap<String, FieldValue>,
        field_descriptors: &[FieldDescriptor],
    ) -> Result<DbfRecord> {
        let mut values = Vec::with_capacity(field_descriptors.len());

        for field in field_descriptors {
            let value = attributes
                .get(&field.name)
                .cloned()
                .unwrap_or(FieldValue::Null);

            let dbf_value = match value {
                FieldValue::String(s) => DbfFieldValue::String(s),
                FieldValue::Integer(i) => DbfFieldValue::Integer(i),
                FieldValue::Float(f) => DbfFieldValue::Float(f),
                FieldValue::Bool(b) => DbfFieldValue::Boolean(b),
                FieldValue::Null => DbfFieldValue::Null,
                FieldValue::UInteger(u) => DbfFieldValue::Integer(u as i64),
                FieldValue::Date(d) => DbfFieldValue::Date(format!("{d}")),
                FieldValue::Blob(_) | FieldValue::Array(_) | FieldValue::Object(_) => {
                    DbfFieldValue::Null
                }
            };

            values.push(dbf_value);
        }

        Ok(DbfRecord::new(values))
    }

    /// 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
        }
    }
}

/// Dispatches a `Geometry` to the correct `Shape` variant, including Z and M.
fn dispatch_geometry(geom: &Geometry) -> Result<Shape> {
    let has_z = geom.has_z();
    let has_m = geom.has_m();

    match geom {
        Geometry::Point(point) => point_z_m::geometry_point_to_shape(point, has_z, has_m),
        Geometry::LineString(linestring) => {
            polyline_z_m::geometry_linestring_to_shape(linestring, has_z, has_m)
        }
        Geometry::Polygon(polygon) => polygon_z_m::geometry_polygon_to_shape(polygon, has_z, has_m),
        Geometry::MultiPoint(multipoint) => {
            point_z_m::geometry_multipoint_to_shape(multipoint, has_z, has_m)
        }
        Geometry::MultiLineString(multilinestring) => {
            polyline_z_m::geometry_multilinestring_to_shape(multilinestring, has_z, has_m)
        }
        Geometry::MultiPolygon(multipolygon) => {
            polygon_z_m::geometry_multipolygon_to_shape(multipolygon, has_z, has_m)
        }
        Geometry::GeometryCollection(_) => Err(ShapefileError::invalid_geometry(
            "GeometryCollection is not supported in Shapefile format",
        )),
    }
}

/// Builder for creating Shapefile field descriptors
pub struct ShapefileSchemaBuilder {
    fields: Vec<FieldDescriptor>,
}

impl ShapefileSchemaBuilder {
    /// Creates a new schema builder
    pub fn new() -> Self {
        Self { fields: Vec::new() }
    }

    /// Adds a character field
    pub fn add_character_field(mut self, name: &str, length: u8) -> Result<Self> {
        let field = FieldDescriptor::new(name.to_string(), FieldType::Character, length, 0)?;
        self.fields.push(field);
        Ok(self)
    }

    /// Adds a numeric field
    pub fn add_numeric_field(mut self, name: &str, length: u8, decimals: u8) -> Result<Self> {
        let field = FieldDescriptor::new(name.to_string(), FieldType::Number, length, decimals)?;
        self.fields.push(field);
        Ok(self)
    }

    /// Adds a logical field
    pub fn add_logical_field(mut self, name: &str) -> Result<Self> {
        let field = FieldDescriptor::new(name.to_string(), FieldType::Logical, 1, 0)?;
        self.fields.push(field);
        Ok(self)
    }

    /// Adds a date field
    pub fn add_date_field(mut self, name: &str) -> Result<Self> {
        let field = FieldDescriptor::new(name.to_string(), FieldType::Date, 8, 0)?;
        self.fields.push(field);
        Ok(self)
    }

    /// Builds the field descriptors
    pub fn build(self) -> Vec<FieldDescriptor> {
        self.fields
    }
}

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

#[cfg(test)]
#[allow(clippy::panic, clippy::assertions_on_constants)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_schema_builder() {
        let schema = ShapefileSchemaBuilder::new()
            .add_character_field("NAME", 50)
            .expect("Failed to add NAME field")
            .add_numeric_field("VALUE", 10, 2)
            .expect("Failed to add VALUE field")
            .add_logical_field("ACTIVE")
            .expect("Failed to add ACTIVE field")
            .build();

        assert_eq!(schema.len(), 3);
        assert_eq!(schema[0].name, "NAME");
        assert_eq!(schema[0].field_type, FieldType::Character);
        assert_eq!(schema[1].name, "VALUE");
        assert_eq!(schema[2].name, "ACTIVE");
    }

    #[test]
    fn test_bbox_calculation() {
        let features = vec![
            ShapefileFeature::new(
                1,
                Some(Geometry::Point(oxigdal_core::vector::Point::new(
                    10.0, 20.0,
                ))),
                HashMap::new(),
            ),
            ShapefileFeature::new(
                2,
                Some(Geometry::Point(oxigdal_core::vector::Point::new(
                    30.0, 40.0,
                ))),
                HashMap::new(),
            ),
            ShapefileFeature::new(
                3,
                Some(Geometry::Point(oxigdal_core::vector::Point::new(
                    -5.0, 15.0,
                ))),
                HashMap::new(),
            ),
        ];

        let bbox = ShapefileWriter::calculate_bbox(&features).expect("Failed to calculate bbox");
        assert!((bbox.x_min - (-5.0)).abs() < f64::EPSILON);
        assert!((bbox.y_min - 15.0).abs() < f64::EPSILON);
        assert!((bbox.x_max - 30.0).abs() < f64::EPSILON);
        assert!((bbox.y_max - 40.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_geometry_to_shape_2d_point() {
        let geometry = Some(Geometry::Point(oxigdal_core::vector::Point::new(
            10.5, 20.3,
        )));
        let shape =
            ShapefileWriter::geometry_to_shape(&geometry).expect("Failed to convert geometry");

        if let Shape::Point(point) = shape {
            assert!((point.x - 10.5).abs() < f64::EPSILON);
            assert!((point.y - 20.3).abs() < f64::EPSILON);
        } else {
            panic!("Expected Point shape");
        }
    }

    #[test]
    fn test_geometry_to_shape_3d_point() {
        use oxigdal_core::vector::Coordinate;
        let geometry = Some(Geometry::Point(oxigdal_core::vector::Point::from_coord(
            Coordinate::new_3d(10.5, 20.3, 55.0),
        )));
        let shape =
            ShapefileWriter::geometry_to_shape(&geometry).expect("Failed to convert 3D point");

        if let Shape::PointZ(pz) = shape {
            assert!((pz.x - 10.5).abs() < f64::EPSILON);
            assert!((pz.y - 20.3).abs() < f64::EPSILON);
            assert!((pz.z - 55.0).abs() < f64::EPSILON);
        } else {
            panic!("Expected PointZ shape, got {:?}", shape);
        }
    }

    #[test]
    fn test_geometry_to_shape_polygon_z_type() {
        use oxigdal_core::vector::{Coordinate, LineString, Polygon};
        let exterior = LineString::new(vec![
            Coordinate::new_3d(0.0, 0.0, 1.0),
            Coordinate::new_3d(1.0, 0.0, 1.5),
            Coordinate::new_3d(1.0, 1.0, 2.0),
            Coordinate::new_3d(0.0, 1.0, 1.0),
            Coordinate::new_3d(0.0, 0.0, 1.0),
        ])
        .expect("valid exterior");
        let poly = Polygon::new(exterior, vec![]).expect("valid polygon");
        let geometry = Some(Geometry::Polygon(poly));
        let shape =
            ShapefileWriter::geometry_to_shape(&geometry).expect("Failed to convert PolygonZ");

        if let Shape::PolygonZ(sz) = shape {
            assert_eq!(sz.base.num_points, 5);
            assert_eq!(sz.z_values.len(), 5);
            assert!((sz.z_values[0] - 1.0).abs() < f64::EPSILON);
            assert!((sz.z_values[2] - 2.0).abs() < f64::EPSILON);
        } else {
            panic!("Expected PolygonZ shape, got {:?}", shape);
        }
    }
}