geopandas point in polygon

后端 未结 1 563
南方客
南方客 2020-12-25 14:46

I have a GeoDataFrame of polygons (~30) and a GeoDataFrame of Points (~10k)

I\'m looking to create 30 new columns (with appropriate polygon names) in my GeoDataFrame

相关标签:
1条回答
  • 2020-12-25 15:21

    Not really clear what kind of data structures you actually have. Also, all your expected results are False, so that's kind of hard to check. Assuming GeoSeries and GeoDataFrames, I would do this:

    from shapely.geometry import Point, Polygon
    import geopandas
    
    polys = geopandas.GeoSeries({
        'foo': Polygon([(5, 5), (5, 13), (13, 13), (13, 5)]),
        'bar': Polygon([(10, 10), (10, 15), (15, 15), (15, 10)]),
    })
    
    _pnts = [Point(3, 3), Point(8, 8), Point(11, 11)]
    pnts = geopandas.GeoDataFrame(geometry=_pnts, index=['A', 'B', 'C'])
    pnts = pnts.assign(**{key: pnts.within(geom) for key, geom in polys.items()})
    
    print(pnts)
    

    And that gives me:

            geometry    bar    foo
    A    POINT (3 3)  False  False
    B    POINT (8 8)  False   True
    C  POINT (11 11)   True   True
    
    0 讨论(0)
提交回复
热议问题