【发布时间】:2021-12-07 04:31:15
【问题描述】:
我想计算每个多边形有多少个点
# 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
现在我sjoin这两个。我首先使用df_poly,以便将点dfp 添加到GeoDataframe df_poly。
df_poly.sjoin(dfp)
现在我想计算每个polygon 有多少points。
我以为
df_poly.sjoin(dfp).groupby('OBJECTID').count()
但这不会将column 添加到GeoDataframe df_poly 以及每个count 的count。
【问题讨论】: