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

Coord buffer refactor #844

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 rust/geoarrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ name = "area"
harness = false
required-features = ["flatgeobuf"]

[[bench]]
name = "total_bounds"
harness = false
required-features = ["flatgeobuf"]

[[bench]]
name = "from_geo"
harness = false
Expand Down
3 changes: 2 additions & 1 deletion rust/geoarrow/benches/geos_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use criterion::{criterion_group, criterion_main, Criterion};
use geoarrow::algorithm::geos::Buffer;
use geoarrow::array::{CoordBuffer, InterleavedCoordBuffer, PointArray, PolygonArray};
use geoarrow::datatypes::Dimension;

fn generate_data() -> PointArray<2> {
let coords = vec![0.0; 100_000];
let coord_buffer = CoordBuffer::Interleaved(InterleavedCoordBuffer::new(coords.into()));
let coord_buffer = CoordBuffer::new_interleaved(coords.into(), Dimension::XY).unwrap();
PointArray::new(coord_buffer, None, Default::default())
}

Expand Down
34 changes: 34 additions & 0 deletions rust/geoarrow/benches/total_bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use criterion::{criterion_group, criterion_main, Criterion};
use geoarrow::algorithm::geo::Area;
use geoarrow::algorithm::native::TotalBounds;
use geoarrow::array::{AsChunkedNativeArray, MultiPolygonArray};
use geoarrow::io::flatgeobuf::read_flatgeobuf;
use geoarrow::trait_::ArrayAccessor;
use std::fs::File;

fn load_file() -> MultiPolygonArray<2> {
let mut file = File::open("fixtures/flatgeobuf/countries.fgb").unwrap();
let table = read_flatgeobuf(&mut file, Default::default()).unwrap();
table
.geometry_column(None)
.unwrap()
.as_ref()
.as_multi_polygon::<2>()
.chunks()
.first()
.unwrap()
.clone()
}

