Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read_shapefile to python io docs #855

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/docs/api/io/shapefile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Shapefile

Read Shapefile files.

::: geoarrow.rust.io.read_shapefile
9 changes: 9 additions & 0 deletions python/geoarrow-io/python/geoarrow/rust/io/_io.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,15 @@ def read_shapefile(
) -> Table:
"""
Read a Shapefile into an Arrow Table.

The returned Arrow table will have geometry information in native GeoArrow encoding.

!!! note
Coordinate Reference System information is not currently read from the Shapefile.

Args:
shp_file: the path to the `.shp` file or the `.shp` file as a Python file object in binary read mode.
dbf_file: the path to the `.dbf` file or the `.dbf` file as a Python file object in binary read mode.
"""

def write_csv(table: ArrowStreamExportable, file: str | Path | BinaryIO) -> None:
Expand Down
54 changes: 21 additions & 33 deletions python/geoarrow-io/src/io/postgis.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,43 @@
use crate::error::{PyGeoArrowError, PyGeoArrowResult};
use crate::error::PyGeoArrowError;
use crate::util::table_to_pytable;
use geoarrow::error::GeoArrowError;
use geoarrow::io::postgis::read_postgis as _read_postgis;
use pyo3::prelude::*;
use pyo3_arrow::PyTable;
use pyo3_async_runtimes::tokio::future_into_py;
use sqlx::postgres::PgPoolOptions;

#[pyfunction]
pub fn read_postgis(
py: Python,
connection_url: String,
sql: String,
) -> PyGeoArrowResult<Option<PyObject>> {
pub fn read_postgis(py: Python, connection_url: String, sql: String) -> PyResult<Option<PyObject>> {
// https://tokio.rs/tokio/topics/bridging#what-tokiomain-expands-to
tokio::runtime::Builder::new_multi_thread()
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async move {
let pool = PgPoolOptions::new()
.connect(&connection_url)
.await
.map_err(|err| PyGeoArrowError::GeoArrowError(GeoArrowError::SqlxError(err)))?;
.unwrap();

let table = _read_postgis(&pool, &sql)
.await
.map_err(PyGeoArrowError::GeoArrowError)?;

Ok(table
.map(|table| table_to_pytable(table).to_arro3(py))
.transpose()?)
})
// TODO: py.allow_threads
let out = runtime.block_on(read_postgis_inner(connection_url, sql))?;
out.map(|table| table.to_arro3(py)).transpose()
}

#[pyfunction]
pub fn read_postgis_async(
py: Python,
connection_url: String,
sql: String,
) -> PyGeoArrowResult<PyObject> {
let fut = future_into_py(py, async move {
let pool = PgPoolOptions::new()
.connect(&connection_url)
.await
.map_err(|err| PyGeoArrowError::GeoArrowError(GeoArrowError::SqlxError(err)))?;
) -> PyResult<Bound<PyAny>> {
future_into_py(py, read_postgis_inner(connection_url, sql))
}

async fn read_postgis_inner(connection_url: String, sql: String) -> PyResult<Option<PyTable>> {
let pool = PgPoolOptions::new()
.connect(&connection_url)
.await
.map_err(|err| PyGeoArrowError::GeoArrowError(GeoArrowError::SqlxError(err)))?;

let table = _read_postgis(&pool, &sql)
.await
.map_err(PyGeoArrowError::GeoArrowError)?;
let table = _read_postgis(&pool, &sql)
.await
.map_err(PyGeoArrowError::GeoArrowError)?;

Ok(table.map(table_to_pytable))
})?;
Ok(fut.into())
Ok(table.map(table_to_pytable))
}
5 changes: 3 additions & 2 deletions python/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ nav:
- api/compute/enums.md
- api/compute/types.md
- geoarrow.rust.io:
- api/io/arrow_ipc.md
- api/io/csv.md
- api/io/flatgeobuf.md
- api/io/gdal.md
- api/io/geojson.md
- api/io/geoparquet.md
- api/io/postgis.md
- api/io/arrow_ipc.md
- api/io/gdal.md
- api/io/shapefile.md
- Ecosystem:
- ecosystem/geopandas.md
- ecosystem/lonboard.md
Expand Down
Loading