Struct Writer

Source
pub struct Writer<T: Write + Seek> { /* private fields */ }
Expand description

The Writer writes a complete shapefile that is, it writes the 3 mandatory files (.shp, .shx, .dbf)

The recommended way to create a new shapefile is via the Writer::from_path or Writer::from_path_with_info associated functions.

§Examples

To create a Writer that writes a .dbf file that has the same structure as .dbf read earlier you will have to do:

let mut reader = shapefile::Reader::from_path("tests/data/multipatch.shp")?;
let shape_records = reader.read()?;
let table_info = reader.into_table_info();

let writer = shapefile::Writer::from_path_with_info("new_multipatch.shp", table_info);

Implementations§

Source§

impl<T: Write + Seek> Writer<T>

Source

pub fn new(shape_writer: ShapeWriter<T>, dbase_writer: TableWriter<T>) -> Self

Creates a new writer using the provided ShapeWriter and TableWriter

§Example

Creating a Writer that writes to in memory buffers.

use std::convert::TryInto;
let mut shp_dest = std::io::Cursor::new(Vec::<u8>::new());
let mut shx_dest = std::io::Cursor::new(Vec::<u8>::new());
let mut dbf_dest = std::io::Cursor::new(Vec::<u8>::new());

let shape_writer = shapefile::ShapeWriter::with_shx(&mut shp_dest, &mut shx_dest);
let dbase_writer = dbase::TableWriterBuilder::new()
    .add_character_field("Name".try_into().unwrap(), 50)
    .build_with_dest(&mut dbf_dest);

let shape_writer = shapefile::Writer::new(shape_writer, dbase_writer);
Source

pub fn write_shape_and_record<S: EsriShape, R: WritableRecord>( &mut self, shape: &S, record: &R, ) -> Result<(), Error>

Examples found in repository?
examples/create.rs (line 34)
4fn example_1() {
5    const FILE_NAME: &str = "hello_shape_1.shp";
6
7    let square = Polyline::new(vec![
8        Point::new(0.0, 0.0),
9        Point::new(0.0, 1.0),
10        Point::new(1.0, 1.0),
11        Point::new(1.0, 0.0),
12    ]);
13
14    let bigger_square = Polyline::new(vec![
15        Point::new(0.0, 0.0),
16        Point::new(0.0, 10.0),
17        Point::new(10.0, 10.0),
18        Point::new(10.0, 0.0),
19    ]);
20
21    // Create the builder for the accompanying dbase (.dbf) file
22    // For this simple example we will only have a single field
23    // "Name", with 55 chars max
24    let table_builder =
25        TableWriterBuilder::new().add_character_field("Name".try_into().unwrap(), 55);
26
27    {
28        let mut writer = Writer::from_path(FILE_NAME, table_builder)
29            .expect("Failed to create the shapefile writer");
30
31        let mut first_record = dbase::Record::default();
32        first_record.insert("Name".to_string(), "Square".to_string().into());
33        writer
34            .write_shape_and_record(&square, &first_record)
35            .expect("Failed to write first record");
36
37        let mut second_record = dbase::Record::default();
38        second_record.insert("Name".to_string(), "Big Square".to_string().into());
39        writer
40            .write_shape_and_record(&bigger_square, &second_record)
41            .expect("Failed to write second record");
42    }
43
44    println!("File created, you can use `cargo run --example print-content {FILE_NAME}`");
45}
46
47fn example_2() {
48    dbase_record!(
49        #[derive(Debug)]
50        struct UserRecord {
51            first_name: String,
52            last_name: String,
53        }
54    );
55
56    const FILE_NAME: &str = "hello_shape_2.shp";
57
58    let square = Polyline::new(vec![
59        Point::new(0.0, 0.0),
60        Point::new(0.0, 1.0),
61        Point::new(1.0, 1.0),
62        Point::new(1.0, 0.0),
63    ]);
64
65    let bigger_square = Polyline::new(vec![
66        Point::new(0.0, 0.0),
67        Point::new(0.0, 10.0),
68        Point::new(10.0, 10.0),
69        Point::new(10.0, 0.0),
70    ]);
71
72    // Create the builder for the accompanying dbase (.dbf) file
73    let table_builder = TableWriterBuilder::new()
74        .add_character_field("FirstName".try_into().unwrap(), 55)
75        .add_character_field("LastName".try_into().unwrap(), 55);
76
77    {
78        let mut writer = Writer::from_path(FILE_NAME, table_builder)
79            .expect("Failed to create the shapefile writer");
80
81        let first_record = UserRecord {
82            first_name: "Yoshi".to_string(),
83            last_name: "Green".to_string(),
84        };
85
86        writer
87            .write_shape_and_record(&square, &first_record)
88            .expect("Failed to write first record");
89
90        let second_record = UserRecord {
91            first_name: "Yoshi".to_string(),
92            last_name: "Red".to_string(),
93        };
94        writer
95            .write_shape_and_record(&bigger_square, &second_record)
96            .expect("Failed to write second record");
97    }
98
99    println!("File created, you can use `cargo run --example print-content {FILE_NAME}`");
100}
Source

