15 unstable releases

new 0.7.0 Apr 5, 2025
0.6.0 Mar 23, 2024
0.5.0 Aug 8, 2023
0.4.0 Feb 27, 2023
0.0.1 Jan 21, 2015

#12 in Geospatial

Download history 2266/week @ 2024-12-20 1752/week @ 2024-12-27 2408/week @ 2025-01-03 2774/week @ 2025-01-10 3239/week @ 2025-01-17 3812/week @ 2025-01-24 3968/week @ 2025-01-31 4348/week @ 2025-02-07 4141/week @ 2025-02-14 4193/week @ 2025-02-21 3424/week @ 2025-02-28 3902/week @ 2025-03-07 4155/week @ 2025-03-14 3276/week @ 2025-03-21 4012/week @ 2025-03-28 5513/week @ 2025-04-04

17,725 downloads per month
Used in 17 crates (12 directly)

MIT license

215KB
5K SLoC

shapefile-rs

Rust library to read & write shapefiles .dbf files supported via the dbase crate

let mut reader = shapefile::Reader::from_path(filename).unwrap();

for result in reader.iter_shapes_and_records() {
    let (shape, record) = result.unwrap();
    println ! ("Shape: {}, records: ", shape);
    for (name, value) in record {
        println ! ("\t{}: {:?}, ", name, value);
    }
    println ! ();
}

You can check out examples in the examples folder


lib.rs:

Read & Write Shapefile in Rust

A shapefile is in reality a collection of 3 mandatory files:

  • .shp (feature geometry aka shapes)
  • .shx (index of feature geometry)
  • .dbf (attribute information, aka records)

As different shapefiles can store different type of shapes (but one shapefile can only store the same type of shapes) This library provide two ways of reading the shapes:

  1. Reading as Shape and then do a match to handle the different shapes
  2. Reading directly as concrete shapes (ie Polyline, PolylineZ, Point, etc) this of course only works if the file actually contains shapes that matches the requested type

dBase

The attributes (stored in the .dbg) files are read and written using the dbase crate which is re-exported so you can use use shapefile::dbase. dBase files may have different encoding which may only be supported if either one of the following features is enabled:

  • encoding_rs (notably supports GBK encoding)
  • yore

Shapefiles shapes

The Point, PointM and PointZ are the base data types of shapefiles, the other shapes (Polyline, Multipoint, ...) are collections of these type of points with different semantics (multiple parts or no, closed parts or no, ...)

With the exception of the Multipatch shape, each shape as a variant for each type of point. (Multipatch always uses PointZ) Eg: For the polyline, there is Polyline, PolylineM, PolylineZ

Reading

For more details see the reader module

Writing

To write a file see the writer module

Features

The geo-types feature can be enabled to have access to From and TryFrom implementations allowing to convert (or try to) back and forth between shapefile's type and the one in geo_types

The yore or encoding_rs feature can be activated to allows the dbase crate to handle files with special encodings.

Dependencies

~1–2MB
~50K SLoC