如何在 geopandas 的多边形中创建点
How to make point in polygon in geopandas
我有两个 pandas 数据框和 geopandas 数组
第一个,pnts
0 POINT (96.69866 5.20478)
1 POINT (97.92708 2.34781)
2 POINT (96.12920 4.13244)
3 POINT (96.85947 4.71685)
4 POINT (97.95656 4.47959)
...
2922 POINT (136.06094 -1.17746)
2923 POINT (140.70931 -2.56853)
2924 POINT (138.31903 -2.37457)
2925 POINT (140.62557 -4.90224)
2926 POINT (136.07973 -1.15792)
Name: geometry, Length: 2927, dtype: geometry
第二个,polys
0 POLYGON ((104.82248 -2.98102, 104.82165 -2.985...
1 POLYGON ((104.75516 -3.01316, 104.75480 -3.013...
2 POLYGON ((104.77081 -2.98185, 104.77082 -2.981...
3 POLYGON ((104.77081 -2.98185, 104.77070 -2.981...
4 POLYGON ((104.77317 -2.99340, 104.77316 -2.993...
...
81907 POLYGON ((138.79489 -4.09306, 138.78723 -4.096...
81908 POLYGON ((136.88606 -3.76603, 136.86576 -3.772...
81909 POLYGON ((121.51784 -8.76968, 121.51784 -8.769...
81910 POLYGON ((122.69762 0.87165, 122.69698 0.87106...
81911 POLYGON ((97.48882 1.09282, 97.48853 1.09108, ...
Name: geometry, Length: 81912, dtype: geometry
我想做的是,知道哪个多边形gdf['geometry']
包括
我做的是
pnts = pnts.assign(**{key: pnts.within(geom) for key, geom in polys.items()})
错误
<ipython-input-39-aaf67bdae48e> in <module>
----> 3 pnts = pnts.assign(**{key: pnts.within(geom) for key, geom in polys.items()})
~/.local/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
5139 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5140 return self[name]
-> 5141 return object.__getattribute__(self, name)
5142
5143 def __setattr__(self, name: str, value) -> None:
AttributeError: 'GeoSeries' object has no attribute 'assign'
我做错了什么?
这真的简化为 sjoin()
。下面的示例显示了哪些城市 (pnts) 位于哪些国家 (polys)
import geopandas as gpd
pnts = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
polys = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
pnts.sjoin(polys)
我有两个 pandas 数据框和 geopandas 数组
第一个,pnts
0 POINT (96.69866 5.20478)
1 POINT (97.92708 2.34781)
2 POINT (96.12920 4.13244)
3 POINT (96.85947 4.71685)
4 POINT (97.95656 4.47959)
...
2922 POINT (136.06094 -1.17746)
2923 POINT (140.70931 -2.56853)
2924 POINT (138.31903 -2.37457)
2925 POINT (140.62557 -4.90224)
2926 POINT (136.07973 -1.15792)
Name: geometry, Length: 2927, dtype: geometry
第二个,polys
0 POLYGON ((104.82248 -2.98102, 104.82165 -2.985...
1 POLYGON ((104.75516 -3.01316, 104.75480 -3.013...
2 POLYGON ((104.77081 -2.98185, 104.77082 -2.981...
3 POLYGON ((104.77081 -2.98185, 104.77070 -2.981...
4 POLYGON ((104.77317 -2.99340, 104.77316 -2.993...
...
81907 POLYGON ((138.79489 -4.09306, 138.78723 -4.096...
81908 POLYGON ((136.88606 -3.76603, 136.86576 -3.772...
81909 POLYGON ((121.51784 -8.76968, 121.51784 -8.769...
81910 POLYGON ((122.69762 0.87165, 122.69698 0.87106...
81911 POLYGON ((97.48882 1.09282, 97.48853 1.09108, ...
Name: geometry, Length: 81912, dtype: geometry
我想做的是,知道哪个多边形gdf['geometry']
包括
我做的是
pnts = pnts.assign(**{key: pnts.within(geom) for key, geom in polys.items()})
错误
<ipython-input-39-aaf67bdae48e> in <module>
----> 3 pnts = pnts.assign(**{key: pnts.within(geom) for key, geom in polys.items()})
~/.local/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
5139 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5140 return self[name]
-> 5141 return object.__getattribute__(self, name)
5142
5143 def __setattr__(self, name: str, value) -> None:
AttributeError: 'GeoSeries' object has no attribute 'assign'
我做错了什么?
这真的简化为 sjoin()
。下面的示例显示了哪些城市 (pnts) 位于哪些国家 (polys)
import geopandas as gpd
pnts = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
polys = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
pnts.sjoin(polys)