-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>; | ||
|
||
/// Create a Series from a vector of geometries | ||
fn from_geom_vec(geoms: &[Geometry<f64>]) -> Result<Series>; | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having for geom in geometry.into_iter() {
output_vector.push(Geometry::Point(geom))
} That means
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
||
|
@@ -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); | ||
|
||
|
There was a problem hiding this comment.
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 fnexplode
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 28dd535