Skip to content

Commit 501df70

Browse files
authored
[GH-2768] Replace len(self)==0 with cheaper _is_empty() check in GeoSeries (#2770)
1 parent e01cd5c commit 501df70

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

python/sedona/spark/geopandas/geoseries.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ def __init__(
341341
if crs:
342342
self.set_crs(crs, inplace=True)
343343

344+
def _is_empty(self) -> bool:
345+
"""Check if this GeoSeries has no rows without triggering a full Spark scan."""
346+
return not self._internal.spark_frame.take(1)
347+
344348
# ============================================================================
345349
# COORDINATE REFERENCE SYSTEM (CRS) OPERATIONS
346350
# ============================================================================
@@ -382,7 +386,7 @@ def crs(self) -> Union["CRS", None]:
382386
"""
383387
from pyproj import CRS
384388

385-
if len(self) == 0:
389+
if self._is_empty():
386390
return None
387391

388392
# F.first is non-deterministic, but it doesn't matter because all non-null values should be the same.
@@ -1152,8 +1156,8 @@ def line_merge(self, directed=False):
11521156
)
11531157

11541158
def build_area(self, node=True):
1155-
if len(self) == 0:
1156-
return GeoSeries([], name="polygons", crs=self.crs)
1159+
if self._is_empty():
1160+
return GeoSeries([], name="polygons", crs=None)
11571161

11581162
if node:
11591163
aggr_expr = sta.ST_Union_Aggr(self.spark.column)
@@ -1189,8 +1193,8 @@ def polygonize(self, node=True, full=False):
11891193
"Sedona does not support full=True for polygonize."
11901194
)
11911195

1192-
if len(self) == 0:
1193-
return GeoSeries([], name="polygons", crs=self.crs)
1196+
if self._is_empty():
1197+
return GeoSeries([], name="polygons", crs=None)
11941198

11951199
if node:
11961200
aggr_expr = sta.ST_Union_Aggr(self.spark.column)
@@ -1245,7 +1249,7 @@ def union_all(self, method="unary", grid_size=None) -> BaseGeometry:
12451249
f"Sedona does not support manually specifying different union methods. Ignoring non-default method argument of {method}"
12461250
)
12471251

1248-
if len(self) == 0:
1252+
if self._is_empty():
12491253
# While it's not explicitly defined in GeoPandas docs, this is what GeoPandas returns for empty GeoSeries.
12501254
# If it ever changes for some reason, we'll catch that with the test
12511255
from shapely.geometry import GeometryCollection
@@ -1260,7 +1264,7 @@ def union_all(self, method="unary", grid_size=None) -> BaseGeometry:
12601264
return geom
12611265

12621266
def intersection_all(self) -> BaseGeometry:
1263-
if len(self) == 0:
1267+
if self._is_empty():
12641268
from shapely.geometry import GeometryCollection
12651269

12661270
return GeometryCollection()
@@ -2645,7 +2649,7 @@ def bounds(self) -> pspd.DataFrame:
26452649
def total_bounds(self):
26462650
import warnings
26472651

2648-
if len(self) == 0:
2652+
if self._is_empty():
26492653
# numpy 'min' cannot handle empty arrays
26502654
# TODO with numpy >= 1.15, the 'initial' argument can be used
26512655
return np.array([np.nan, np.nan, np.nan, np.nan])

0 commit comments

Comments
 (0)