5

I have two GeoPandas dataframes titre_polyG and df which I have intersected as follows:

titre_polyG is as follows:

0      MULTIPOLYGON (((-7.64026 33.59134, -7.63996 33...
1      MULTIPOLYGON (((-7.63987 33.58992, -7.63988 33...
2      MULTIPOLYGON (((-7.63643 33.59023, -7.63670 33...
3      MULTIPOLYGON (((-7.63868 33.59040, -7.63874 33...
4      MULTIPOLYGON (((-7.63892 33.59003, -7.63903 33...
                             ...                        
133    MULTIPOLYGON (((-7.63924 33.59013, -7.63933 33...
134    MULTIPOLYGON (((-7.63958 33.59210, -7.63939 33...
135    MULTIPOLYGON (((-7.63958 33.59210, -7.63939 33...
136    MULTIPOLYGON (((-7.63958 33.59210, -7.63939 33...
137    MULTIPOLYGON (((-7.63937 33.59163, -7.63937 33...
Name: geometry, Length: 138, dtype: geometry

df is as follows:

      apt_psqm           geometry
0   15010.000000    POLYGON ((-7.61096 33.55602, -7.61095 33.55602...
1   12675.000000    POLYGON ((-7.59432 33.53328, -7.59432 33.53328...
2   13810.000000    POLYGON ((-7.65112 33.52504, -7.65112 33.52504...
3   16070.000000    POLYGON ((-7.64377 33.54902, -7.64377 33.54902...
4   13930.000000    POLYGON ((-7.64605 33.52386, -7.64605 33.52387...
... ... ...
85  10732.000000    POLYGON ((-7.52905 33.61630, -7.52905 33.61630...
86  9900.000000 POLYGON ((-7.49753 33.59832, -7.49753 33.59832...
87  9170.000000 POLYGON ((-7.51582 33.59657, -7.51582 33.59657...
88  8509.166667 MULTIPOLYGON (((-7.55564 33.57855, -7.55563 33...
89  9495.280899 POLYGON ((-7.57822 33.56580, -7.57821 33.56580...

The code related to the intersection is below:

# iterate over each row in df and identify intersecting polygons

for index, row in df.iterrows():   
    match_poly = df[df.geometry.intersects(titre_polyG['geometry'])]

match_poly shows the rows of df where there is an intersection with titre_polyG.

My aim is to find the row numbers in titre_polyG where that intersection happens. titre_polyG has 138 rows and if intersection happens for instance at row 1 and 60 then I need to find a way to display those numbers.

Any idea?

0

2 Answers 2

5

Unless you need the intersection geometries you dont need to intersect. You can use spatial join instead:

import geopandas as gpd
import numpy as np

titre = gpd.read_file("/home/bera/Desktop/GIStest/titre.shp")
titre["titrerow"] = np.arange(0, titre.shape[0]).astype(int) #Create a row number, to later groupby
df = gpd.read_file("/home/bera/Desktop/GIStest/df.shp")
df["row"] = np.arange(0, df.shape[0]).astype(int).astype(str) #Create a row number column

join = gpd.sjoin(left_df=titre[["titrerow","geometry"]], right_df=df[["row","geometry"]], how="left", predicate="intersects")
#print(join[["titrerow","row"]])
#    titrerow row
#         0   1
#         0   2
#         0   0
#         1   4
#         2   4
#So titrerow 0 is intersecting 1, 2 and 0 ...

#Groupby titrerow and concatenate the df rows
intersecting = join.groupby("titrerow")["row"].apply(lambda x: ','.join(x)) 
#0    1,2,0
#1        4
#2        4

#Join the result to input
titre = titre.merge(intersecting, left_on="titrerow", right_index=True)
0
0

@BERA Thank you. Just like you said, by using spatial join we can retrieve all attributes about the intersecting polygons which is what I was looking for at the first place.

Here is the new code changed accordingly:

join = gpd.sjoin(left_df=df, right_df=titre_polyG,  how="left", predicate="intersects")
    
#Groupby by the required column

intersecting=join.groupby('quartier').agg({'Montant_x':'mean','Titre':'-'.join, 'Adresse':'-'.join}).reset_index()
1
  • 1
    I will amend my response. Thank you
    – bravopapa
    Commented Nov 28, 2022 at 13:02

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.