Skip to content

Commit 0a8dd86

Browse files
authored
[GH-1993] Geopandas __repr__() and to_geopandas() (#1990)
* Implement to_geopandas and __repr__ for GeoSeries * Add check_geoseries_equal() to test_geoseries * Use wkb.loads instead of from_wkb and fix lint * Combine check_geoseries and check_geoseries_equal into one function
1 parent fd7aa6a commit 0a8dd86

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

python/sedona/geopandas/geoseries.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from pyspark.pandas.utils import scol_for
2929
from pyspark.sql.types import BinaryType
3030

31+
import shapely
32+
3133
from sedona.geopandas._typing import Label
3234
from sedona.geopandas.base import GeoFrame
3335
from sedona.geopandas.geodataframe import GeoDataFrame
@@ -43,6 +45,18 @@ def __getitem__(self, key: Any) -> Any:
4345
# Implementation of the abstract method
4446
raise NotImplementedError("This method is not implemented yet.")
4547

48+
def __repr__(self) -> str:
49+
"""
50+
Return a string representation of the GeoSeries in WKT format.
51+
"""
52+
try:
53+
pandas_series = self.to_geopandas()
54+
return gpd.GeoSeries(pandas_series).__repr__()
55+
56+
except Exception as e:
57+
# Fallback to parent's representation if conversion fails
58+
return super().__repr__()
59+
4660
def __init__(
4761
self,
4862
data=None,
@@ -206,13 +220,19 @@ def dtypes(self) -> Union[gpd.GeoSeries, pd.Series, Dtype]:
206220
# Implementation of the abstract method
207221
raise NotImplementedError("This method is not implemented yet.")
208222

209-
def to_geopandas(self) -> Union[gpd.GeoDataFrame, pd.Series]:
210-
# Implementation of the abstract method
211-
raise NotImplementedError("This method is not implemented yet.")
223+
def to_geopandas(self) -> gpd.GeoSeries:
224+
"""
225+
Convert the GeoSeries to a geopandas GeoSeries.
212226
213-
def _to_geopandas(self) -> Union[gpd.GeoDataFrame, pd.Series]:
214-
# Implementation of the abstract method
215-
raise NotImplementedError("This method is not implemented yet.")
227+
Returns:
228+
- geopandas.GeoSeries: A geopandas GeoSeries.
229+
"""
230+
return self._to_geopandas()
231+
232+
def _to_geopandas(self) -> gpd.GeoSeries:
233+
return gpd.GeoSeries(
234+
self._to_internal_pandas().map(lambda wkb: shapely.wkb.loads(bytes(wkb)))
235+
)
216236

217237
@property
218238
def geometry(self) -> "GeoSeries":

python/tests/geopandas/test_geoseries.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import shutil
1919
import tempfile
20+
from geopandas.testing import assert_geoseries_equal
2021

2122
from shapely.geometry import (
2223
Point,
@@ -44,7 +45,7 @@ def teardown_method(self):
4445

4546
def test_constructor(self):
4647
s = GeoSeries([Point(x, x) for x in range(3)])
47-
check_geoseries(s)
48+
check_geoseries_equal(s, s)
4849

4950
def test_psdf(self):
5051
# this is to make sure the spark session works with pandas on spark api
@@ -109,6 +110,13 @@ def test_buffer_then_geoparquet(self):
109110
# -----------------------------------------------------------------------------
110111

111112

112-
def check_geoseries(s):
113-
assert isinstance(s, GeoSeries)
114-
assert isinstance(s.geometry, GeoSeries)
113+
def check_geoseries_equal(s1, s2):
114+
assert isinstance(s1, GeoSeries)
115+
assert isinstance(s1.geometry, GeoSeries)
116+
assert isinstance(s2, GeoSeries)
117+
assert isinstance(s2.geometry, GeoSeries)
118+
if isinstance(s1, GeoSeries):
119+
s1 = s1.to_geopandas()
120+
if isinstance(s2, GeoSeries):
121+
s2 = s2.to_geopandas()
122+
assert_geoseries_equal(s1, s2)

0 commit comments

Comments
 (0)