计算多边形中的点并将结果写入 (Geo)Dataframe
Count Points in Polygon and write result to (Geo)Dataframe
我想计算每个多边形有多少个点
# Credits of this code go to:
import pandas as pd
import numpy as np
import geopandas as gpd
import shapely.geometry
import requests
# source some points and polygons
# fmt: off
dfp = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0]
dfp = gpd.GeoDataFrame(dfp, geometry=dfp.loc[:,["Longitude", "Latitude",]].apply(shapely.geometry.Point, axis=1))
res = requests.get("https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson")
df_poly = gpd.GeoDataFrame.from_features(res.json())
# fmt: on
现在我sjoin
两个。我首先使用 df_poly
,以便将点 dfp
添加到 GeoDataframe
df_poly
。
df_poly.sjoin(dfp)
现在我想数一数每个 polygon
有多少 points
。
我以为
df_poly.sjoin(dfp).groupby('OBJECTID').count()
但这并没有将 column
添加到 GeoDataframe
df_poly
以及每个 group
的 count
。
您需要使用合并将 count()
的输出中的一列添加回原始 DataFrame。我使用了几何列并将其重命名为 n_points
:
df_poly.merge(
df_poly.sjoin(
dfp
).groupby(
'OBJECTID'
).count().geometry.rename(
'n_points'
).reset_index())
这是这个问题的后续
空间连接的 - right_index 给出了多边形的索引,因为多边形位于空间连接的右侧
- 因此可以将系列
gpd.sjoin(dfp, df_poly).groupby("index_right").size().rename("points")
简单地加入多边形 GeoDataFrame 以给出找到了多少个点
- 注意
how="left"
以确保它是左联接,而不是内联接。任何没有点的多边形都有 NaN
在这种情况下你可能想要 fillna(0)
。
import pandas as pd
import numpy as np
import geopandas as gpd
import shapely.geometry
import requests
# source some points and polygons
# fmt: off
dfp = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0]
dfp = pd.concat([dfp,dfp]).reset_index(drop=True)
dfp = gpd.GeoDataFrame(dfp, geometry=dfp.loc[:,["Longitude", "Latitude",]].apply(shapely.geometry.Point, axis=1))
res = requests.get("https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson")
df_poly = gpd.GeoDataFrame.from_features(res.json())
# fmt: on
df_poly.join(
gpd.sjoin(dfp, df_poly).groupby("index_right").size().rename("points"),
how="left",
)
基于 Fergus McClean 提供的答案,这甚至可以用更少的代码完成:
df_poly.merge(df_poly.sjoin(dfp).groupby('OBJECTID').size().rename('n_points').reset_index())
然而,Rob Raymond 提出的方法(.join()
)将两者结合dataframes
保留了没有计数的条目。
我想计算每个多边形有多少个点
# Credits of this code go to:
import pandas as pd
import numpy as np
import geopandas as gpd
import shapely.geometry
import requests
# source some points and polygons
# fmt: off
dfp = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0]
dfp = gpd.GeoDataFrame(dfp, geometry=dfp.loc[:,["Longitude", "Latitude",]].apply(shapely.geometry.Point, axis=1))
res = requests.get("https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson")
df_poly = gpd.GeoDataFrame.from_features(res.json())
# fmt: on
现在我sjoin
两个。我首先使用 df_poly
,以便将点 dfp
添加到 GeoDataframe
df_poly
。
df_poly.sjoin(dfp)
现在我想数一数每个 polygon
有多少 points
。
我以为
df_poly.sjoin(dfp).groupby('OBJECTID').count()
但这并没有将 column
添加到 GeoDataframe
df_poly
以及每个 group
的 count
。
您需要使用合并将 count()
的输出中的一列添加回原始 DataFrame。我使用了几何列并将其重命名为 n_points
:
df_poly.merge(
df_poly.sjoin(
dfp
).groupby(
'OBJECTID'
).count().geometry.rename(
'n_points'
).reset_index())
这是这个问题的后续
-
空间连接的
- right_index 给出了多边形的索引,因为多边形位于空间连接的右侧
- 因此可以将系列
gpd.sjoin(dfp, df_poly).groupby("index_right").size().rename("points")
简单地加入多边形 GeoDataFrame 以给出找到了多少个点 - 注意
how="left"
以确保它是左联接,而不是内联接。任何没有点的多边形都有NaN
在这种情况下你可能想要fillna(0)
。
import pandas as pd
import numpy as np
import geopandas as gpd
import shapely.geometry
import requests
# source some points and polygons
# fmt: off
dfp = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0]
dfp = pd.concat([dfp,dfp]).reset_index(drop=True)
dfp = gpd.GeoDataFrame(dfp, geometry=dfp.loc[:,["Longitude", "Latitude",]].apply(shapely.geometry.Point, axis=1))
res = requests.get("https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson")
df_poly = gpd.GeoDataFrame.from_features(res.json())
# fmt: on
df_poly.join(
gpd.sjoin(dfp, df_poly).groupby("index_right").size().rename("points"),
how="left",
)
基于 Fergus McClean 提供的答案,这甚至可以用更少的代码完成:
df_poly.merge(df_poly.sjoin(dfp).groupby('OBJECTID').size().rename('n_points').reset_index())
然而,Rob Raymond 提出的方法(.join()
)将两者结合dataframes
保留了没有计数的条目。