alex_bn_lee

导航

【553】rtree,R树 相关

参考:Rtree 0.9.7 文档 » 教程

参考:python中Rtree使用方法的一些简单介绍


  图解 R-Tree

  现将矢量数据的边框信息存储到 rtree 里面,然后在查询相关信息的时候,直接通过查找对应的矩形就行,这样的话可以大大缩短搜索时间,也减少计算时间,然后通过对应的索引信息来获取具体的矢量数据。

 

  举例:将路网数据构建 rtree

1. 获取路网数据

import pandas as pd 

fp = "../../02-Data/02_Road/shanghai_road_ind_7948.txt"
df = pd.read_csv(fp, sep='\t') 
df.head() 

  显示

id	geom	tag	flag
0	850470721	LINESTRING (121.352874 31.09228, 121.352748 31...	{"road_grade": "7", "urban": "yes", "lanes:bac...	1
1	850470731	LINESTRING (121.353524 31.086087, 121.353945 3...	{"road_grade": "7", "urban": "yes", "lanes:bac...	1
2	850470732	LINESTRING (121.354285 31.085564, 121.354481 3...	{"road_grade": "7", "urban": "yes", "lanes:bac...	1
3	850470733	LINESTRING (121.35373 31.085535, 121.353767 31...	{"road_grade": "7", "urban": "yes", "lanes:bac...	1
4	850470734	LINESTRING (121.353488 31.085566, 121.35373 31...	{"road_grade": "7", "urban": "yes", "lanes:bac...	1

 

2. 遍历获取每个路网数据的 bounds,并 insert 到 rtree 里面

from rtree import index
from shapely.geometry import LineString 

idx = index.Index()

# 遍历每一条记录构建 rtree
for i in range(len(df)):
    line_str = df.loc[i]['geom']
    line_pts = [[float(pt.strip().split()[0]), float(pt.strip().split()[1])] for pt in line_str[12:-1].split(',')]
    line_shp = LineString(line_pts)

    # 添加索引值以及 bounds 
    idx.insert(i, line_shp.bounds) 

 

3. 获取某一个路网折线相交的路网

a = [[float(pt.strip().split()[0]), float(pt.strip().split()[1])] for pt in df.loc[0]['geom'][12:-1].split(',')]
b = LineString(a) 

# 获取与 b.bounds 相交的索引值
hits = idx.intersection(b.bounds) 

# 通过索引值获取对应的路网信息
for i in hits: 
    print(df.loc[i]['geom']) 

  显示

LINESTRING (121.352643 31.092467, 121.352716 31.09236, 121.352764 31.092281, 121.352792 31.092209)
LINESTRING (121.353627 31.09253, 121.352874 31.09228)
LINESTRING (121.352874 31.09228, 121.352748 31.092555)
LINESTRING (121.353481 31.092834, 121.352928 31.092622, 121.352748 31.092555)
LINESTRING (121.352748 31.092555, 121.352658 31.092751, 121.352663 31.092795, 121.352686 31.09282, 121.353346 31.093082)

 

4. 获取其他信息

hits = idx.intersection(b.bounds, objects=True) 

for o in hits: 
    print("id:", o.id)
    print("bounds", o.bbox) 
    print("geom:", df.loc[o.id]['geom'])
    print()

  显示

id: 540
bounds [121.352643, 31.092209, 121.352792, 31.092467]
geom: LINESTRING (121.352643 31.092467, 121.352716 31.09236, 121.352764 31.092281, 121.352792 31.092209)

id: 186
bounds [121.352874, 31.09228, 121.353627, 31.09253]
geom: LINESTRING (121.353627 31.09253, 121.352874 31.09228)

id: 0
bounds [121.352748, 31.09228, 121.352874, 31.092555]
geom: LINESTRING (121.352874 31.09228, 121.352748 31.092555)

id: 429
bounds [121.352748, 31.092555, 121.353481, 31.092834]
geom: LINESTRING (121.353481 31.092834, 121.352928 31.092622, 121.352748 31.092555)

id: 189
bounds [121.352658, 31.092555, 121.353346, 31.093082]
geom: LINESTRING (121.352748 31.092555, 121.352658 31.092751, 121.352663 31.092795, 121.352686 31.09282, 121.353346 31.093082)

 

posted on 2021-04-23 10:58  McDelfino  阅读(644)  评论(0编辑  收藏  举报