我的Df如下所示:
position_latitude position_longitude geohash
0 53.398940 10.069293 u1
1 53.408875 10.052669 u1
2 48.856350 9.171759 u0
3 48.856068 9.170798 u0
4 48.856350 9.171759 u0
我想知道的是,使用基于Geohash的不同shapefile来接收距离此位置最近的节点
所以我想做的是,从文件中加载Geohash(exu1)中的ever group图形,然后在函数中使用该图形来获取最近的节点
我可以在for循环中完成,但是我认为有更有效的方法
我想到了这样的事情:
df['nearestNode'] = geoSub.apply(lambda x: getDistanceToEdge(x.position_latitude,x. position_longitude,x. geohash), axis=1)
但是,我不知道如何在每个组中只加载一次图形,因为从文件中获取它需要一些时间
到目前为止,我想到的是:
groupHashed = geoSub.groupby('geohash')
geoSub['distance'] = np.nan
for name, group in groupHashed:
G = osmnx.graph.graph_from_xml('geohash/'+name+'.osm', simplify=True, retain_all=False)
geoSub['distance'] = geoSub.apply(lambda x: getDistanceToEdge(x.position_latitude,x.position_longitude, G) if x.geohash == name, axis=1)
看起来确实有效,但我觉得如果情况严重,速度会急剧减慢
更新: 刚刚更新:
geoSub['distance'] = geoSub.apply(lambda x: getDistanceToEdge(x.position_latitude,x.position_longitude, G) if x.geohash == name, axis=1)
致:
geoSub['distance'] = geoSub[geoSub['geohash'] == name].apply(lambda x: getDistanceToEdge(x.position_latitude,x.position_longitude, G), axis=1)
现在快多了。还有更好的方法吗
您可以使用
transform
我正在存根
G
和getDistanceToEdge
(如x+y+geohash[-1]
),因此显示一个工作示例输出:
如您所见,您可以在函数
fun
中使用pos.values[0][-1]
获取组的名称。这是因为我们关心将pos
列构造为(lat、log、geohash)的元组,并且groupby之后的组中的每个geohash
都是相同的。因此,对于一个组,我们可以通过获取任何行的元组(pos
)的最后一个值来获取geohash
pos.values[0][-1]
给出第一行元组的最后一个值相关问题 更多 >
编程相关推荐