My final project is to improve Waze with AI. In order to do that I need to join 2 datasets.
1 - jams - linestrings that I convert to polygons using buffer
2 - alerts by user - points
Both are in the same city.
After cleaning the data and make columns as geometry object I need to add to alerts DF new column with value of the jams that contain the alert. I tried a lot of solutions in the net, no one helped me.
This is the 2 dataset: Alerts + Traffic
screenshot head - enter image description here
Trafficjan.set_geometry("polygeo")
m = gpd.sjoin(Alertsjan, Trafficjan, how="left", predicate="within")
This code gives me nothing.
1 Answer 1
set_geometry is not in place, it is returning a data frame. So you are not changing anything with your code.
import geopandas as gpd
road = gpd.read_file(r"/home/bera/Desktop/GIStest/small_road_sample.shp")
alert = gpd.read_file(r"/home/bera/Desktop/GIStest/small_road_sample_randpoints.shp")
road["buffer"] = road.buffer(1000) #Create a column with buffered lines/polygons
road.set_geometry("buffer") #This does not set the geometry in place, it is returning a dataframe
# road.geometry
# 0 LINESTRING (674867.662 7175384.128, 674813.154...
# 1 LINESTRING (681203.816 7174459.938, 681064.945...
road["oldgeom"] = road.geometry #Save the line geometries to be
# able to able to go back to them after the join
road = road.set_geometry("buffer") #Then do this to set the polygons as geometries
# road.geometry
# 0 POLYGON ((674105.219 7173524.585, 674083.449 7...
# 1 POLYGON ((680835.875 7171500.973, 680640.249 7...
alerts_within = gpd.sjoin(left_df=alert, right_df=road, how="left", predicate="within")
# alerts_within
# id_left ... oldgeom
# 0 0.0 ... LINESTRING (665662.375 7166913.539, 665534.059...
# 0 0.0 ... LINESTRING (666183.436 7168040.957, 666178.778...
# 0 0.0 ... LINESTRING (665662.375 7166913.539, 666121.528...
# 1 1.0 ... None
Another option is to use sjoin_within.
Explore related questions
See similar questions with these tags.