forked from georust/geozero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geozero-api.rs
105 lines (90 loc) · 2.86 KB
/
geozero-api.rs
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
use flatgeobuf::{FallibleStreamingIterator as _, FeatureProperties as _, FgbReader, GeometryType};
use geozero::error::Result as GeozeroResult;
use geozero::{ColumnValue, CoordDimensions, GeomProcessor, PropertyProcessor};
use seek_bufread::BufReader;
use std::fs::File;
struct VertexCounter(u64);
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
impl GeomProcessor for VertexCounter {
fn xy(&mut self, _x: f64, _y: f64, _idx: usize) -> GeozeroResult<()> {
self.0 += 1;
Ok(())
}
}
#[test]
fn vertex_counter() -> Result<()> {
let mut filein = BufReader::new(File::open("tests/data/countries.fgb")?);
let mut fgb = FgbReader::open(&mut filein)?.select_bbox(8.8, 47.2, 9.5, 55.3)?;
let feature = fgb.next()?.unwrap();
let geometry = feature.geometry().unwrap();
let mut vertex_counter = VertexCounter(0);
geometry.process(&mut vertex_counter, GeometryType::MultiPolygon)?;
assert_eq!(vertex_counter.0, 24);
Ok(())
}
struct MaxHeightFinder(f64);
impl GeomProcessor for MaxHeightFinder {
fn dimensions(&self) -> CoordDimensions {
CoordDimensions {
z: true,
m: false,
t: false,
tm: false,
}
}
fn coordinate(
&mut self,
_x: f64,
_y: f64,
z: Option<f64>,
_m: Option<f64>,
_t: Option<f64>,
_tm: Option<u64>,
_idx: usize,
) -> GeozeroResult<()> {
if let Some(z) = z {
if z > self.0 {
self.0 = z
}
}
Ok(())
}
}
#[test]
#[ignore]
fn max_height_finder() -> Result<()> {
let mut filein = BufReader::new(File::open(
"tests/data/geoz_lod1_gebaeude_max_3d_extract.fgb",
)?);
let mut fgb = FgbReader::open(&mut filein)?.select_all()?;
let mut max_finder = MaxHeightFinder(0.0);
while let Some(feature) = fgb.next()? {
let geometry = feature.geometry().unwrap();
geometry.process(&mut max_finder, GeometryType::MultiPolygon)?;
}
assert_eq!(max_finder.0, 457.1);
Ok(())
}
struct FeatureFinder;
impl PropertyProcessor for FeatureFinder {
fn property(&mut self, i: usize, _name: &str, v: &ColumnValue) -> GeozeroResult<bool> {
Ok(i == 0 && v == &ColumnValue::String("DNK"))
}
}
#[test]
fn feature_finder() -> Result<()> {
let mut filein = BufReader::new(File::open("tests/data/countries.fgb")?);
let mut fgb = FgbReader::open(&mut filein)?.select_all()?;
let mut finder = FeatureFinder {};
while let Some(feature) = fgb.next()? {
let found = feature.process_properties(&mut finder);
if found.is_err() || found.unwrap() {
break;
}
}
let feature = fgb.cur_feature();
let props = feature.properties()?;
assert_eq!(props["id"], "DNK".to_string());
assert_eq!(props["name"], "Denmark".to_string());
Ok(())
}