package module
Version: v1.2.5 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2021 License: MIT Imports: 1 Imported by: 0




This package provides an in-memory R-Tree implementation for Go. It's designed for Tile38 and is optimized for fast rect inserts and replacements.




To start using rbang, install Go and run go get:

$ go get -u github.com/tidwall/rbang
Basic operations
// create a 2D RTree
var tr rbang.RTree

// insert a point
tr.Insert([2]float64{-112.0078, 33.4373}, [2]float64{-112.0078, 33.4373}, "PHX")

// insert a rect
tr.Insert([2]float64{10, 10}, [2]float64{20, 20}, "rect")

// search 
tr.Search([2]float64{-112.1, 33.4}, [2]float64{-112.0, 33.5}, 
 	func(min, max [2]float64, value interface{}) bool {
		println(value.(string)) // prints "PHX"

// delete 
tr.Delete([2]float64{-112.0078, 33.4373}, [2]float64{-112.0078, 33.4373}, "PHX")


This implementation is a variant of the original paper:


Same as the original algorithm. From the root to the leaf, the rects which will incur the least enlargment are chosen. Ties go to rects with the smallest area.


Same as the original algorithm. A target rect is deleted directly. When the number of children in a rect falls below it's minumum entries, it is removed from the tree and it's items are re-inserted.


This is a custom algorithm. It attempts to minimize intensive operations such as pre-sorting the children and comparing overlaps & area sizes. The desire is to do simple single axis distance calculations each child only once, with a target 50/50 chance that the child might be moved in-memory.

When a rect has reached it's max number of entries it's largest axis is calculated and the rect is split into two smaller rects, named left and right. Each child rects is then evaluated to determine which smaller rect it should be placed into. Two values, min-dist and max-dist, are calcuated for each child.

  • min-dist is the distance from the parent's minumum value of it's largest axis to the child's minumum value of the parent largest axis.
  • max-dist is the distance from the parent's maximum value of it's largest axis to the child's maximum value of the parent largest axis.

When the min-dist is less than max-dist then the child is placed into the left rect. When the max-dist is less than min-dist then the child is placed into the right rect. When the min-dist is equal to max-dist then the child is placed into an equal bucket until all of the children are evaluated. Each equal rect is then one-by-one placed in either left or right, whichever has less children.


rbang source code is available under the MIT License.




This section is empty.


This section is empty.


This section is empty.


type RTree

type RTree struct {
	// contains filtered or unexported fields

RTree ...

func (*RTree) Bounds

func (tr *RTree) Bounds() (min, max [2]float64)

Bounds returns the minimum bounding rect

func (*RTree) Children added in v1.0.0

func (tr *RTree) Children(
	parent interface{},
	reuse []child.Child,
) []child.Child

Children is a utility function that returns all children for parent node. If parent node is nil then the root nodes should be returned. The min, max, data, and items slices all must have the same lengths. And, each element from all slices must be associated. Returns true for `items` when the the item at the leaf level. The reuse buffers are empty length slices that can optionally be used to avoid extra allocations.

func (*RTree) Delete

func (tr *RTree) Delete(min, max [2]float64, data interface{})

Delete data from tree

func (*RTree) Insert

func (tr *RTree) Insert(min, max [2]float64, value interface{})

Insert inserts an item into the RTree

func (*RTree) Len added in v1.0.0

func (tr *RTree) Len() int

Len returns the number of items in tree

func (*RTree) Replace added in v1.2.0

func (tr *RTree) Replace(
	oldMin, oldMax [2]float64, oldData interface{},
	newMin, newMax [2]float64, newData interface{},

Replace an item. This is effectively just a Delete followed by an Insert. Which means the new item will always be inserted, whether or not the old item was deleted.

func (*RTree) Scan

func (tr *RTree) Scan(iter func(min, max [2]float64, data interface{}) bool)

Scan iterates through all data in tree.

func (*RTree) Search

func (tr *RTree) Search(
	min, max [2]float64,
	iter func(min, max [2]float64, value interface{}) bool,

Search ...

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL