Struct shapefile::reader::ShapeReader[][src]

pub struct ShapeReader<T: Read> { /* fields omitted */ }

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

Implementations

impl<T: Read> ShapeReader<T>[src]

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

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)?;

pub fn with_shx(source: T, shx_source: T) -> Result<Self, Error>[src]

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)?;

pub fn header(&self) -> &Header[src]

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);

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

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);

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

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"),
    }
}

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

Notable traits for ShapeIterator<'a, T, S>

impl<'a, T: Read, S: ReadableShape> Iterator for ShapeIterator<'a, T, S> type Item = Result<S, Error>;
[src]

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);
}

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

Notable traits for ShapeIterator<'a, T, S>

impl<'a, T: Read, S: ReadableShape> Iterator for ShapeIterator<'a, T, S> type Item = Result<S, Error>;
[src]

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"),
    }
}

impl<T: Read + Seek> ShapeReader<T>[src]

Sources that implements Seek have access to a few more methods that uses the index file(.shx)

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

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.

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

Reads the nth shape of the shapefile

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

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

impl ShapeReader<BufReader<File>>[src]

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

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.