I have 2 tables, the first is called 'filtered_commerce' and the 2nd is called 'filtered_secteur'. The 'filtered_commerce' table contains a 'zone_population' column which contains polygons and multipolygons representing geographic areas. The filtered_sector table contains a 'centroid_degres' column which contains points and an 'INS9' column which contains the identifiers of the points. I would like to find which points are in each zone and save for each zone the INS9 of the points included in it in a new column of the 'filtered_commerce'. Areas are polygons or multipolygons saved as follows: POLYGON ((4.589822 51.324047,....)) and points are saved as: POINT (4.440742, 51.179215)
I tried this :
# Fonction pour trouver les INS9 des points dans chaque zone
def find_points_in_zones(df_commerce, df_secteur):
results = {}
for commerce_index, row_commerce in df_commerce.iterrows():
zone = row_commerce['zone_population']
print(f"Zone {commerce_index}: {zone}")
points_in_zone = []
for secteur_index, row_secteur in df_secteur.iterrows():
point = row_secteur['centroid_degres']
print(f" Point {secteur_index}: {point}")
if zone.contains(point):
print('il y a un point!')
points_in_zone.append(row_secteur['INS9'])
results[commerce_index] = points_in_zone
return results
points_in_zones = find_points_in_zones(filtered_commerce, filtered_secteur)
# Afficher les résultats
for zone_index, points in points_in_zones.items():
print(f"Zone {zone_index}: {points}")
but it is as if no area contained points. I guess the problem is 'zone.contains(point)' because we never enter in this if. I also tried the '.intercepts' and it didn't worked.
In an other part of the project, I used a similar function but when I tried something similar here, it didn't work :
polygone_concurrence = isochrone_commerce_double.iloc[0]['geometry']
filtered_commerce.at[index, 'zone_concurrence'] = polygone_concurrence
concurrents = filtered_commerce[filtered_commerce.geometry.within(polygone_concurrence)]
concurrents_ids = concurrents['UNITID'].tolist()
filtered_commerce.at[index, 'concurrence'] = concurrents_ids
Thank you for your help.
It looks like you have WKT strings.
You can convert them to real geometries with shapely, create geodataframes and spatial join point data to the polygons: