TransWikia.com

Invert of Spatial Join within (Not Within)

Geographic Information Systems Asked by Amol on June 10, 2021

I have two geodataframes

  1. Source_Line (LineString)
  2. Edge_Area (Polygon)

I am using Geopandas for retrieving LineStrings (Source_Line) Geometry within Polygon

Following is Code

gpd_sJoin = ~gpd.sjoin(Source_Line, Edge_Area, how='inner', op='within')

Now, the objective is to retrieve geometry that is not within that means invert/inverse/opposite of within
tried following is my code

gpd_sJoin = ~gpd.sjoin(Source_Line, Edge_Area, how='inner', op='within')

2 Answers

enter image description here

Simply use left in sjoin and you get both results

print(Source_Line)
    id              geometry
0   1  LINESTRING (-0.74637 0.25810, -0.56089 0.08156)
1   2  LINESTRING (-0.55419 0.37654, -0.59441 0.22458)
2   3  LINESTRING (-0.29050 0.43017, -0.16983 0.32961)
3   4  LINESTRING (-0.92067 0.53073, -1.04134 0.38101)
4   5  LINESTRING (-0.38212 0.21341, -0.08045 0.22905)

Within with inner (line 1 and 2)

print(gpd.sjoin(Source_Line, Edge_Area, how='inner', op='within'))
 id_left          geometry                           index_right  id_right
0   1  LINESTRING (-0.74637 0.25810, -0.56089 0.08156)    0     0
1   2  LINESTRING (-0.55419 0.37654, -0.59441 0.22458)    0     0

Within and not within with left:

print(gpd.sjoin(Source_Line, Edge_Area, how='left', op='within'))
 id_left         geometry                           index_right  id_right
0   1  LINESTRING (-0.74637 0.25810, -0.56089 0.08156)    0     0
1   2  LINESTRING (-0.55419 0.37654, -0.59441 0.22458)    0     0
2   3  LINESTRING (-0.29050 0.43017, -0.16983 0.32961)   NaN   NaN
3   4  LINESTRING (-0.92067 0.53073, -1.04134 0.38101)   NaN   NaN
4   5  LINESTRING (-0.38212 0.21341, -0.08045 0.22905)   NaN   NaN

Within:

res = gpd.sjoin(Source_Line, Edge_Area, how='left', op='within')
print(res.loc[res['id_right'].notna()])
 id_left         geometry                           index_right  id_right
0   1  LINESTRING (-0.74637 0.25810, -0.56089 0.08156)    0     0
1   2  LINESTRING (-0.55419 0.37654, -0.59441 0.22458)    0     0

Not within:

print(res.loc[res['id_right'].isna()])
 id_left         geometry                           index_right  id_right
2   3  LINESTRING (-0.29050 0.43017, -0.16983 0.32961)   NaN   NaN
3   4  LINESTRING (-0.92067 0.53073, -1.04134 0.38101)   NaN   NaN
4   5  LINESTRING (-0.38212 0.21341, -0.08045 0.22905)   NaN   NaN

Answered by gene on June 10, 2021

I tried following the code and it's working

Source_Line.reset_index(drop=True, inplace=True)

Source_Line['Uniqq'] = Source_Line.index

gpd_sJoin = gpd.sjoin(Source_Line, Edge_Area, how='inner', op='within')

result = Source_Line[~Source_Line['Uniqq'].isin(gpd_sJoin['Uniqq'].unique())]

Answered by Amol on June 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP