Skip to content

Commit

Permalink
Deduplicate CRS struct in Python bindings (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Dec 6, 2024
1 parent 07e0c03 commit 11d8bde
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 73 deletions.
1 change: 1 addition & 0 deletions python/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ object_store = "0.11"
parquet = "53"
pyo3 = { version = "0.23.0", features = ["hashbrown", "serde", "anyhow"] }
pyo3-arrow = "0.6"
pyo3-geoarrow = { path = "./pyo3-geoarrow" }
serde_json = "1"
thiserror = "1"
9 changes: 2 additions & 7 deletions python/geoarrow-core/src/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ use geoarrow::array::{
PointArray, PolygonArray,
};
use pyo3::prelude::*;
use pyo3_geoarrow::{PyCoordBuffer, PyGeoArrowResult, PyNativeArray, PyOffsetBuffer};

use crate::crs::CRS;
use pyo3_geoarrow::{PyCoordBuffer, PyGeoArrowResult, PyNativeArray, PyOffsetBuffer, CRS};

fn create_array_metadata(crs: Option<CRS>) -> Arc<ArrayMetadata> {
Arc::new(ArrayMetadata {
crs: crs.map(|c| c.into_inner()),
..Default::default()
})
Arc::new(crs.map(|inner| inner.into_inner()).unwrap_or_default())
}

#[pyfunction]
Expand Down
53 changes: 0 additions & 53 deletions python/geoarrow-core/src/crs.rs

This file was deleted.

8 changes: 2 additions & 6 deletions python/geoarrow-core/src/interop/shapely/from_shapely.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;
use crate::constructors::{
linestrings, multilinestrings, multipoints, multipolygons, points, polygons,
};
use crate::crs::CRS;
use crate::ffi::to_python::native_array_to_pyobject;
use crate::interop::shapely::utils::import_shapely;
use arrow_array::builder::BinaryBuilder;
Expand All @@ -15,7 +14,7 @@ use pyo3::prelude::*;
use pyo3::pybacked::PyBackedBytes;
use pyo3::types::{PyDict, PyString, PyTuple};
use pyo3::PyAny;
use pyo3_geoarrow::PyGeoArrowResult;
use pyo3_geoarrow::{PyGeoArrowResult, CRS};

/// Check that the value of the GeometryType enum returned from shapely.to_ragged_array matches the
/// expected variant for this geometry array.
Expand Down Expand Up @@ -155,10 +154,7 @@ pub fn from_shapely(

native_array_to_pyobject(py, arr)
} else {
let metadata = Arc::new(ArrayMetadata {
crs: crs.map(|c| c.into_inner()),
..Default::default()
});
let metadata = Arc::new(crs.map(|inner| inner.into_inner()).unwrap_or_default());

// TODO: support 3d WKB
let wkb_arr = make_wkb_arr(py, input, metadata)?;
Expand Down
1 change: 0 additions & 1 deletion python/geoarrow-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyTuple;
mod constructors;
pub(crate) mod crs;
pub mod ffi;
pub mod interop;
pub mod table;
Expand Down
1 change: 1 addition & 0 deletions python/geoarrow-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ object_store = { workspace = true, features = [
parquet = { workspace = true }
pyo3 = { workspace = true }
pyo3-arrow = { workspace = true }
pyo3-geoarrow = { workspace = true }
pyo3-async-runtimes = { version = "0.23", features = [
"tokio-runtime",
], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion python/geoarrow-io/src/io/flatgeobuf/sync.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::crs::PyprojCRSTransform;
use crate::error::{PyGeoArrowError, PyGeoArrowResult};
use crate::io::input::sync::FileWriter;
use crate::io::input::{construct_reader, AnyFileReader};
Expand All @@ -9,6 +8,7 @@ use geoarrow::io::flatgeobuf::{
};
use pyo3::prelude::*;
use pyo3_arrow::input::AnyRecordBatch;
use pyo3_geoarrow::PyprojCRSTransform;

#[pyfunction]
#[pyo3(signature = (file, *, store=None, batch_size=65536, bbox=None))]
Expand Down
10 changes: 7 additions & 3 deletions python/geoarrow-io/src/io/parquet/async.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::sync::Arc;

use crate::crs::CRS;
use crate::error::{PyGeoArrowError, PyGeoArrowResult};
use crate::io::input::{construct_reader, AnyFileReader};
use crate::io::parquet::options::create_options;
Expand All @@ -25,6 +24,7 @@ use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3_arrow::{PyArray, PySchema};
use pyo3_async_runtimes::tokio::future_into_py;
use pyo3_geoarrow::CRS;
use pyo3_object_store::PyObjectStore;
use pythonize::depythonize;

Expand Down Expand Up @@ -129,7 +129,9 @@ impl ParquetFile {
#[pyo3(signature = (column_name=None))]
fn crs(&self, py: Python, column_name: Option<&str>) -> PyGeoArrowResult<PyObject> {
if let Some(crs) = self.geoparquet_meta.crs(column_name)? {
CRS::from_projjson(crs.clone()).to_pyproj(py)
Ok(CRS::from_projjson(crs.clone())
.to_pyproj(py)
.map_err(PyErr::from)?)
} else {
Ok(py.None())
}
Expand Down Expand Up @@ -386,7 +388,9 @@ impl ParquetDataset {
#[pyo3(signature = (column_name=None))]
fn crs(&self, py: Python, column_name: Option<&str>) -> PyGeoArrowResult<PyObject> {
if let Some(crs) = self.meta.crs(column_name)? {
CRS::from_projjson(crs.clone()).to_pyproj(py)
Ok(CRS::from_projjson(crs.clone())
.to_pyproj(py)
.map_err(PyErr::from)?)
} else {
Ok(py.None())
}
Expand Down
2 changes: 1 addition & 1 deletion python/geoarrow-io/src/io/parquet/sync.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs::File;
use std::sync::Mutex;

use crate::crs::PyprojCRSTransform;
use crate::error::{PyGeoArrowError, PyGeoArrowResult};
use crate::io::input::sync::{FileReader, FileWriter};
use crate::io::input::{construct_reader, AnyFileReader};
Expand All @@ -19,6 +18,7 @@ use geoarrow::io::parquet::{
GeoParquetWriterOptions,
};
use pyo3_arrow::input::AnyRecordBatch;
use pyo3_geoarrow::PyprojCRSTransform;

#[pyfunction]
#[pyo3(signature = (path, *, store=None, batch_size=None))]
Expand Down
1 change: 0 additions & 1 deletion python/geoarrow-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use pyo3::exceptions::PyRuntimeWarning;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyTuple;
pub(crate) mod crs;
pub mod error;
// pub mod ffi;
pub mod io;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use serde_json::Value;

use crate::error::PyGeoArrowResult;

/// A wrapper around the CRS functionality contained within [ArrayMetadata] to integrate with
/// `pyproj` Python APIs.
#[allow(clippy::upper_case_acronyms)]
#[derive(Clone, Debug)]
pub struct CRS(ArrayMetadata);
Expand Down Expand Up @@ -133,6 +135,12 @@ impl PyprojCRSTransform {
}
}

impl Default for PyprojCRSTransform {
fn default() -> Self {
Self::new()
}
}

impl CRSTransform for PyprojCRSTransform {
fn _convert_to_projjson(
&self,
Expand Down
2 changes: 2 additions & 0 deletions python/pyo3-geoarrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod array;
mod chunked_array;
mod coord_buffer;
mod coord_type;
mod crs;
mod data_type;
mod dimension;
mod error;
Expand All @@ -13,6 +14,7 @@ pub use array::{PyNativeArray, PySerializedArray};
pub use chunked_array::PyChunkedNativeArray;
pub use coord_buffer::PyCoordBuffer;
pub use coord_type::PyCoordType;
pub use crs::{PyprojCRSTransform, CRS};
pub use data_type::{PyNativeType, PySerializedType};
pub use dimension::PyDimension;
pub use error::{PyGeoArrowError, PyGeoArrowResult};
Expand Down

0 comments on commit 11d8bde

Please sign in to comment.