pub fn write_shapes_and_records<'a, S: EsriShape + 'a, R: WritableRecord + 'a, C: IntoIterator<Item = (&'a S, &'a R)>>( self, container: C, ) -> Result<(), Error>

Source§

impl Writer<BufWriter<File>>

Source

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

Creates all the files needed for the shapefile to be complete (.shp, .shx, .dbf)

use std::convert::TryInto;
let table_builder = dbase::TableWriterBuilder::new()
    .add_character_field("name".try_into().unwrap(), 50);
let writer = shapefile::Writer::from_path("new_cities.shp", table_builder)?;
Examples found in repository?
examples/create.rs (line 28)
4fn example_1() {
5    const FILE_NAME: &str = "hello_shape_1.shp";
6
7    let square = Polyline::new(vec![
8        Point::new(0.0, 0.0),
9        Point::new(0.0, 1.0),
10        Point::new(1.0, 1.0),
11        Point::new(1.0, 0.0),
12    ]);
13
14    let bigger_square = Polyline::new(vec![
15        Point::new(0.0, 0.0),
16        Point::new(0.0, 10.0),
17        Point::new(10.0, 10.0),
18        Point::new(10.0, 0.0),
19    ]);
20
21    // Create the builder for the accompanying dbase (.dbf) file
22    // For this simple example we will only have a single field
23    // "Name", with 55 chars max
24    let table_builder =
25        TableWriterBuilder::new().add_character_field("Name".try_into().unwrap(), 55);
26
27    {
28        let mut writer = Writer::from_path(FILE_NAME, table_builder)
29            .expect("Failed to create the shapefile writer");
30
31        let mut first_record = dbase::Record::default();
32        first_record.insert("Name".to_string(), "Square".to_string().into());
33        writer
34            .write_shape_and_record(&square, &first_record)
35            .expect("Failed to write first record");
36
37        let mut second_record = dbase::Record::default();
38        second_record.insert("Name".to_string(), "Big Square".to_string().into());
39        writer
40            .write_shape_and_record(&bigger_square, &second_record)
41            .expect("Failed to write second record");
42    }
43
44    println!("File created, you can use `cargo run --example print-content {FILE_NAME}`");
45}
46
47fn example_2() {
48    dbase_record!(
49        #[derive(Debug)]
50        struct UserRecord {
51            first_name: String,
52            last_name: String,
53        }
54    );
55
56    const FILE_NAME: &str = "hello_shape_2.shp";
57
58    let square = Polyline::new(vec![
59        Point::new(0.0, 0.0),
60        Point::new(0.0, 1.0),
61        Point::new(1.0, 1.0),
62        Point::new(1.0, 0.0),
63    ]);
64
65    let bigger_square = Polyline::new(vec![
66        Point::new(0.0, 0.0),
67        Point::new(0.0, 10.0),
68        Point::new(10.0, 10.0),
69        Point::new(10.0, 0.0),
70    ]);
71
72    // Create the builder for the accompanying dbase (.dbf) file
73    let table_builder = TableWriterBuilder::new()
74        .add_character_field("FirstName".try_into().unwrap(), 55)
75        .add_character_field("LastName".try_into().unwrap(), 55);
76
77    {
78        let mut writer = Writer::from_path(FILE_NAME, table_builder)
79            .expect("Failed to create the shapefile writer");
80
81        let first_record = UserRecord {
82            first_name: "Yoshi".to_string(),
83            last_name: "Green".to_string(),
84        };
85
86        writer
87            .write_shape_and_record(&square, &first_record)
88            .expect("Failed to write first record");
89
90        let second_record = UserRecord {
91            first_name: "Yoshi".to_string(),
92            last_name: "Red".to_string(),
93        };
94        writer
95            .write_shape_and_record(&bigger_square, &second_record)
96            .expect("Failed to write second record");
97    }
98
99    println!("File created, you can use `cargo run --example print-content {FILE_NAME}`");
100}
Source

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

Auto Trait Implementations§

§

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

§

impl<T> !RefUnwindSafe for Writer<T>

§

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

§

impl<T> !Sync for Writer<T>

§

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

§

impl<T> !UnwindSafe for Writer<T>

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>,

Source§

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>,

Source§

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.