【发布时间】:2019-12-16 15:17:31
【问题描述】:
我有 2 个熊猫数据框。第一个包含站点的经纬度信息,只有 3 行:
stat_id stat_lon stat_lat
0 db_695203 9.444328 54.787590
1 db_699007 9.438629 54.789577
2 db_695442 9.445865 54.786215
第二个包含列'Polygon'(匀称的多边形格式)并且有20行:
0 POLYGON ((9.444721146384639 54.78805404001241,...
1 POLYGON ((9.429828147969117 54.79003403977831,...
2 POLYGON ((9.429153147576411 54.78516304109078,...
.......................................................
18 POLYGON ((9.417355147148637 54.79108504035977,...
19 POLYGON ((9.44272277037326 54.79218198146992, ...
我的目标是:
- 检查点(带有她的坐标的站)是否在多边形中(这个没问题)
- 计算一个多边形中的站数(这就是问题所在)
我该怎么办:
for j in range(len(piece_clean_data)): #it's a df which contains polygons
P = shapely.wkt.loads(piece_clean_data.iloc[j,87]) #i convert string to Polygon
for i in range(len(three_stations)): #df with 3 stations
p1 = Point(three_stations.iloc[i,1], three_stations.iloc[i,2]) #station coordinates
st = P.contains(p1) #the answer is "True/False" - here i check, whether the point is in polygon or not
if st == 'True': #and here I don't have any idea.
所以,最后我想多写一列“多边形中的站点数”,例如:
0 POLYGON ((9.444721146384639 54.78805404001241,... 0
1 POLYGON ((9.429828147969117 54.79003403977831,... 0
2 POLYGON ((9.429153147576411 54.78516304109078,... 1
请问有什么想法吗?提前非常感谢!
【问题讨论】:
-
在这种情况下使用 GeoPandas 可能会更好。见Counting number of points in each grid?
标签: python pandas polygon shapely