Search code examples
pythonpandasspatialgeopandas

Intersects geoDataFrames



I learned solution from question
[https://stackoverflow.com/questions/45287150/identify-intersecting-polygons-in-single-geojson-file-with-geopandas][1]

i got a geoDataFrame of zid which intersect, but, when i test on-by-one, they don't intersect. Why?
I try solution from answer1 and 2, but the result the same.
zones 514, 519 have common border

admzones = admzones[['zid', 'geometry']]
together = geopandas.sjoin(admzones, admzones.set_index('zid'))
together.loc[together.zid != together.index_right]

# zid|geometry|index_right
# 519|POLIGON()|514
#...

...

geo1 = admzones[admzones['zid']==519]
geo2 = admzones[admzones['zid']==514]
geo1['interssects'] = geo1.intersects(geo2)
geo1['interssects']
#False

geo1['intersection'] = geo1.intersection(geo2)
geo1['intersection']
#None

enter image description here


Solution

  • You don't share an example but IIUC, you need to turn off the index-alignment (align=False):

    geo_519 = admzones.loc[admzones["zid"]==519]
    geo_514 = admzones.loc[admzones["zid"]==514]
    
    geo_519.intersects(geo_514, align=False).any() # True
    geo_519.intersection(geo_514, align=False).astype(bool).any() # True
    

    enter image description here

    admzones = admzones[["zid", "geometry"]]
    together = admzones.sjoin(admzones.set_index("zid"))
    
    print(together.loc[together["zid"] != together["index_right"]])
    
    #    zid                                           geometry  index_right
    # 0  514  POLYGON ((0.40000 0.10000, 0.75000 1.00000, 1....          519
    # 1  519  POLYGON ((0.60000 0.50000, 0.59904 0.48040, 0....          514
    

    Your code is most likely showing a UserWarning such :

    UserWarning: The indices of the two GeoSeries are different.

    From the intersects and intersection docs :

    enter image description here

    Used input :

    import geopandas as gpd
    from shapely.geometry import Polygon, Point
    
    admzones = gpd.GeoDataFrame(
        {"zid": [514, 519], "geometry":
         [Polygon([(0.4, 0.1), (0.75, 1), (1.5, 0.3)]),
          Point(0.4, 0.5).buffer(0.2)]})