0

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')
MrXsquared
36.2k22 gold badges76 silver badges127 bronze badges
asked Jun 4, 2021 at 9:05
3
  • 1
    is there a disjoint op? Commented Jun 4, 2021 at 9:40
  • Yes I also tried with Source_Line[Source_Line.geometry.disjoint(Edge_Area)] could not able to use .to_file() Commented Jun 4, 2021 at 11:09
  • 1
    Can you provide some reproducible example? Commented Jun 4, 2021 at 12:48

2 Answers 2

1

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 Jun 4, 2021 at 16:26
0
0

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())]
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Jun 9, 2021 at 4:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.