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

Implement explode_geometries method #111

Merged
merged 5 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 76 additions & 57 deletions Cargo.lock

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

87 changes: 87 additions & 0 deletions geopolars/src/geoseries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub trait GeoSeries {
/// Applies to GeoSeries containing only Polygons. Returns `None` for other geometry types.
fn exterior(&self) -> Result<Series>;

/// Explodes multi-part geometries into multiple single geometries.
fn explode_geometries(&self) -> Result<Series>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that polars already has an explode function, but in this case, the meaning is the same for both, so maybe we could call this fn explode as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 28dd535


/// Create a Series from a vector of geometries
fn from_geom_vec(geoms: &[Geometry<f64>]) -> Result<Series>;

Expand Down Expand Up @@ -307,6 +310,60 @@ impl GeoSeries for Series {
Ok(series)
}

fn explode_geometries(&self) -> Result<Series> {
let mut nested_vector = vec![];

for geometry in iter_geom(self) {
match geometry {
Geometry::Point(geometry) => {
let point = vec![Geometry::Point(geometry)];
nested_vector.push(point)
}
Geometry::MultiPoint(geometry) => {
let points: Vec<Geometry> = geometry.into_iter().map(Geometry::Point).collect();
nested_vector.push(points)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having nested_vector being vec![vec![geometry]], could we push individual parts into that function?

for geom in geometry.into_iter() {
    output_vector.push(Geometry::Point(geom))
}

That means

  1. we avoid the extra vec construction for each geometry
  2. we avoid an extra iteration down below when you flatten

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 43f9edb

}
Geometry::Line(geometry) => {
let line = vec![Geometry::Line(geometry)];
nested_vector.push(line)
}
Geometry::LineString(geometry) => {
let line_string = vec![Geometry::LineString(geometry)];
nested_vector.push(line_string)
}
Geometry::MultiLineString(geometry) => {
let line_strings: Vec<Geometry> =
geometry.into_iter().map(Geometry::LineString).collect();
nested_vector.push(line_strings)
}
Geometry::Polygon(geometry) => {
let polygon = vec![Geometry::Polygon(geometry)];
nested_vector.push(polygon)
}
Geometry::MultiPolygon(geometry) => {
let polygons: Vec<Geometry> =
geometry.into_iter().map(Geometry::Polygon).collect();
nested_vector.push(polygons)
}
Geometry::Rect(geometry) => {
let rectangle = vec![Geometry::Rect(geometry)];
nested_vector.push(rectangle)
}
Geometry::Triangle(geometry) => {
let triangle = vec![Geometry::Triangle(geometry)];
nested_vector.push(triangle)
}
_ => unimplemented!(),
};
}

let flattened_vector: Vec<Geometry> = nested_vector.into_iter().flatten().collect();

let exploded_series = Series::from_geom_vec(&flattened_vector)?;

Ok(exploded_series)
}

fn exterior(&self) -> Result<Series> {
let mut output_array = MutableBinaryArray::<i32>::with_capacity(self.len());

Expand Down Expand Up @@ -943,6 +1000,36 @@ mod tests {
assert_eq!(10.0_f64, as_vec[0]);
}
#[test]
fn explode_geometries() {
let point_0 = Point::new(0., 0.);
let point_1 = Point::new(1., 1.);
let point_2 = Point::new(2., 2.);
let point_3 = Point::new(3., 3.);
let point_4 = Point::new(4., 4.);

let expected_series = Series::from_geom_vec(&[
Geometry::Point(point_0),
Geometry::Point(point_1),
Geometry::Point(point_2),
Geometry::Point(point_3),
Geometry::Point(point_4),
])
.unwrap();

let multipoint_0 = MultiPoint::new(vec![point_0, point_1]);
let multipoint_1 = MultiPoint::new(vec![point_2, point_3, point_4]);

let input_series = Series::from_geom_vec(&[
Geometry::MultiPoint(multipoint_0),
Geometry::MultiPoint(multipoint_1),
])
.unwrap();

let output_series = input_series.explode_geometries().unwrap();

assert_eq!(output_series, expected_series);
}
#[test]
fn haversine_length() {
let mut test_data = MutableBinaryArray::<i32>::with_capacity(1);

Expand Down