oxigdal-shapefile 0.1.3

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
# OxiGDAL Shapefile Driver

[![Crates.io](https://img.shields.io/crates/v/oxigdal-shapefile.svg)](https://crates.io/crates/oxigdal-shapefile)
[![Documentation](https://docs.rs/oxigdal-shapefile/badge.svg)](https://docs.rs/oxigdal-shapefile)
[![License](https://img.shields.io/crates/l/oxigdal-shapefile.svg)](LICENSE)
[![Pure Rust](https://img.shields.io/badge/pure-rust-brightgreen)](#pure-rust)

A pure Rust implementation of ESRI Shapefile format support for the OxiGDAL ecosystem. Read and write complete Shapefiles (.shp, .dbf, .shx) with full geometry type support, attribute handling, and spatial indexing.

## Features

- **Pure Rust Implementation** - Zero C/C++/Fortran dependencies; works everywhere Rust compiles
- **Complete File Format Support** - Reads and writes all three core files (.shp geometry, .dbf attributes, .shx spatial index)
- **14 Geometry Types** - Point, PointZ, PointM, PolyLine, PolyLineZ, PolyLineM, Polygon, PolygonZ, PolygonM, MultiPoint, MultiPointZ, MultiPointM, MultiPatch, and Null types
- **All DBF Field Types** - Character, Number, Logical, Date, and Float fields with proper encoding
- **Spatial Indexing** - SHX file support for fast random access to records
- **Round-Trip Compatibility** - Read → modify → write workflow without data loss
- **No Unsafe** - Sound error handling with comprehensive `Result<T>` types; no `unwrap()` or `panic!()` in production code
- **Buffered I/O** - Efficient streaming for large files
- **OxiGDAL Integration** - Native conversion to/from OxiGDAL vector types
- **Feature Flags** - Optional async, Arrow support, and no-std compatibility

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
oxigdal-shapefile = "0.1"
```

### Optional Features

```toml
[dependencies]
oxigdal-shapefile = { version = "0.1", features = ["async", "arrow"] }
```

- **`std`** (default) - Standard library support
- **`async`** - Tokio-based asynchronous I/O
- **`arrow`** - Apache Arrow integration for columnar operations

## Quick Start

### Reading Shapefiles

```rust
use oxigdal_shapefile::ShapefileReader;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Open a Shapefile (reads .shp, .dbf, and .shx automatically)
    let reader = ShapefileReader::open("path/to/shapefile")?;

    // Read all features at once
    let features = reader.read_features()?;

    // Access geometry and attributes
    for feature in &features {
        println!("Record #{}: {:?}", feature.record_number, feature.geometry);
        println!("Attributes: {:?}", feature.attributes);
    }

    Ok(())
}
```

### Writing Shapefiles

```rust
use oxigdal_shapefile::{ShapefileFeature, ShapefileWriter};
use oxigdal_shapefile::shp::shapes::ShapeType;
use oxigdal_shapefile::writer::ShapefileSchemaBuilder;
use oxigdal_core::vector::{Coordinate, Geometry, Point as CorePoint};
use std::collections::HashMap;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create schema with attribute fields
    let schema = ShapefileSchemaBuilder::new()
        .add_character_field("NAME", 50)?
        .add_numeric_field("POPULATION", 10, 0)?
        .add_numeric_field("DENSITY", 8, 2)?
        .build();

    // Create writer for Point shapefile
    let mut writer = ShapefileWriter::new(
        "cities",
        ShapeType::Point,
        schema
    )?;

    // Create features with geometry and attributes
    let mut features = vec![];

    // Feature 1
    let point = Geometry::Point(CorePoint::new_2d(-73.9352, 40.7306)?);
    let mut attrs = HashMap::new();
    attrs.insert("NAME".to_string(),
                 oxigdal_core::vector::PropertyValue::String("New York".to_string()));
    attrs.insert("POPULATION".to_string(),
                 oxigdal_core::vector::PropertyValue::Integer(8_000_000));
    attrs.insert("DENSITY".to_string(),
                 oxigdal_core::vector::PropertyValue::Float(27_000.0));

    features.push(ShapefileFeature::new(1, Some(point), attrs));

    // Write all features (creates cities.shp, cities.dbf, cities.shx)
    writer.write_features(&features)?;

    Ok(())
}
```

## Usage Examples

### Reading Individual Records

```rust
use oxigdal_shapefile::ShapefileReader;
use std::path::Path;

fn process_shapefile(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let reader = ShapefileReader::open(path)?;

    // Get all features
    let features = reader.read_features()?;

    // Process each feature
    for feature in features {
        // Access geometry type
        if let Some(geom) = &feature.geometry {
            match geom.geometry_type() {
                oxigdal_core::vector::GeometryType::Point => {
                    println!("Point feature at record {}", feature.record_number);
                }
                oxigdal_core::vector::GeometryType::LineString => {
                    println!("LineString feature at record {}", feature.record_number);
                }
                oxigdal_core::vector::GeometryType::Polygon => {
                    println!("Polygon feature at record {}", feature.record_number);
                }
                _ => {}
            }
        }

        // Access attributes
        if let Some(name) = feature.attributes.get("NAME") {
            println!("Name: {:?}", name);
        }
    }

    Ok(())
}
```

### Working with Different Geometry Types

```rust
use oxigdal_shapefile::shp::shapes::ShapeType;

// Check geometry type capabilities
let point_type = ShapeType::Point;
assert!(!point_type.has_z());
assert!(!point_type.has_m());

let point_z = ShapeType::PointZ;
assert!(point_z.has_z());
assert!(!point_z.has_m());

let point_m = ShapeType::PointM;
assert!(!point_m.has_z());
assert!(point_m.has_m());

// Polygon with Z coordinates
let polygon_z = ShapeType::PolygonZ;
assert!(polygon_z.has_z());
```

### Building Complex Schemas

```rust
use oxigdal_shapefile::writer::ShapefileSchemaBuilder;

let schema = ShapefileSchemaBuilder::new()
    // Character fields (up to 254 characters)
    .add_character_field("NAME", 100)?
    .add_character_field("DESCRIPTION", 254)?

    // Numeric fields (precision.scale)
    .add_numeric_field("ID", 10, 0)?
    .add_numeric_field("VALUE", 15, 2)?

    // Other field types
    .add_logical_field("ACTIVE")?
    .add_date_field("CREATED")?
    .add_float_field("SCORE")?

    .build();
```

### Error Handling

```rust
use oxigdal_shapefile::{ShapefileReader, ShapefileError};

fn read_safely(path: &str) -> Result<(), ShapefileError> {
    let reader = ShapefileReader::open(path)
        .map_err(|e| {
            eprintln!("Failed to open shapefile: {}", e);
            e
        })?;

    let features = reader.read_features()?;
    println!("Successfully read {} features", features.len());

    Ok(())
}

// All errors use Result<T> pattern - no unwrap() calls
match read_safely("data.shp") {
    Ok(()) => println!("Success"),
    Err(e) => eprintln!("Error: {}", e),
}
```

## API Overview

### Core Types

| Type | Description |
|------|-------------|
| `ShapefileReader` | High-level interface for reading complete Shapefiles |
| `ShapefileWriter` | High-level interface for writing Shapefiles |
| `ShapefileFeature` | Combines geometry and attributes from a Shapefile record |
| `ShapeType` | Enum of all 14 supported geometry types |
| `ShapefileError` | Comprehensive error type with contextual information |

### Geometry Modules

| Module | Description |
|--------|-------------|
| `shp` | Shapefile geometry format (.shp files) and Shape types |
| `dbf` | dBase attribute format (.dbf files), field types, and records |
| `shx` | Spatial index format (.shx files) for fast record access |
| `reader` | High-level reading interface combining all three files |
| `writer` | High-level writing interface with schema builder |
| `error` | Comprehensive error handling with Result type |

### Key Traits

- `FieldType` - Enumeration of DBF field types
- `FieldValue` - Attribute values (String, Integer, Float, Date, Logical)
- `Geometry` - OxiGDAL integration for vector geometries

## Supported Geometry Types

### 2D Geometries
- **Point** - Single coordinate point
- **PolyLine** - Connected line segments (LineString)
- **Polygon** - Closed rings with optional holes
- **MultiPoint** - Multiple disconnected points

### 3D Geometries (with Z)
- **PointZ** - Point with elevation/height
- **PolyLineZ** - LineString with Z coordinates
- **PolygonZ** - Polygon with Z coordinates
- **MultiPointZ** - MultiPoint with Z coordinates

### Measured Geometries (with M)
- **PointM** - Point with measurement value
- **PolyLineM** - LineString with M values
- **PolygonM** - Polygon with M values
- **MultiPointM** - MultiPoint with M values

### Other
- **MultiPatch** - 3D surface (limited support)
- **Null** - Empty geometry

## Supported DBF Field Types

| Type | Description | Max Length |
|------|-------------|-----------|
| Character | Text strings | 254 bytes |
| Number | Fixed-point numbers with precision | 20 digits |
| Float | Double-precision floating point | 20 digits |
| Logical | Boolean (Y/N) | 1 byte |
| Date | Calendar dates (YYYYMMDD) | 8 bytes |

## File Format Details

### Shapefile (.shp)

```
Header (100 bytes):
  [0-3]    File Code (9994 - big endian)
  [4-7]    File Length in 16-bit words (big endian)
  [8-11]   Version (1000 - little endian)
  [12-15]  Shape Type (little endian)
  [16-83]  Bounding Box (4 doubles: Xmin, Ymin, Xmax, Ymax)
  [84-99]  Z/M ranges (if 3D)

Records (variable):
  [0-3]    Record Number (big endian)
  [4-7]    Content Length in 16-bit words (big endian)
  [8+]     Shape content (little endian)
```

### dBase (.dbf)

```
Header (32 bytes):
  [0]      Version
  [1-3]    Last Update (YY, MM, DD)
  [4-7]    Record Count
  [8-9]    Header Size
  [10-11]  Record Size
  [12-31]  Reserved

Field Descriptors (32 bytes each):
  [0-10]   Field Name (null-padded)
  [11]     Field Type (C/N/F/L/D)
  [12-15]  Reserved
  [16]     Field Length
  [17]     Decimal Count
  [18-31]  Reserved

Record Data:
  [0]      Deletion Flag (0x20 = active, 0x2A = deleted)
  [1+]     Field data (fixed-length)

Terminator: 0x1A
```

### Spatial Index (.shx)

```
Header (100 bytes): Same as .shp

Index Entries (8 bytes each):
  [0-3]    Record Offset in 16-bit words (big endian)
  [4-7]    Content Length in 16-bit words (big endian)
```

## Performance Characteristics

### Reading
- **Header parsing**: Negligible (O(1))
- **Feature loading**: O(n) where n = number of features
- **Memory usage**: One feature at a time with streaming API (future)
- **Large files**: Buffered I/O optimizes disk access

### Writing
- **Feature writing**: O(n) with automatic bounding box calculation
- **File generation**: All three files (.shp, .dbf, .shx) written simultaneously
- **Validation**: Compile-time type safety + runtime error checking

## Examples

See the [examples](examples/) directory for practical demonstrations:

- `create_test_shapefile_samples.rs` - Create Shapefiles with various geometry types
- `verify_shapefile_samples.rs` - Read and validate Shapefile integrity

Run examples with:

```bash
cargo run --package oxigdal-shapefile --example create_test_shapefile_samples
cargo run --package oxigdal-shapefile --example verify_shapefile_samples
```

## Integration with OxiGDAL

Convert to OxiGDAL types:

```rust
use oxigdal_shapefile::ShapefileReader;

let reader = ShapefileReader::open("data")?;
let shapefile_features = reader.read_features()?;

// Convert to OxiGDAL Features
let oxigdal_features: Result<Vec<_>, _> = shapefile_features
    .iter()
    .map(|f| f.to_oxigdal_feature())
    .collect();
```

Create from OxiGDAL types:

```rust
use oxigdal_shapefile::{ShapefileFeature, ShapefileWriter};
use oxigdal_core::vector::Feature;

fn shapefile_from_oxigdal(
    features: &[Feature],
    output_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let shapefile_features: Vec<_> = features
        .iter()
        .enumerate()
        .map(|(idx, feature)| {
            let mut attrs = std::collections::HashMap::new();
            // Copy properties
            for (key, value) in feature.properties() {
                attrs.insert(key.clone(), value.clone());
            }
            ShapefileFeature::new((idx + 1) as i32, Some(feature.geometry().clone()), attrs)
        })
        .collect();

    let schema = ShapefileSchemaBuilder::new().build();
    let mut writer = ShapefileWriter::new(output_path, ShapeType::Point, schema)?;
    writer.write_features(&shapefile_features)?;

    Ok(())
}
```

## COOLJAPAN Policies

This library strictly adheres to COOLJAPAN ecosystem standards:

### Pure Rust
- 100% pure Rust with zero C/C++/Fortran dependencies
- Works on any platform that Rust supports
- No platform-specific code or conditional compilation (except `std` feature)

### No `unwrap()` Policy
- All fallible operations return descriptive `Result<T, ShapefileError>`
- Comprehensive error types with contextual information
- Safe error handling throughout the entire codebase

### Clean Architecture
- Single-responsibility modules (shp, dbf, shx, reader, writer)
- Clear public API with re-exports
- Extensive documentation with examples
- All files kept under 2000 lines using splitrs if needed

### Testing
- Unit tests for all major functions
- Integration tests for round-trip operations
- Property-based tests for format validation
- Comprehensive error case coverage

### Performance
- Zero-copy where possible
- Buffered I/O for large files
- Efficient spatial indexing

## Limitations

- Point geometry conversion to OxiGDAL is fully supported
- PolyLine, Polygon, and MultiPoint parsing is implemented, conversion pending
- MultiPatch (3D surfaces) has limited support
- No support for memo fields (.dbt files)
- No support for extended .prj (projection) parsing beyond reading
- Single-threaded design (async feature for I/O only)

## References

- [ESRI Shapefile Technical Description]https://www.esri.com/content/dam/esrisites/sitecore-archive/Files/Pdfs/library/whitepapers/pdfs/shapefile.pdf
- [dBase File Format Specification]http://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm
- [OxiGDAL Documentation]https://github.com/cool-japan/oxigdal

## Testing

Run the full test suite:

```bash
cargo test --all-features --package oxigdal-shapefile
```

Run with no default features:

```bash
cargo test --no-default-features --package oxigdal-shapefile
```

Run specific examples:

```bash
cargo test --package oxigdal-shapefile --test '*'
```

## Documentation

Full API documentation is available at [docs.rs/oxigdal-shapefile](https://docs.rs/oxigdal-shapefile).

Generate local documentation:

```bash
cargo doc --package oxigdal-shapefile --open
```

## Contributing

Contributions are welcome! Please ensure:

- All tests pass: `cargo test --all-features`
- No clippy warnings: `cargo clippy --all-features`
- Code adheres to COOLJAPAN policies (no unwrap, pure Rust, etc.)
- Documentation is updated for public APIs

## License

Licensed under the Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0).

## Related Projects

Part of the [OxiGDAL](https://github.com/cool-japan/oxigdal) ecosystem:

- **OxiGDAL Core** - Pure Rust GDAL alternative
- **OxiGDAL Drivers** - Format drivers (GeoTIFF, NetCDF, HDF5, etc.)
- **SciRS2** - Scientific computing ecosystem
- **NumRS2** - Numerical computing (NumPy-like)
- **OxiBLAS** - Pure Rust BLAS operations
- **Oxicode** - Rust serialization (bincode replacement)

---

**Part of the [COOLJAPAN](https://github.com/cool-japan) ecosystem** - Pure Rust geospatial and scientific computing.