1use crate::byte_source::ByteSource;
4use crate::chunk::{self, DecompressedChunk};
5use crate::error::CopcError;
6use crate::header::{self, CopcHeader, CopcInfo};
7use crate::hierarchy::{HierarchyCache, HierarchyEntry};
8use crate::types::VoxelKey;
9
10pub struct CopcStreamingReader<S: ByteSource> {
15 source: S,
16 header: CopcHeader,
17 hierarchy: HierarchyCache,
18}
19
20impl<S: ByteSource> CopcStreamingReader<S> {
21 pub async fn open(source: S) -> Result<Self, CopcError> {
23 let size = source.size().await?.unwrap_or(65536);
24 let read_size = size.min(65536);
25 let data = source.read_range(0, read_size).await?;
26 let header = header::parse_header(&data)?;
27
28 let mut hierarchy = HierarchyCache::new();
29 hierarchy.load_root(&source, &header.copc_info).await?;
30
31 Ok(Self {
32 source,
33 header,
34 hierarchy,
35 })
36 }
37
38 pub fn header(&self) -> &CopcHeader {
42 &self.header
43 }
44
45 pub fn copc_info(&self) -> &CopcInfo {
47 &self.header.copc_info
48 }
49
50 pub fn evlr_offset(&self) -> u64 {
52 self.header.evlr_offset
53 }
54
55 pub fn evlr_count(&self) -> u32 {
57 self.header.evlr_count
58 }
59
60 pub fn source(&self) -> &S {
62 &self.source
63 }
64
65 pub fn get(&self, key: &VoxelKey) -> Option<&HierarchyEntry> {
69 self.hierarchy.get(key)
70 }
71
72 pub fn entries(&self) -> impl Iterator<Item = (&VoxelKey, &HierarchyEntry)> {
74 self.hierarchy.iter()
75 }
76
77 pub fn children(&self, key: &VoxelKey) -> Vec<&HierarchyEntry> {
83 key.children()
84 .iter()
85 .filter_map(|child| self.hierarchy.get(child))
86 .collect()
87 }
88
89 pub fn node_count(&self) -> usize {
91 self.hierarchy.len()
92 }
93
94 pub fn has_pending_pages(&self) -> bool {
96 self.hierarchy.has_pending_pages()
97 }
98
99 pub async fn load_pending_pages(&mut self) -> Result<(), CopcError> {
103 self.hierarchy.load_pending_pages(&self.source).await
104 }
105
106 pub async fn load_hierarchy_for_bounds(
111 &mut self,
112 bounds: &crate::types::Aabb,
113 ) -> Result<(), CopcError> {
114 let root_bounds = self.header.copc_info.root_bounds();
115 self.hierarchy
116 .load_pages_for_bounds(&self.source, bounds, &root_bounds)
117 .await
118 }
119
120 pub async fn load_hierarchy_for_bounds_to_level(
131 &mut self,
132 bounds: &crate::types::Aabb,
133 max_level: i32,
134 ) -> Result<(), CopcError> {
135 let root_bounds = self.header.copc_info.root_bounds();
136 self.hierarchy
137 .load_pages_for_bounds_to_level(&self.source, bounds, &root_bounds, max_level)
138 .await
139 }
140
141 pub async fn load_all_hierarchy(&mut self) -> Result<(), CopcError> {
143 self.hierarchy
144 .load_all(&self.source, &self.header.copc_info)
145 .await
146 }
147
148 pub async fn fetch_chunk(&self, key: &VoxelKey) -> Result<DecompressedChunk, CopcError> {
152 let entry = self
153 .hierarchy
154 .get(key)
155 .ok_or(CopcError::NodeNotFound(*key))?;
156 let point_record_length = self.header.las_header.point_format().len()
157 + self.header.las_header.point_format().extra_bytes;
158 chunk::fetch_and_decompress(
159 &self.source,
160 entry,
161 &self.header.laz_vlr,
162 point_record_length,
163 )
164 .await
165 }
166
167 pub fn read_points(&self, chunk: &DecompressedChunk) -> Result<Vec<las::Point>, CopcError> {
169 chunk::read_points(chunk, &self.header.las_header)
170 }
171
172 pub fn read_points_range(
178 &self,
179 chunk: &DecompressedChunk,
180 range: std::ops::Range<u32>,
181 ) -> Result<Vec<las::Point>, CopcError> {
182 chunk::read_points_range(chunk, &self.header.las_header, range)
183 }
184
185 pub fn read_points_in_bounds(
187 &self,
188 chunk: &DecompressedChunk,
189 bounds: &crate::types::Aabb,
190 ) -> Result<Vec<las::Point>, CopcError> {
191 let points = chunk::read_points(chunk, &self.header.las_header)?;
192 Ok(filter_points_by_bounds(points, bounds))
193 }
194
195 pub fn read_points_range_in_bounds(
201 &self,
202 chunk: &DecompressedChunk,
203 range: std::ops::Range<u32>,
204 bounds: &crate::types::Aabb,
205 ) -> Result<Vec<las::Point>, CopcError> {
206 let points = chunk::read_points_range(chunk, &self.header.las_header, range)?;
207 Ok(filter_points_by_bounds(points, bounds))
208 }
209
210 pub async fn query_points(
222 &mut self,
223 bounds: &crate::types::Aabb,
224 ) -> Result<Vec<las::Point>, CopcError> {
225 self.load_hierarchy_for_bounds(bounds).await?;
226 let root_bounds = self.header.copc_info.root_bounds();
227
228 let keys: Vec<VoxelKey> = self
229 .hierarchy
230 .iter()
231 .filter(|(k, e)| e.point_count > 0 && k.bounds(&root_bounds).intersects(bounds))
232 .map(|(k, _)| *k)
233 .collect();
234
235 let mut all_points = Vec::new();
236 for key in keys {
237 let chunk = self.fetch_chunk(&key).await?;
238 let points = self.read_points_in_bounds(&chunk, bounds)?;
239 all_points.extend(points);
240 }
241 Ok(all_points)
242 }
243
244 pub async fn query_points_to_level(
254 &mut self,
255 bounds: &crate::types::Aabb,
256 max_level: i32,
257 ) -> Result<Vec<las::Point>, CopcError> {
258 self.load_hierarchy_for_bounds_to_level(bounds, max_level)
259 .await?;
260 let root_bounds = self.header.copc_info.root_bounds();
261
262 let keys: Vec<VoxelKey> = self
263 .hierarchy
264 .iter()
265 .filter(|(k, e)| {
266 e.point_count > 0
267 && k.level <= max_level
268 && k.bounds(&root_bounds).intersects(bounds)
269 })
270 .map(|(k, _)| *k)
271 .collect();
272
273 let mut all_points = Vec::new();
274 for key in keys {
275 let chunk = self.fetch_chunk(&key).await?;
276 let points = self.read_points_in_bounds(&chunk, bounds)?;
277 all_points.extend(points);
278 }
279 Ok(all_points)
280 }
281}
282
283pub fn filter_points_by_bounds(
285 points: Vec<las::Point>,
286 bounds: &crate::types::Aabb,
287) -> Vec<las::Point> {
288 points
289 .into_iter()
290 .filter(|p| {
291 p.x >= bounds.min[0]
292 && p.x <= bounds.max[0]
293 && p.y >= bounds.min[1]
294 && p.y <= bounds.max[1]
295 && p.z >= bounds.min[2]
296 && p.z <= bounds.max[2]
297 })
298 .collect()
299}