-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathflatgeobuf.rs
More file actions
50 lines (40 loc) · 1.67 KB
/
flatgeobuf.rs
File metadata and controls
50 lines (40 loc) · 1.67 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
use datafusion::catalog::TableProvider;
use datafusion::datasource::listing::{
ListingOptions, ListingTable, ListingTableConfig, ListingTableUrl,
};
use datafusion::prelude::SessionContext;
use datafusion_ffi::table_provider::FFI_TableProvider;
use geodatafusion_flatgeobuf::FlatGeobufFormat;
use pyo3::prelude::*;
use pyo3::types::PyCapsule;
use pyo3::{Bound, PyResult, Python, pyclass, pymethods};
use pyo3_async_runtimes::tokio::get_runtime;
use std::sync::Arc;
#[pyfunction]
pub(crate) fn new_flatgeobuf(path: &str) -> PyFlatGeobufTableProvider {
let format = Arc::new(FlatGeobufFormat::default());
let options = ListingOptions::new(format).with_file_extension(".fgb");
let table_path = ListingTableUrl::parse(path).unwrap();
let state = SessionContext::new().state();
let runtime = get_runtime();
let inferred_schema =
runtime.block_on(async { options.infer_schema(&state, &table_path).await.unwrap() });
let config = ListingTableConfig::new(table_path)
.with_listing_options(options)
.with_schema(inferred_schema);
let table = ListingTable::try_new(config).unwrap();
PyFlatGeobufTableProvider(Arc::new(table))
}
#[pyclass(module = "geodatafusion", name = "FlatGeobufTableProvider", frozen)]
pub(crate) struct PyFlatGeobufTableProvider(Arc<dyn TableProvider + Send>);
#[pymethods]
impl PyFlatGeobufTableProvider {
pub fn __datafusion_table_provider__<'py>(
&self,
py: Python<'py>,
) -> PyResult<Bound<'py, PyCapsule>> {
let name = cr"datafusion_table_provider".into();
let provider = FFI_TableProvider::new(self.0.clone(), false, None);
PyCapsule::new(py, provider, Some(name))
}
}