空間相交是gis中常用的功能,一般就是使用分析庫進行相交就可以了,但是多數據時這樣產生笛卡爾積計算效率低下,這時可以考慮使用索引,本文中要介紹的是r-tree的使用。
R樹是一棵平衡樹。樹上有兩類結點:葉子結點和非葉子結點。每一個結點由若干個索引項構成。對於葉子結點,索引項形如(Index,Obj_ID)。其中,Index表示包圍空間數據對象的最小外接矩形MBR,Obj_ID標識一個空間數據對象。對於一個非葉子結點,它的索引項形如(Index,Child_Pointer)。 Child_Pointer 指向該結點的子結點。Index仍指一個矩形區域,該矩形區域包圍了子結點上所有索引項MBR的最小矩形區域。
jsts的STRtree創建,插入數據,查詢
let strtree=new jsts.index.strtree.STRtree()
const extent=item.getExtent()
strtree.insert(new jsts.geom.Envelope(extent.xmin, extent.xmax, extent.ymin, extent.ymax), count)
let re=strtree.query(new jsts.geom.Envelope(x, x+0.01, y, y+0.01))
rbush的創建插入數據查詢
const tree = new RBush();
const extent=item.getExtent()
const indexitem = {
minX: extent.xmin,
minY: extent.ymin,
maxX: extent.xmax,
maxY: extent.ymax,
index:count
};
tree.insert(indexitem);
let extent= {
minX: x,
minY: y,
maxX: x+0.01,
maxY: y+0.01
}
let mydata=tree.search(extent,test);
兩個索引的性能比較,224條相同的數據查詢相同數據下rbush更高效一些
RBush search | STRtree search |
---|---|
3.22ms | 16.65ms |
rbush中遞歸查找相交的葉子節點或者包含節點部分
search(bbox) {
let node = this.data;
const result = [];
if (!intersects(bbox, node)) return result;
const toBBox = this.toBBox;
const nodesToSearch = [];
while (node) {
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
const childBBox = node.leaf ? toBBox(child) : child;
if (intersects(bbox, childBBox)) {
if (node.leaf) result.push(child);
else if (contains(bbox, childBBox)) this._all(child, result);
else nodesToSearch.push(child);
}
}
node = nodesToSearch.pop();
}
return result;
}
參考資料:
https://blog.csdn.net/cdl2008sky/article/details/18217327
https://github.com/mourner/rbush
https://github.com/bjornharrtell/jsts
https://zhuanlan.zhihu.com/p/38597148
https://www.cnblogs.com/yinchuanqi/p/5607696.html
https://yq.aliyun.com/articles/322443
https://blog.csdn.net/cdl2008sky/article/details/18217327
https://www.cnblogs.com/naaoveGIS/p/6774549.html
https://blog.csdn.net/dickwang1229/article/details/39160511
https://www.jianshu.com/p/44d6281d1a01
https://blog.csdn.net/wzf1993/article/details/79547037
https://blog.csdn.net/MongChia1993/article/details/69941783#toc_6
https://blog.csdn.net/chenyq2008/article/details/2140477
https://www.cnblogs.com/arxive/p/8138586.html