Skip to content

Commit cb5f53c

Browse files
mrocklinjorisvandenbossche
authored andcommitted
Support sjoin on small or empty dataframes
1 parent ac67fb8 commit cb5f53c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

geopandas/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from geopandas.tools import sjoin
1010
from geopandas.tools import overlay
1111

12+
from shapely.geometry.base import CAP_STYLE, JOIN_STYLE
13+
1214
import geopandas.datasets
1315

1416
# make the interactive namespace easier to use

geopandas/algos.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void strtree_query_callback(void *item, void *vec)
2222
GEOSSTRtree *create_index(GEOSContextHandle_t handle, GEOSGeometry **geoms, size_t n)
2323
{
2424
int i;
25-
GEOSSTRtree* tree = GEOSSTRtree_create_r(handle, n);
25+
GEOSSTRtree* tree = GEOSSTRtree_create_r(handle, n > 1 ? n : 2);
2626

2727
for (i = 0; i < n ; i++)
2828
{
@@ -62,6 +62,8 @@ size_vector sjoin(GEOSContextHandle_t handle,
6262
kv_init(vec);
6363

6464
// begin = clock();
65+
if (nright == 0)
66+
return out;
6567

6668
GEOSSTRtree* tree = create_index(handle, right, nright);
6769

geopandas/tools/tests/test_sjoin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,19 @@ def test_errors():
103103

104104
assert "both" in str(info.value)
105105
assert "inner" in str(info.value)
106+
107+
108+
@pytest.mark.parametrize('l', [0, 1, 5])
109+
@pytest.mark.parametrize('r', [0, 1, 5])
110+
def test_small(l, r):
111+
left = gpd.GeoDataFrame({'geometry': triangles,
112+
'x': np.random.random(len(triangles)),
113+
'y': np.random.random(len(triangles))},
114+
index=np.arange(len(triangles)) * 2).iloc[:l]
115+
116+
right = gpd.GeoDataFrame({'geometry': points,
117+
'x': np.random.random(len(points)),
118+
'z': np.random.random(len(points))},
119+
index=list(string.ascii_lowercase[:len(points)])).iloc[:r]
120+
121+
result = sjoin(left, right)

0 commit comments

Comments
 (0)