Struct shapefile::reader::ShapeReader
source · 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>
impl<T: Read> ShapeReader<T>
sourcepub fn new(source: T) -> Result<Self, Error>
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)?;
sourcepub fn with_shx<ShxSource>(
source: T,
shx_source: ShxSource
) -> Result<Self, Error>where
ShxSource: Read,
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§impl<T: Read + Seek> ShapeReader<T>
impl<T: Read + Seek> ShapeReader<T>
sourcepub fn read_as<S: ReadableShape>(self) -> Result<Vec<S>, Error>
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);
sourcepub fn read(self) -> Result<Vec<Shape>, Error>
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"),
}
}
sourcepub fn iter_shapes_as<S: ReadableShape>(&mut self) -> ShapeIterator<'_, T, S> ⓘ
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);
}
sourcepub fn iter_shapes(&mut self) -> ShapeIterator<'_, T, Shape> ⓘ
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"),
}
}
sourcepub fn read_nth_shape_as<S: ReadableShape>(
&mut self,
index: usize
) -> Option<Result<S, Error>>
pub fn read_nth_shape_as<S: ReadableShape>( &mut self, index: usize ) -> Option<Result<S, Error>>
Reads the n
th 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.
sourcepub fn read_nth_shape(&mut self, index: usize) -> Option<Result<Shape, Error>>
pub fn read_nth_shape(&mut self, index: usize) -> Option<Result<Shape, Error>>
Reads the n
th shape of the shapefile
sourcepub fn seek(&mut self, index: usize) -> Result<(), Error>
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
sourcepub fn shape_count(&self) -> Result<usize, Error>
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");