1

I have two GeoPandas DataFrames:

  1. points (long,lat) : 60,000 rows
  2. Jams segment (LineStrings) : 204,000 rows

I am trying to create a new DataFrame with the points that are located on LineStrings.

I tried using a buffer on the LineStrings and the predicate: contains,intersect but it didn't succeed

len(gdfTraffic_df)
201053
len(gdfAlerts_df)
58844

gdfTraffic_df.buffer(0.1)
merged = gdfTraffic_df.sjoin(gdfAlerts_df, predicate="contains")
len(merged)
474646

Something is not going as expected with the action of spatial join. Any ideas how to solve the problem?

0

1 Answer 1

1

You don't change the lines to polygons so you are trying to contain points within lines.

Try this:

import geopandas as gpd

lines = gpd.read_file(r"C:\GIS\data\lines.shp")
points = gpd.read_file(r"C:\GIS\data\points.shp")

lines["geometry"].buffer(100) #This just return buffered geometries, you dont change the line geometries to polygons.
lines.sjoin(points, how="inner", predicate="contains")
#So you get: Empty GeoDataFrame

lines["oldgeom"] = lines["geometry"] #Save the line geometries, for later use
lines = lines.set_geometry(lines["geometry"].buffer(100)) #Set the geometries of the df to polygons

lines = lines.sjoin(points, how="inner", predicate="contains")
#lines.shape
#Out[18]: (20, 9) Now there are matches

lines = lines.set_geometry(lines["oldgeom"]) #Set the geometries to the lines again.
del(lines["oldgeom"]) #Drop the column or shape export might fail.

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.