oxigdal-node 0.1.4

Node.js bindings for OxiGDAL - Pure Rust geospatial data abstraction library
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Async operations for Node.js bindings
//!
//! This module provides Promise-based async operations for I/O and processing.

use napi::bindgen_prelude::*;
use napi::tokio;
use napi_derive::napi;
use std::path::Path;

use crate::buffer::BufferWrapper;
use crate::error::NodeError;
use crate::raster::Dataset;
use crate::vector::FeatureCollection;

/// Opens a raster dataset asynchronously
#[allow(dead_code)]
#[napi]
pub async fn open_raster_async(path: String) -> Result<Dataset> {
    tokio::task::spawn_blocking(move || Dataset::open(path))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Saves a dataset asynchronously
#[allow(dead_code)]
#[napi]
pub async fn save_raster_async(dataset: &Dataset, path: String) -> Result<()> {
    let ds_clone = dataset.clone();
    tokio::task::spawn_blocking(move || ds_clone.save(path))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Reads a GeoJSON file asynchronously
#[allow(dead_code)]
#[napi]
pub async fn read_geojson_async(path: String) -> Result<FeatureCollection> {
    tokio::task::spawn_blocking(move || {
        let content = std::fs::read_to_string(&path).map_err(|e| NodeError {
            code: "IO_ERROR".to_string(),
            message: format!("Failed to read file: {}", e),
        })?;
        FeatureCollection::from_geojson(content)
    })
    .await
    .map_err(|e| NodeError {
        code: "TASK_ERROR".to_string(),
        message: format!("Task execution failed: {}", e),
    })?
}

/// Writes a GeoJSON file asynchronously
#[allow(dead_code)]
#[napi]
pub async fn write_geojson_async(path: String, collection: &FeatureCollection) -> Result<()> {
    let content = collection.to_geojson()?;
    tokio::task::spawn_blocking(move || {
        std::fs::write(&path, content).map_err(|e| {
            NodeError {
                code: "IO_ERROR".to_string(),
                message: format!("Failed to write file: {}", e),
            }
            .into()
        })
    })
    .await
    .map_err(|e| NodeError {
        code: "TASK_ERROR".to_string(),
        message: format!("Task execution failed: {}", e),
    })?
}

/// Resamples a buffer asynchronously
#[allow(dead_code)]
#[napi]
pub async fn resample_async(
    buffer: &BufferWrapper,
    new_width: u32,
    new_height: u32,
    method: crate::algorithms::ResamplingMethod,
) -> Result<BufferWrapper> {
    let buffer_clone = buffer.clone();
    tokio::task::spawn_blocking(move || {
        crate::algorithms::resample(&buffer_clone, new_width, new_height, method)
    })
    .await
    .map_err(|e| NodeError {
        code: "TASK_ERROR".to_string(),
        message: format!("Task execution failed: {}", e),
    })?
}

/// Computes hillshade asynchronously
#[allow(dead_code)]
#[napi]
pub async fn hillshade_async(
    dem: &BufferWrapper,
    azimuth: f64,
    altitude: f64,
    z_factor: f64,
) -> Result<BufferWrapper> {
    let dem_clone = dem.clone();
    tokio::task::spawn_blocking(move || {
        crate::algorithms::hillshade(&dem_clone, azimuth, altitude, z_factor)
    })
    .await
    .map_err(|e| NodeError {
        code: "TASK_ERROR".to_string(),
        message: format!("Task execution failed: {}", e),
    })?
}

/// Computes slope asynchronously
#[allow(dead_code)]
#[napi]
pub async fn slope_async(
    dem: &BufferWrapper,
    z_factor: f64,
    as_percent: bool,
) -> Result<BufferWrapper> {
    let dem_clone = dem.clone();
    tokio::task::spawn_blocking(move || crate::algorithms::slope(&dem_clone, z_factor, as_percent))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Computes aspect asynchronously
#[allow(dead_code)]
#[napi]
pub async fn aspect_async(dem: &BufferWrapper) -> Result<BufferWrapper> {
    let dem_clone = dem.clone();
    tokio::task::spawn_blocking(move || crate::algorithms::aspect(&dem_clone))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Computes zonal statistics asynchronously
#[allow(dead_code)]
#[napi]
pub async fn zonal_stats_async(
    raster: &BufferWrapper,
    zones: &BufferWrapper,
) -> Result<Vec<crate::algorithms::ZonalStatistics>> {
    let raster_clone = raster.clone();
    let zones_clone = zones.clone();
    tokio::task::spawn_blocking(move || crate::algorithms::zonal_stats(&raster_clone, &zones_clone))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Buffer operation asynchronously
#[allow(dead_code)]
#[napi]
pub async fn buffer_async(
    geometry: &crate::vector::GeometryWrapper,
    distance: f64,
    segments: u32,
) -> Result<crate::vector::GeometryWrapper> {
    let geom_clone = geometry.clone();
    tokio::task::spawn_blocking(move || crate::algorithms::buffer(&geom_clone, distance, segments))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Area calculation asynchronously
#[allow(dead_code)]
#[napi]
pub async fn area_async(geometry: &crate::vector::GeometryWrapper, method: String) -> Result<f64> {
    let geom_clone = geometry.clone();
    tokio::task::spawn_blocking(move || crate::algorithms::area(&geom_clone, method))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Simplify operation asynchronously
#[allow(dead_code)]
#[napi]
pub async fn simplify_async(
    geometry: &crate::vector::GeometryWrapper,
    tolerance: f64,
    method: String,
) -> Result<crate::vector::GeometryWrapper> {
    let geom_clone = geometry.clone();
    tokio::task::spawn_blocking(move || crate::algorithms::simplify(&geom_clone, tolerance, method))
        .await
        .map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })?
}

/// Batch processes multiple rasters asynchronously
#[allow(dead_code)]
#[napi]
pub async fn batch_process_rasters(
    paths: Vec<String>,
    output_dir: String,
    operation: String,
) -> Result<Vec<String>> {
    let mut tasks = Vec::new();

    for path in paths {
        let output_path = format!(
            "{}/processed_{}",
            output_dir,
            Path::new(&path)
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("output.tif")
        );
        let op = operation.clone();

        let task = tokio::task::spawn_blocking(move || -> Result<String> {
            let dataset = Dataset::open(path)?;

            // Apply operation based on string
            let processed = match op.as_str() {
                "identity" => dataset,
                _ => {
                    return Err(NodeError {
                        code: "INVALID_OPERATION".to_string(),
                        message: format!("Unknown operation: {}", op),
                    }
                    .into());
                }
            };

            processed.save(output_path.clone())?;
            Ok(output_path)
        });

        tasks.push(task);
    }

    let mut results = Vec::new();
    for task in tasks {
        let result = task.await.map_err(|e| NodeError {
            code: "TASK_ERROR".to_string(),
            message: format!("Task execution failed: {}", e),
        })??;
        results.push(result);
    }

    Ok(results)
}

/// Progress callback for long-running operations
#[allow(dead_code)]
#[napi(ts_args_type = "callback: (progress: number) => void")]
pub fn set_progress_callback(_callback: Function<'_, Unknown<'_>>) -> Result<()> {
    // Store callback for use in long-running operations
    // This is a placeholder for actual implementation
    Ok(())
}

/// Cancellation token for async operations
#[napi]
pub struct CancellationToken {
    cancelled: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

#[napi]
impl CancellationToken {
    /// Creates a new cancellation token
    #[napi(constructor)]
    pub fn new() -> Self {
        Self {
            cancelled: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
        }
    }

    /// Cancels the operation
    #[napi]
    pub fn cancel(&self) {
        self.cancelled
            .store(true, std::sync::atomic::Ordering::SeqCst);
    }

    /// Checks if cancelled
    #[napi]
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Resets the token
    #[napi]
    pub fn reset(&self) {
        self.cancelled
            .store(false, std::sync::atomic::Ordering::SeqCst);
    }
}

/// Parallel processing configuration
#[allow(dead_code)]
#[napi(object)]
pub struct ParallelConfig {
    /// Number of threads to use (0 = automatic)
    pub num_threads: u32,
    /// Chunk size for parallel processing
    pub chunk_size: u32,
    /// Enable progress reporting
    pub report_progress: bool,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            num_threads: 0,
            chunk_size: 1000,
            report_progress: false,
        }
    }
}

/// Processes a large raster in parallel chunks
#[allow(dead_code)]
#[napi]
pub async fn process_raster_parallel(
    dataset: &Dataset,
    operation: String,
    config: Option<ParallelConfig>,
) -> Result<Dataset> {
    let _cfg = config.unwrap_or_default();
    let ds_clone = dataset.clone();

    tokio::task::spawn_blocking(move || -> Result<Dataset> {
        // This is a simplified implementation
        // In production, would process tiles in parallel
        match operation.as_str() {
            "identity" => Ok(ds_clone),
            _ => Err(NodeError {
                code: "INVALID_OPERATION".to_string(),
                message: format!("Unknown operation: {}", operation),
            }
            .into()),
        }
    })
    .await
    .map_err(|e| NodeError {
        code: "TASK_ERROR".to_string(),
        message: format!("Task execution failed: {}", e),
    })?
}

/// Stream processing for large datasets
#[napi]
pub struct RasterStream {
    dataset: Dataset,
    current_row: u32,
    chunk_height: u32,
}

#[napi]
impl RasterStream {
    /// Creates a new raster stream
    #[napi(constructor)]
    pub fn new(dataset: &Dataset, chunk_height: u32) -> Self {
        Self {
            dataset: dataset.clone(),
            current_row: 0,
            chunk_height,
        }
    }

    /// Reads the next chunk
    #[napi]
    pub async unsafe fn read_next_chunk(&mut self) -> Result<Option<BufferWrapper>> {
        if self.current_row >= self.dataset.height() {
            return Ok(None);
        }

        let height = self
            .chunk_height
            .min(self.dataset.height() - self.current_row);
        let chunk =
            self.dataset
                .read_window(0, 0, self.current_row, self.dataset.width(), height)?;

        self.current_row += height;
        Ok(Some(chunk))
    }

    /// Resets the stream to the beginning
    #[napi]
    pub unsafe fn reset(&mut self) {
        self.current_row = 0;
    }

    /// Gets current progress (0.0 - 1.0)
    #[napi]
    pub fn progress(&self) -> f64 {
        if self.dataset.height() == 0 {
            1.0
        } else {
            self.current_row as f64 / self.dataset.height() as f64
        }
    }
}