alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【553】rtree,R树 相关

参考:Rtree 0.9.7 文档 » 教程

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


  图解 R-Tree

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

 

  举例:将路网数据构建 rtree

1. 获取路网数据

1
2
3
4
5
import pandas as pd
 
fp = "../../02-Data/02_Road/shanghai_road_ind_7948.txt"
df = pd.read_csv(fp, sep='\t')
df.head()

  显示

1
2
3
4
5
6
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 里面

1
2
3
4
5
6
7
8
9
10
11
12
13
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. 获取某一个路网折线相交的路网

1
2
3
4
5
6
7
8
9
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'])

  显示

1
2
3
4
5
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. 获取其他信息

1
2
3
4
5
6
7
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()

  显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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   McDelfino  阅读(662)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-04-23 【312】◀▶ arcpy 常用函数说明
点击右上角即可分享
微信分享提示