fn criterion_benchmark(c: &mut Criterion) {
let data = load_file();

c.bench_function("total_bounds", |bencher| {
bencher.iter(|| {
criterion::black_box(criterion::black_box(&data).total_bounds());
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
6 changes: 3 additions & 3 deletions rust/geoarrow/src/algorithm/native/downcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Downcast for MultiPointArray<2> {
fn downcast(&self, small_offsets: bool) -> Self::Output {
// Note: this won't allow a downcast for empty MultiPoints
if *self.geom_offsets.last() as usize == self.len() {
return Arc::new(PointArray::new(
return Arc::new(PointArray::<2>::new(
self.coords.clone(),
self.validity.clone(),
self.metadata(),
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Downcast for MultiLineStringArray<2> {

fn downcast(&self, small_offsets: bool) -> Self::Output {
if *self.geom_offsets.last() as usize == self.len() {
return Arc::new(LineStringArray::new(
return Arc::new(LineStringArray::<2>::new(
self.coords.clone(),
self.ring_offsets.clone(),
self.validity.clone(),
Expand Down Expand Up @@ -203,7 +203,7 @@ impl Downcast for MultiPolygonArray<2> {

fn downcast(&self, small_offsets: bool) -> Self::Output {
if *self.geom_offsets.last() as usize == self.len() {
return Arc::new(PolygonArray::new(
return Arc::new(PolygonArray::<2>::new(
self.coords.clone(),
self.polygon_offsets.clone(),
self.ring_offsets.clone(),
Expand Down
68 changes: 34 additions & 34 deletions rust/geoarrow/src/algorithm/native/map_coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ pub trait MapCoords {

fn map_coords<F>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> geo::Coord + Sync,
F: Fn(&crate::scalar::Coord) -> geo::Coord + Sync,
{
self.try_map_coords(|coord| Ok::<_, GeoArrowError>(map_op(coord)))
}

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>;
}

// Scalar impls

impl MapCoords for Coord<'_, 2> {
impl MapCoords for Coord<'_> {
type Output = geo::Coord;

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(map_op(self)?)
Expand All @@ -48,7 +48,7 @@ impl MapCoords for Point<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(geo::Point(map_op(&self.coord())?))
Expand All @@ -60,7 +60,7 @@ impl MapCoords for LineString<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let output_coords = self
Expand All @@ -76,7 +76,7 @@ impl MapCoords for Polygon<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
if self.exterior().is_none() {
Expand All @@ -98,7 +98,7 @@ impl MapCoords for MultiPoint<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let points = self
Expand All @@ -114,7 +114,7 @@ impl MapCoords for MultiLineString<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let lines = self
Expand All @@ -131,7 +131,7 @@ impl MapCoords for MultiPolygon<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let polygons = self
Expand All @@ -147,7 +147,7 @@ impl MapCoords for Geometry<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
use GeometryType::*;
Expand Down Expand Up @@ -175,7 +175,7 @@ impl MapCoords for GeometryCollection<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let geoms = self
Expand All @@ -191,7 +191,7 @@ impl MapCoords for Rect<'_, 2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let lower = self.min();
Expand All @@ -201,7 +201,7 @@ impl MapCoords for Rect<'_, 2> {
let maxx = upper.x();
let maxy = upper.y();
let coords = vec![minx, miny, maxx, maxy];
let coord_buffer = CoordBuffer::Interleaved(InterleavedCoordBuffer::new(coords.into()));
let coord_buffer = CoordBuffer::new_interleaved(coords.into(), Dimension::XY).unwrap();
let lower_coord = coord_buffer.value(0);
let upper_coord = coord_buffer.value(1);

Expand All @@ -216,7 +216,7 @@ impl MapCoords for PointArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = PointBuilder::with_capacity_and_options(
Expand All @@ -241,7 +241,7 @@ impl MapCoords for LineStringArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = LineStringBuilder::with_capacity_and_options(
Expand All @@ -266,7 +266,7 @@ impl MapCoords for PolygonArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = PolygonBuilder::with_capacity_and_options(
Expand All @@ -291,7 +291,7 @@ impl MapCoords for MultiPointArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = MultiPointBuilder::with_capacity_and_options(
Expand All @@ -316,7 +316,7 @@ impl MapCoords for MultiLineStringArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = MultiLineStringBuilder::with_capacity_and_options(
Expand All @@ -341,7 +341,7 @@ impl MapCoords for MultiPolygonArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = MultiPolygonBuilder::with_capacity_and_options(
Expand All @@ -366,7 +366,7 @@ impl MapCoords for MixedGeometryArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = MixedGeometryBuilder::with_capacity_and_options(
Expand All @@ -392,7 +392,7 @@ impl MapCoords for GeometryCollectionArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = GeometryCollectionBuilder::with_capacity_and_options(
Expand All @@ -418,7 +418,7 @@ impl MapCoords for RectArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
let mut builder = RectBuilder::with_capacity_and_options(self.len(), self.metadata());
Expand All @@ -439,7 +439,7 @@ impl MapCoords for &dyn NativeArray {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
use Dimension::*;
Expand Down Expand Up @@ -470,7 +470,7 @@ impl MapCoords for ChunkedPointArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -484,7 +484,7 @@ impl MapCoords for ChunkedLineStringArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -498,7 +498,7 @@ impl MapCoords for ChunkedPolygonArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -512,7 +512,7 @@ impl MapCoords for ChunkedMultiPointArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -526,7 +526,7 @@ impl MapCoords for ChunkedMultiLineStringArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -540,7 +540,7 @@ impl MapCoords for ChunkedMultiPolygonArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -554,7 +554,7 @@ impl MapCoords for ChunkedMixedGeometryArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -568,7 +568,7 @@ impl MapCoords for ChunkedGeometryCollectionArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -582,7 +582,7 @@ impl MapCoords for ChunkedRectArray<2> {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
Ok(ChunkedGeometryArray::new(
Expand All @@ -596,7 +596,7 @@ impl MapCoords for &dyn ChunkedNativeArray {

fn try_map_coords<F, E>(&self, map_op: F) -> Result<Self::Output>
where
F: Fn(&crate::scalar::Coord<2>) -> std::result::Result<geo::Coord, E> + Sync,
F: Fn(&crate::scalar::Coord) -> std::result::Result<geo::Coord, E> + Sync,
GeoArrowError: From<E>,
{
use Dimension::*;
Expand Down
Loading
Loading