pub struct ShapeReader<T> { /* private fields */ }
Expand description

This reader only reads the .shp and optionally the (.shx) files of a shapefile.

Implementations§

source§

impl<T: Read> ShapeReader<T>

source

pub fn new(source: T) -> Result<Self, Error>

Creates a new ShapeReader from a source that implements the Read trait

The Shapefile header is read upon creation (but no reading of the Shapes is done)

§Errors

Will forward any std::io::Error

Will also return an error if the data is not a shapefile (Wrong file code)

Will also return an error if the shapetype read from the input source is invalid

§Example
use std::fs::File;
let file = File::open("tests/data/line.shp")?;
let reader = shapefile::ShapeReader::new(file)?;
source

pub fn with_shx<ShxSource>( source: T, shx_source: ShxSource ) -> Result<Self, Error>
where ShxSource: Read,

Creates a new ShapeReader using 2 sources, one for the .shp the other for the .shx

The .shp header is read upon creation and the whole .shx file is read upon creation.

§Example
use std::fs::File;
let shp_file = File::open("cities.shp")?;
let shx_file = File::open("cities.shx:")?;
let reader = shapefile::ShapeReader::with_shx(shp_file, shx_file)?;
source

pub fn header(&self) -> &Header

Returns a non-mutable reference to the header read

§Examples
let reader = shapefile::ShapeReader::from_path("tests/data/pointz.shp")?;
let header = reader.header();
assert_eq!(header.shape_type, shapefile::ShapeType::PointZ);
source§

impl<T: Read + Seek> ShapeReader<T>

source

pub fn read_as<S: ReadableShape>(self) -> Result<Vec<S>, Error>

Reads all the shape as shape of a certain type.

To be used if you know in advance which shape type the file contains.

§Errors

The function has an additional error that is returned if the shape type you asked to be read does not match the actual shape type in the file.

§Examples
use shapefile::ShapeReader;
let mut reader = ShapeReader::from_path("tests/data/linem.shp")?;
let polylines_m = reader.read_as::<shapefile::PolylineM>(); // we ask for the correct type
assert_eq!(polylines_m.is_ok(), true);
use shapefile::ShapeReader;
let mut reader = ShapeReader::from_path("tests/data/linem.shp")?;
let polylines = reader.read_as::<shapefile::Polyline>(); // we ask for the wrong type
assert_eq!(polylines.is_err(), true);
source

pub fn read(self) -> Result<Vec<Shape>, Error>

Reads all the shapes and returns them

§Examples
let mut reader = shapefile::ShapeReader::from_path("tests/data/multipoint.shp")?;
let shapes = reader.read()?;
for shape in shapes {
    match shape {
        shapefile::Shape::Multipoint(pts) => println!(" Yay Multipoints: {}", pts),
        _ => panic!("ups, not multipoints"),
    }
}
source

pub fn iter_shapes_as<S: ReadableShape>(&mut self) -> ShapeIterator<'_, T, S>

Returns an iterator that tries to read the shapes as the specified type Will return an error of the type S does not match the actual type in the file

§Examples
let mut reader = shapefile::ShapeReader::from_path("tests/data/multipoint.shp")?;
for multipoints in reader.iter_shapes_as::<shapefile::Multipoint>() {
    let points = multipoints?;
    println!("{}", points);
}
source

pub fn iter_shapes(&mut self) -> ShapeIterator<'_, T, Shape>

Returns an iterator that to reads the shapes wraps them in the enum Shape You do not need to call this method and can iterate over the Reader directly

§Examples
let mut reader = shapefile::ShapeReader::from_path("tests/data/multipoint.shp")?;
for shape in reader.iter_shapes() {
    match shape? {
        shapefile::Shape::Multipatch(shp) => println!("Multipoint!"),
        _ => println!("Other type of shape"),
    }
}
let mut reader = shapefile::ShapeReader::from_path("tests/data/multipoint.shp")?;
for shape in reader.iter_shapes() {
    match shape? {
        shapefile::Shape::Multipatch(shp) => println!("Multipoint!"),
        _ => println!("Other type of shape"),
    }
}
source

pub fn read_nth_shape_as<S: ReadableShape>( &mut self, index: usize ) -> Option<Result<S, Error>>

Reads the nth shape of the shapefile

§Important

Even though in shapefiles, shapes are indexed starting from ‘1’. this method expects indexes starting from 0.

§Returns

None if the index is out of range

§Errors

This method will return an Error::MissingIndexFile if you use it but no .shx was found when opening the shapefile.

source

pub fn read_nth_shape(&mut self, index: usize) -> Option<Result<Shape, Error>>

Reads the nth shape of the shapefile

source

pub fn seek(&mut self, index: usize) -> Result<(), Error>

Seek to the start of the shape at index

§Error

Returns Error::MissingIndexFile if the shx file was not found by ShapeReader::from_path or the reader was not constructed with ShapeReader::with_shx

source

pub fn shape_count(&self) -> Result<usize, Error>

Returns the number of shapes in the shapefile

§Error

Returns Error::MissingIndexFile if the shx file was not found by ShapeReader::from_path or the reader was not constructed with ShapeReader::with_shx

§Example
let reader = shapefile::ShapeReader::from_path("tests/data/point.shp").unwrap();
// point.shp has a .shx file next to it, so we can read the count data
assert_eq!(1, reader.shape_count().unwrap());

let reader = shapefile::ShapeReader::from_path("tests/data/pointm.shp").unwrap();
// There is no pointm.shx, so the shape_count() method returns error
assert!(reader.shape_count().is_err(), "Should return error if no index file");
source§

impl ShapeReader<BufReader<File>>

source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Auto Trait Implementations§

§

impl<T> Freeze for ShapeReader<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ShapeReader<T>
where T: RefUnwindSafe,

§

impl<T> Send for ShapeReader<T>
where T: Send,

§

impl<T> Sync for ShapeReader<T>
where T: Sync,

§

impl<T> Unpin for ShapeReader<T>
where T: Unpin,

§

impl<T> UnwindSafe for ShapeReader<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.