This is an automated email from the ASF dual-hosted git repository.
jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sedona.git
The following commit(s) were added to refs/heads/master by this push:
new 6e8f733324 [GH-2022] Geopandas.GeoSeries: Implement geom_type (#2023)
6e8f733324 is described below
commit 6e8f7333243f1bdc297c1896a9522977cd01938e
Author: Peter Nguyen <[email protected]>
AuthorDate: Thu Jul 3 21:31:33 2025 -0700
[GH-2022] Geopandas.GeoSeries: Implement geom_type (#2023)
---
python/sedona/geopandas/geoseries.py | 41 ++++++++++++++++++--
python/tests/geopandas/test_geoseries.py | 45 +++++++++++++++++++++-
.../tests/geopandas/test_match_geopandas_series.py | 5 ++-
3 files changed, 85 insertions(+), 6 deletions(-)
diff --git a/python/sedona/geopandas/geoseries.py
b/python/sedona/geopandas/geoseries.py
index ea75b3da80..6adb2f1529 100644
--- a/python/sedona/geopandas/geoseries.py
+++ b/python/sedona/geopandas/geoseries.py
@@ -543,9 +543,44 @@ class GeoSeries(GeoFrame, pspd.Series):
return self._process_geometry_column("ST_Area",
rename="area").to_spark_pandas()
@property
- def geom_type(self):
- # Implementation of the abstract method
- raise NotImplementedError("This method is not implemented yet.")
+ def geom_type(self) -> pspd.Series:
+ """
+ Returns a series of strings specifying the geometry type of each
geometry of each object.
+
+ Note: Unlike Geopandas, Sedona returns LineString instead of
LinearRing.
+
+ Returns
+ -------
+ Series
+ A Series containing the geometry type of each geometry.
+
+ Examples
+ --------
+ >>> from shapely.geometry import Polygon, Point
+ >>> from sedona.geopandas import GeoSeries
+
+ >>> gs = GeoSeries([Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]),
Point(0, 0)])
+ >>> gs.geom_type
+ 0 POLYGON
+ 1 POINT
+ dtype: object
+ """
+ result = self._process_geometry_column(
+ "GeometryType", rename="geom_type"
+ ).to_spark_pandas()
+
+ # Sedona returns the string in all caps unlike Geopandas
+ sgpd_to_gpg_name_map = {
+ "POINT": "Point",
+ "LINESTRING": "LineString",
+ "POLYGON": "Polygon",
+ "MULTIPOINT": "MultiPoint",
+ "MULTILINESTRING": "MultiLineString",
+ "MULTIPOLYGON": "MultiPolygon",
+ "GEOMETRYCOLLECTION": "GeometryCollection",
+ }
+ result = result.map(lambda x: sgpd_to_gpg_name_map.get(x, x))
+ return result
@property
def type(self):
diff --git a/python/tests/geopandas/test_geoseries.py
b/python/tests/geopandas/test_geoseries.py
index 99bc9aa654..3c7951e941 100644
--- a/python/tests/geopandas/test_geoseries.py
+++ b/python/tests/geopandas/test_geoseries.py
@@ -23,7 +23,16 @@ import sedona.geopandas as sgpd
from sedona.geopandas import GeoSeries
from tests.test_base import TestBase
from shapely import wkt
-from shapely.geometry import Point, LineString, Polygon, GeometryCollection,
LinearRing
+from shapely.geometry import (
+ Point,
+ LineString,
+ Polygon,
+ GeometryCollection,
+ MultiPoint,
+ MultiLineString,
+ MultiPolygon,
+ LinearRing,
+)
from pandas.testing import assert_series_equal
@@ -167,7 +176,39 @@ class TestGeoSeries(TestBase):
pass
def test_geom_type(self):
- pass
+ geoseries = sgpd.GeoSeries(
+ [
+ Point(0, 0),
+ MultiPoint([Point(0, 0), Point(1, 1)]),
+ LineString([(0, 0), (1, 1)]),
+ MultiLineString(
+ [LineString([(0, 0), (1, 1)]), LineString([(2, 2), (3,
3)])]
+ ),
+ Polygon([(0, 0), (1, 0), (0, 1)]),
+ MultiPolygon(
+ [
+ Polygon([(0, 0), (1, 0), (0, 1)]),
+ Polygon([(2, 2), (3, 2), (2, 3)]),
+ ]
+ ),
+ GeometryCollection([Point(0, 0), LineString([(0, 0), (1,
1)])]),
+ LinearRing([(0, 0), (1, 1), (1, 0), (0, 1), (0, 0)]),
+ ]
+ )
+ result = geoseries.geom_type
+ expected = pd.Series(
+ [
+ "Point",
+ "MultiPoint",
+ "LineString",
+ "MultiLineString",
+ "Polygon",
+ "MultiPolygon",
+ "GeometryCollection",
+ "LineString", # Note: Sedona returns LineString instead of
LinearRing
+ ]
+ )
+ assert_series_equal(result.to_pandas(), expected)
def test_type(self):
pass
diff --git a/python/tests/geopandas/test_match_geopandas_series.py
b/python/tests/geopandas/test_match_geopandas_series.py
index c4d6eb9da0..4a4a3495ca 100644
--- a/python/tests/geopandas/test_match_geopandas_series.py
+++ b/python/tests/geopandas/test_match_geopandas_series.py
@@ -298,7 +298,10 @@ class TestMatchGeopandasSeries(TestBase):
pass
def test_geom_type(self):
- pass
+ for _, geom in self.geoms:
+ sgpd_result = GeoSeries(geom).geom_type
+ gpd_result = gpd.GeoSeries(geom).geom_type
+ self.check_pd_series_equal(sgpd_result, gpd_result)
def test_type(self):
pass