1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
extern crate byteorder;
pub extern crate dbase;
pub mod header;
pub mod reader;
pub mod record;
pub mod writer;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::convert::From;
use std::fmt;
use std::io::{Read, Write};
pub use reader::{read, read_as, read_shapes, read_shapes_as, Reader, ShapeReader};
pub use record::Multipatch;
pub use record::{convert_shapes_to_vec_of, HasShapeType, ReadableShape};
pub use record::{Multipoint, MultipointM, MultipointZ};
pub use record::{Patch, Shape, NO_DATA};
pub use record::{Point, PointM, PointZ};
pub use record::{Polygon, PolygonM, PolygonRing, PolygonZ};
pub use record::{Polyline, PolylineM, PolylineZ};
pub use writer::{ShapeWriter, Writer};
extern crate core;
#[cfg(feature = "geo-types")]
extern crate geo_types;
#[derive(Debug)]
pub enum Error {
IoError(std::io::Error),
InvalidFileCode(i32),
InvalidShapeType(i32),
InvalidPatchType(i32),
MismatchShapeType {
requested: ShapeType,
actual: ShapeType,
},
InvalidShapeRecordSize,
DbaseError(dbase::Error),
MissingDbf,
MissingIndexFile,
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Error {
Error::IoError(error)
}
}
impl From<dbase::Error> for Error {
fn from(e: dbase::Error) -> Error {
Error::DbaseError(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::IoError(e) => write!(f, "{}", e),
Error::InvalidFileCode(code) => write!(
f,
"The file code ' {} ' is invalid, is this a Shapefile ?",
code
),
Error::InvalidShapeType(code) => write!(
f,
"The code ' {} ' does not correspond to any of the ShapeType code defined by ESRI",
code
),
Error::MismatchShapeType { requested, actual } => write!(
f,
"The requested type: '{}' does not correspond to the actual shape type: '{}'",
requested, actual
),
e => write!(f, "{:?}", e),
}
}
}
impl std::error::Error for Error {}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum ShapeType {
NullShape = 0,
Point = 1,
Polyline = 3,
Polygon = 5,
Multipoint = 8,
PointZ = 11,
PolylineZ = 13,
PolygonZ = 15,
MultipointZ = 18,
PointM = 21,
PolylineM = 23,
PolygonM = 25,
MultipointM = 28,
Multipatch = 31,
}
impl ShapeType {
pub(crate) fn read_from<T: Read>(source: &mut T) -> Result<ShapeType, Error> {
let code = source.read_i32::<LittleEndian>()?;
Self::from(code).ok_or_else(|| Error::InvalidShapeType(code))
}
pub(crate) fn write_to<T: Write>(self, dest: &mut T) -> Result<(), std::io::Error> {
dest.write_i32::<LittleEndian>(self as i32)?;
Ok(())
}
pub fn from(code: i32) -> Option<ShapeType> {
match code {
0 => Some(ShapeType::NullShape),
1 => Some(ShapeType::Point),
3 => Some(ShapeType::Polyline),
5 => Some(ShapeType::Polygon),
8 => Some(ShapeType::Multipoint),
11 => Some(ShapeType::PointZ),
13 => Some(ShapeType::PolylineZ),
15 => Some(ShapeType::PolygonZ),
18 => Some(ShapeType::MultipointZ),
21 => Some(ShapeType::PointM),
23 => Some(ShapeType::PolylineM),
25 => Some(ShapeType::PolygonM),
28 => Some(ShapeType::MultipointM),
31 => Some(ShapeType::Multipatch),
_ => None,
}
}
pub fn has_z(self) -> bool {
match self {
ShapeType::PointZ
| ShapeType::PolylineZ
| ShapeType::PolygonZ
| ShapeType::MultipointZ
| ShapeType::Multipatch => true,
_ => false,
}
}
pub fn has_m(self) -> bool {
match self {
ShapeType::PointZ
| ShapeType::PolylineZ
| ShapeType::PolygonZ
| ShapeType::MultipointZ
| ShapeType::PointM
| ShapeType::PolylineM
| ShapeType::PolygonM
| ShapeType::MultipointM => true,
_ => false,
}
}
pub fn is_multipart(self) -> bool {
match self {
ShapeType::Point
| ShapeType::PointM
| ShapeType::PointZ
| ShapeType::Multipoint
| ShapeType::MultipointM
| ShapeType::MultipointZ => false,
_ => true,
}
}
}
impl fmt::Display for ShapeType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ShapeType::NullShape => write!(f, "NullShape"),
ShapeType::Point => write!(f, "Point"),
ShapeType::Polyline => write!(f, "Polyline"),
ShapeType::Polygon => write!(f, "Polygon"),
ShapeType::Multipoint => write!(f, "Multipoint"),
ShapeType::PointZ => write!(f, "PointZ"),
ShapeType::PolylineZ => write!(f, "PolylineZ"),
ShapeType::PolygonZ => write!(f, "PolygonZ"),
ShapeType::MultipointZ => write!(f, "MultipointZ"),
ShapeType::PointM => write!(f, "PointM"),
ShapeType::PolylineM => write!(f, "PolylineM"),
ShapeType::PolygonM => write!(f, "PolygonM"),
ShapeType::MultipointM => write!(f, "MultipointM"),
ShapeType::Multipatch => write!(f, "Multipatch"),
}
}
}