I ran my code successfully last week, cleared the kernel and now it does not work. It must be something simple I'm missing, but I cannot seem to puzzle it out. The purpose of the code is to find all of the polygons inside each of the polygons with a hole. In the data I provided here, there are two polygons with holes in the test_donuts_1.shp file and there are three polygons (including the two polygons with holes) in the test_poly1.shp file.

    import geopandas as gpd

    test_poly1 = gpd.read_file('test_poly1.shp')
    test_donuts_1 = gpd.read_file('test_donuts_1.shp')
    intersections_df = gpd.sjoin(test_poly1, test_donuts_1, how='inner', op='within')

This code will run without error for me, but the output is incorrect. It will say that they are only contained within themselves. But I know that if you union the three polygons in test_poly1 together, you get a single polygon. What I mean is that there are two polygons inside one of the test_donuts_1 polygons and there is one polygon inside of the other test_donuts_1 polygon, but this is not caught by sjoin. I have tried setting the crs for both geometries to be the same in my main file even though it is not reflected in the code above.

Can anyone see where I went wrong with this?

  • The 'within' operation does not mean they are "surrounded" by the other polygon, they really need to be within the actual polygon area. So being located within a hole of the polygon is not 'within' that polygon. – joris Aug 20 '18 at 18:58
  • @joris, would there be a proper way to detect which polygons are in the hole of another polygon (not necessarily touching the boundary of the hole of the polygon) using sjoin? – John Asplund Aug 21 '18 at 16:47
  • One way I can think of: you would first need to extract the holes (called .interiors), turn those into polygons, and then you can use sjoin with those polygons – joris Aug 21 '18 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.