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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use crate::feature_generated::*;
use crate::header_generated::*;
use byteorder::{ByteOrder, LittleEndian};
use geozero::error::{GeozeroError, Result};
use geozero::GeozeroGeometry;
use geozero::{ColumnValue, GeomProcessor, PropertyProcessor};
use std::mem::size_of;
use std::str;
/// Access to the *current* feature during iteration.
pub struct FgbFeature {
pub(crate) header_buf: Vec<u8>, // Using type Header<'a> instead of Vec would require adding a lifetime to FgbFeature
pub(crate) feature_buf: Vec<u8>,
}
impl FgbFeature {
pub(crate) fn header(&self) -> Header<'_> {
// SAFETY: verification is done before creating instance
unsafe { size_prefixed_root_as_header_unchecked(&self.header_buf) }
}
/// Flatbuffers feature access
pub fn fbs_feature(&self) -> Feature<'_> {
// SAFETY: verification is done before creating instance
unsafe { size_prefixed_root_as_feature_unchecked(&self.feature_buf) }
}
/// Flatbuffers geometry access
pub fn geometry(&self) -> Option<Geometry<'_>> {
self.fbs_feature().geometry()
}
fn dimension(&self) -> geo_traits::Dimensions {
match (self.header().has_z(), self.header().has_m()) {
(true, true) => geo_traits::Dimensions::Xyzm,
(true, false) => geo_traits::Dimensions::Xyz,
(false, true) => geo_traits::Dimensions::Xym,
(false, false) => geo_traits::Dimensions::Xy,
}
}
/// Access the underlying geometry, returning an object that implements
/// [`geo_traits::GeometryTrait`].
///
/// This allows for random-access zero-copy vector data interoperability, even with Z, M, and
/// ZM geometries that `geo_types` does not currently support.
///
/// ### Notes:
///
/// - Any `T` values are currently ignored.
/// - This will error on curve geometries since they are not among the core geometry types
/// supported by [`geo_traits`].
pub fn geometry_trait(
&self,
) -> std::result::Result<Option<impl geo_traits::GeometryTrait<T = f64> + use<'_>>, crate::Error>
{
if let Some(geom) = self.geometry() {
let dim = self.dimension();
let result = match self.header().geometry_type() {
GeometryType::Point => crate::geo_trait_impl::Geometry::Point(
crate::geo_trait_impl::Point::new(geom, dim),
),
GeometryType::LineString => crate::geo_trait_impl::Geometry::LineString(
crate::geo_trait_impl::LineString::new(geom, dim),
),
GeometryType::Polygon => crate::geo_trait_impl::Geometry::Polygon(
crate::geo_trait_impl::Polygon::new(geom, dim),
),
GeometryType::MultiPoint => crate::geo_trait_impl::Geometry::MultiPoint(
crate::geo_trait_impl::MultiPoint::new(geom, dim),
),
GeometryType::MultiLineString => crate::geo_trait_impl::Geometry::MultiLineString(
crate::geo_trait_impl::MultiLineString::new(geom, dim),
),
GeometryType::MultiPolygon => crate::geo_trait_impl::Geometry::MultiPolygon(
crate::geo_trait_impl::MultiPolygon::new(geom, dim),
),
GeometryType::Unknown => crate::geo_trait_impl::Geometry::new(geom, dim),
GeometryType::GeometryCollection => {
crate::geo_trait_impl::Geometry::GeometryCollection(
crate::geo_trait_impl::GeometryCollection::new(geom, dim),
)
}
geom_type => {
return Err(crate::Error::UnsupportedGeometryType(format!(
"Unsupported geometry type in geo-traits: {geom_type:?}",
)))
}
};
Ok(Some(result))
} else {
Ok(None)
}
}
}
impl geozero::FeatureAccess for FgbFeature {}
impl GeozeroGeometry for FgbFeature {
fn process_geom<P: GeomProcessor>(&self, processor: &mut P) -> Result<()> {
let geometry = self
.fbs_feature()
.geometry()
.ok_or(GeozeroError::GeometryFormat)?;
let geometry_type = self.header().geometry_type();
geometry.process(processor, geometry_type)
}
}
impl geozero::FeatureProperties for FgbFeature {
/// Process feature properties.
fn process_properties<P: PropertyProcessor>(&self, reader: &mut P) -> Result<bool> {
if self.header().columns().is_none() {
return Ok(false);
}
let columns_meta = self
.header()
.columns()
.ok_or(GeozeroError::GeometryFormat)?;
let mut finish = false;
if let Some(properties) = self.fbs_feature().properties() {
let mut offset = 0;
let bytes = properties.bytes();
while offset + 1 < properties.len() && !finish {
// NOTE: it should be offset < properties.len(), but there is data with a
// trailing byte in the last column of type Binary
let column_idx = LittleEndian::read_u16(&bytes[offset..offset + 2]) as usize;
offset += size_of::<u16>();
if column_idx >= columns_meta.len() {
// NOTE: reading also fails if column._type is different from effective entry
return Err(GeozeroError::GeometryFormat);
}
let column = &columns_meta.get(column_idx);
match column.type_() {
ColumnType::Int => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Int(LittleEndian::read_i32(&bytes[offset..offset + 4])),
)?;
offset += size_of::<i32>();
}
ColumnType::Long => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Long(LittleEndian::read_i64(&bytes[offset..offset + 8])),
)?;
offset += size_of::<i64>();
}
ColumnType::ULong => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::ULong(LittleEndian::read_u64(&bytes[offset..offset + 8])),
)?;
offset += size_of::<u64>();
}
ColumnType::Double => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Double(LittleEndian::read_f64(
&bytes[offset..offset + 8],
)),
)?;
offset += size_of::<f64>();
}
ColumnType::String => {
let len = LittleEndian::read_u32(&bytes[offset..offset + 4]) as usize;
offset += size_of::<u32>();
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::String(
// unsafe variant without UTF-8 checking would be faster...
str::from_utf8(&bytes[offset..offset + len]).map_err(|_| {
GeozeroError::Property("Invalid UTF-8 encoding".to_string())
})?,
),
)?;
offset += len;
}
ColumnType::Byte => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Byte(bytes[offset] as i8),
)?;
offset += size_of::<i8>();
}
ColumnType::UByte => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::UByte(bytes[offset]),
)?;
offset += size_of::<u8>();
}
ColumnType::Bool => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Bool(bytes[offset] != 0),
)?;
offset += size_of::<u8>();
}
ColumnType::Short => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Short(LittleEndian::read_i16(&bytes[offset..offset + 2])),
)?;
offset += size_of::<i16>();
}
ColumnType::UShort => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::UShort(LittleEndian::read_u16(
&bytes[offset..offset + 2],
)),
)?;
offset += size_of::<u16>();
}
ColumnType::UInt => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::UInt(LittleEndian::read_u32(&bytes[offset..offset + 4])),
)?;
offset += size_of::<u32>();
}
ColumnType::Float => {
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Float(LittleEndian::read_f32(&bytes[offset..offset + 4])),
)?;
offset += size_of::<f32>();
}
ColumnType::Json => {
let len = LittleEndian::read_u32(&bytes[offset..offset + 4]) as usize;
offset += size_of::<u32>();
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Json(
// JSON may be represented using UTF-8, UTF-16, or UTF-32. The default encoding is UTF-8.
str::from_utf8(&bytes[offset..offset + len]).map_err(|_| {
GeozeroError::Property("Invalid UTF-8 encoding".to_string())
})?,
),
)?;
offset += len;
}
ColumnType::DateTime => {
let len = LittleEndian::read_u32(&bytes[offset..offset + 4]) as usize;
offset += size_of::<u32>();
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::DateTime(
// unsafe variant without UTF-8 checking would be faster...
str::from_utf8(&bytes[offset..offset + len]).map_err(|_| {
GeozeroError::Property("Invalid UTF-8 encoding".to_string())
})?,
),
)?;
offset += len;
}
ColumnType::Binary => {
let len = LittleEndian::read_u32(&bytes[offset..offset + 4]) as usize;
offset += size_of::<u32>();
finish = reader.property(
column_idx,
column.name(),
&ColumnValue::Binary(&bytes[offset..offset + len]),
)?;
offset += len;
}
ColumnType(_) => {}
}
}
}
Ok(finish)
}
}