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')
3
  • 1
    is there a disjoint op?
    – Ian Turton
    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()
    – Amol
    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
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())]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.