@@ -206,11 +206,23 @@ def sjoin(left_df, right_df, op='intersects', how='inner',
206
206
raise ValueError ("'right_df' should be GeoDataFrame, got {}" .format (
207
207
type (right_df )))
208
208
209
- allowed_hows = [ 'left' , 'right' , 'inner' ]
209
+ allowed_hows = ( 'left' , 'right' , 'inner' )
210
210
if how not in allowed_hows :
211
211
raise ValueError ("`how` was \" %s\" but is expected to be in %s" %
212
212
(how , allowed_hows ))
213
213
214
+ original_op = op
215
+ original_how = how
216
+ if op == "within" :
217
+ # within implemented as the inverse of contains; swap names
218
+ # This is done for efficiency reasons
219
+ op = 'contains'
220
+ left_df , right_df = right_df , left_df
221
+ if how == 'left' :
222
+ how = 'right'
223
+ elif how == 'right' :
224
+ how = 'left'
225
+
214
226
if left_df .crs != right_df .crs :
215
227
warn (
216
228
('CRS of frames being joined does not match!'
@@ -240,6 +252,11 @@ def sjoin(left_df, right_df, op='intersects', how='inner',
240
252
left = left_df .take (left_indices )
241
253
right = right_df .take (right_indices )
242
254
255
+ if original_op == 'within' : # switch back
256
+ left , right = right , left
257
+ n_left , n_right = n_right , n_left
258
+ how = original_how
259
+
243
260
if how in ('inner' , 'left' ):
244
261
del right [right ._geometry_column_name ]
245
262
index = left .index
0 commit comments