-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_tsunami_shapefile.rs
More file actions
186 lines (155 loc) · 5.01 KB
/
parse_tsunami_shapefile.rs
File metadata and controls
186 lines (155 loc) · 5.01 KB
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
#![allow(clippy::type_complexity)]
use std::collections::HashMap;
use std::path::Path;
use shapefile::dbase::{FieldValue, Record};
use shapefile::{Shape, ShapeReader};
use crate::math::*;
use renderer_types::codes;
struct TsunamiAreaCodeBuffer {
area_code_to_internal_code: HashMap<u32, u16>,
}
impl TsunamiAreaCodeBuffer {
fn new() -> Self {
Self {
area_code_to_internal_code: Default::default(),
}
}
fn insert(&mut self, area_code: u32) -> u16 {
match self.area_code_to_internal_code.get(&area_code) {
Some(index) => *index,
None => {
let index = self.area_code_to_internal_code.len();
self.area_code_to_internal_code
.insert(area_code, index as u16);
index as u16
}
}
}
fn into_buffer(self) -> HashMap<u32, u16> {
self.area_code_to_internal_code
}
}
struct VertexBuffer {
buffer: Vec<(Of32, Of32, u16)>,
dict: HashMap<(Of32, Of32, u16), usize>,
}
impl VertexBuffer {
fn new() -> Self {
Self {
buffer: Default::default(),
dict: Default::default(),
}
}
fn insert(&mut self, v: (Of32, Of32, u16)) -> usize {
match self.dict.get(&v) {
Some(index) => *index,
None => {
self.buffer.push(v);
let index = self.buffer.len() - 1;
self.dict.insert(v, index);
index
}
}
}
fn into_buffer(self) -> Vec<(f32, f32, u16)> {
self.buffer
.into_iter()
.map(|(x, y, code)| (x.0, y.0, code))
.collect()
}
}
struct AreaLines {
lines: Vec<Line>,
津波予報区: codes::津波予報区,
}
impl AreaLines {
fn try_new(shape: Shape, record: Record) -> Option<Self> {
let Shape::Polyline(polyline) = shape else {
return None;
};
let 津波予報区: codes::津波予報区 = match record.get("code").unwrap() {
FieldValue::Character(Some(c)) => match c.parse() {
Ok(c) => codes::津波予報区(c),
Err(_) => panic!("Failed to parse code"),
},
FieldValue::Character(None) => panic!("UNNUMBERED_AREA DETECTED!"),
_ => panic!("Failed to get code"),
};
if 津波予報区 == codes::津波予報区::帰属未定 {
return None;
}
let lines: Vec<_> = polyline
.parts()
.iter()
.map(|line| Line::from(line.clone()))
.collect();
Some(Self {
lines,
津波予報区,
})
}
}
struct Shapefile {
entries: Vec<AreaLines>,
}
impl Shapefile {
fn new<P: AsRef<Path>>(shp_file: P, dbf_file: P) -> Self {
let shp_file = std::fs::File::open(shp_file);
let dbf_file = std::fs::File::open(dbf_file);
let (Ok(shp_file), Ok(dbf_file)) = (shp_file, dbf_file) else {
panic!(
r#"EEWBot Renderer requirements is not satisfied.
Simplified shape files are not found.
- assets/shapefile/tsunami_forecast/tsunami_forecast_simplified.shp
- assets/shapefile/tsunami_forecast/tsunami_forecast_simplified.dbf
Please follow:
https://github.com/EEWBot/eew-renderer/wiki#shapefile-%E5%85%A5%E6%89%8B%E5%85%88"#
)
};
let shape_reader = ShapeReader::new(shp_file).unwrap();
let dbf_reader = shapefile::dbase::Reader::new(dbf_file).unwrap();
let mut reader = shapefile::reader::Reader::new(shape_reader, dbf_reader);
let entries = reader
.iter_shapes_and_records()
.flatten()
.flat_map(|shape_record| AreaLines::try_new(shape_record.0, shape_record.1))
.collect();
Self { entries }
}
}
pub fn read() -> (
Vec<(f32, f32, u16)>, // vertices
Vec<u32>, // indices
HashMap<u32, u16>, // tsunami_area_code_to_internal_code
) {
let shapefile = Shapefile::new(
"../assets/shapefile/tsunami_forecast/tsunami_forecast_simplified.shp",
"../assets/shapefile/tsunami_forecast/tsunami_forecast_simplified.dbf",
);
let mut vertex_buffer = VertexBuffer::new();
let mut lines = Vec::new();
let mut tsunami_area_code_buffer = TsunamiAreaCodeBuffer::new();
for e in shapefile.entries {
for line in e.lines {
let line: Vec<u32> = line
.vertices
.into_iter()
.map(|v| {
vertex_buffer.insert((
Of32::from(v.longitude),
Of32::from(v.latitude),
tsunami_area_code_buffer.insert(e.津波予報区.0),
)) as u32
})
.collect();
lines.extend_from_slice(&line);
// append NUL vertex
lines.push(0);
}
}
(
vertex_buffer.into_buffer(),
lines,
tsunami_area_code_buffer.into_buffer(),
)
}