Skip to content

Commit

Permalink
👔 Handle local filepaths by prepending with file://
Browse files Browse the repository at this point in the history
Handle the RelativeUrlWithoutBase parsing error raised when local filepaths are given without `file://` in front. Also moved the parse url logic out of the async block.
  • Loading branch information
weiji14 committed Mar 1, 2024
1 parent 33faafb commit f9607f3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use object_store::{parse_url, ObjectStore};
use pyo3::prelude::{pyfunction, pymodule, PyModule, PyResult, Python};
use pyo3::wrap_pyfunction;
use tokio;
use url::ParseError;
use url::Url;

/// Read a GeoTIFF file from a path on disk into an ndarray
Expand All @@ -46,20 +47,28 @@ fn read_geotiff_py<'py>(
path: &str,
py: Python<'py>,
) -> PyResult<&'py PyArray<f32, Dim<[usize; 2]>>> {
// Parse URL, prepend file:// for local filepaths
let url = match Url::parse(path) {
Ok(url) => url,
Err(ParseError::RelativeUrlWithoutBase) => {
let new_path = "file://".to_owned() + path;
let url = Url::parse(new_path.as_str()).expect(&format!("Cannot parse path: {path}"));
url
}
Err(e) => Err(format!("{}", e)).unwrap(),
};
let (store, location) = parse_url(&url).expect(&format!("Cannot parse url: {url}"));

// Initialize async runtime
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;

// Get TIFF file stream asynchronously
let stream = runtime.block_on(async {
// Parse URL
let url = Url::parse(path).expect(&format!("Cannot parse path: {path}"));
let (store, location) = parse_url(&url).expect(&format!("Cannot parse url: {url}"));

// Return cursor to in-memory buffer
let result = store.get(&location).await.unwrap();
let bytes = result.bytes().await.unwrap();
// Return cursor to in-memory buffer
Cursor::new(bytes)
});

Expand Down

0 comments on commit f9607f3

Please sign in to comment.