简体   繁体   English

计算多边形中的点并将结果写入(地理)数据帧

[英]Count Points in Polygon and write result to (Geo)Dataframe

I want to count how many points there are per Polygon我想计算每个多边形有多少点

# Credits of this code go to: https://stackoverflow.com/questions/69642668/the-indices-of-the-two-geoseries-are-different-understanding-indices/69644010#69644010
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

Now I sjoin the two.现在我sjoin两个。 I use df_poly first, in order to add the points dfp to the GeoDataframe df_poly .我首先使用df_poly ,以便将点dfp添加到GeoDataframe df_poly

df_poly.sjoin(dfp)

Now I want to count how many points there are per polygon .现在我想计算每个polygon有多少points I thought我想

df_poly.sjoin(dfp).groupby('OBJECTID').count()

But that does not add a column to the GeoDataframe df_poly with the count of each group .但这不会向GeoDataframe df_poly添加一column ,其中GeoDataframe每个groupcount

You need to add one of the columns from the output of count() back into the original DataFrame using merge.您需要使用合并将count()的输出中的一列添加回原始 DataFrame。 I have used the geometry column and renamed it to n_points :我使用了几何列并将其重命名为n_points

df_poly.merge(
    df_poly.sjoin(
        dfp
    ).groupby(
        'OBJECTID'
    ).count().geometry.rename(
        'n_points'
    ).reset_index())

This is a follow on to this question The indices of the two GeoSeries are different - Understanding Indices这是这个问题后续两个 GeoSeries 的索引不同 - 理解索引

  • right_index of spatial join gives index of polygon as polygon was on right of spatial join空间连接的right_index给出多边形的索引,因为多边形位于空间连接的右侧
  • hence the series gpd.sjoin(dfp, df_poly).groupby("index_right").size().rename("points") can then be simply joined to the polygon GeoDataFrame to give how many points were found因此系列gpd.sjoin(dfp, df_poly).groupby("index_right").size().rename("points")然后可以简单地加入多边形GeoDataFrame以给出找到的点数
  • note how="left" to ensure it's a left join, not an inner join.注意how="left"以确保它是左连接,而不是内部连接。 Any polygons with no points with have NaN you may want to fillna(0) in this case.在这种情况下,任何没有点的多边形都有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",
)

Building on the answere Fergus McClean provided, this can even be done in less code:基于 Fergus McClean 提供的答案,这甚至可以用更少的代码完成:

df_poly.merge(df_poly.sjoin(dfp).groupby('OBJECTID').size().rename('n_points').reset_index())

However, the method ( .join() ) proposed by Rob Raymond to combine the two dataframes keeps the entries that have no count.然而,Rob Raymond 提出的方法 ( .join() ) 将两个dataframes结合起来,保留了没有计数的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM