tile38/index/index.go

206 lines
4.9 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package index
2016-10-03 21:37:16 +03:00
import (
"math"
2017-08-11 03:32:40 +03:00
"unsafe"
2016-10-03 21:37:16 +03:00
"github.com/tidwall/tile38/index/rtree"
)
2016-03-05 02:08:16 +03:00
// Item represents an index item.
type Item interface {
2016-10-03 21:37:16 +03:00
Point() (x, y, z float64)
Rect() (minX, minY, minZ, maxX, maxY, maxZ float64)
2016-03-05 02:08:16 +03:00
}
// FlexItem can represent a point or a rectangle
type FlexItem struct {
2016-10-03 21:37:16 +03:00
MinX, MinY, MinZ, MaxX, MaxY, MaxZ float64
2016-03-05 02:08:16 +03:00
}
// Rect returns the rectangle
2016-10-03 21:37:16 +03:00
func (item *FlexItem) Rect() (minX, minY, minZ, maxX, maxY, maxZ float64) {
return item.MinX, item.MinY, item.MinZ, item.MaxX, item.MaxY, item.MaxZ
2016-03-05 02:08:16 +03:00
}
// Point returns the point
2016-10-03 21:37:16 +03:00
func (item *FlexItem) Point() (x, y, z float64) {
return item.MinX, item.MinY, item.MinZ
2016-03-05 02:08:16 +03:00
}
// Index is a geospatial index
type Index struct {
2016-08-19 17:47:39 +03:00
r *rtree.RTree
nr map[*rtree.Rect]Item // normalized points
nrr map[Item][]*rtree.Rect // normalized points
2017-08-11 03:32:40 +03:00
mulm map[interface{}]bool // store items that contain multiple rects
2016-03-05 02:08:16 +03:00
}
// New create a new index
func New() *Index {
return &Index{
r: rtree.New(),
2017-08-11 03:32:40 +03:00
mulm: make(map[interface{}]bool),
2016-03-05 02:08:16 +03:00
nr: make(map[*rtree.Rect]Item),
nrr: make(map[Item][]*rtree.Rect),
}
}
// Insert inserts an item into the index
func (ix *Index) Insert(item Item) {
2016-10-03 21:37:16 +03:00
minX, minY, minZ, maxX, maxY, maxZ := item.Rect()
2016-03-05 02:08:16 +03:00
if minX == maxX && minY == maxY {
x, y, normd := normPoint(minY, minX)
if normd {
2016-10-03 21:37:16 +03:00
nitem := &rtree.Rect{MinX: x, MinY: y, MinZ: minZ, MaxX: x, MaxY: y, MaxZ: maxZ}
2016-08-19 17:47:39 +03:00
ix.nr[nitem] = item
ix.nrr[item] = []*rtree.Rect{nitem}
ix.r.Insert(nitem)
2016-03-05 02:08:16 +03:00
} else {
2016-08-19 17:47:39 +03:00
ix.r.Insert(item)
2016-03-05 02:08:16 +03:00
}
} else {
mins, maxs, normd := normRect(minY, minX, maxY, maxX)
if normd {
var nitems []*rtree.Rect
for i := range mins {
minX, minY, maxX, maxY := mins[i][0], mins[i][1], maxs[i][0], maxs[i][1]
2016-10-03 21:37:16 +03:00
nitem := &rtree.Rect{MinX: minX, MinY: minY, MinZ: minZ, MaxX: maxX, MaxY: maxY, MaxZ: maxZ}
2016-03-05 02:08:16 +03:00
ix.nr[nitem] = item
nitems = append(nitems, nitem)
ix.r.Insert(nitem)
}
ix.nrr[item] = nitems
if len(mins) > 1 {
ix.mulm[item] = true
}
} else {
ix.r.Insert(item)
}
}
return
}
// Remove removed an item from the index
func (ix *Index) Remove(item Item) {
2016-08-19 17:47:39 +03:00
if nitems, ok := ix.nrr[item]; ok {
for _, nitem := range nitems {
ix.r.Remove(nitem)
delete(ix.nr, nitem)
2016-03-05 02:08:16 +03:00
}
2016-08-19 17:47:39 +03:00
delete(ix.nrr, item)
2016-03-05 02:08:16 +03:00
} else {
2016-08-19 17:47:39 +03:00
ix.r.Remove(item)
2016-03-05 02:08:16 +03:00
}
}
// Count counts all items in the index.
func (ix *Index) Count() int {
count := 0
2017-08-11 03:32:40 +03:00
ix.Search(-90, -180, 90, 180, math.Inf(-1), math.Inf(+1), func(_ interface{}) bool {
2016-03-05 02:08:16 +03:00
count++
return true
})
return count
}
// Bounds returns the minimum bounding rectangle of all items in the index.
2017-08-11 03:32:40 +03:00
func (ix *Index) Bounds() (MinX, MinY, MaxX, MaxY float64) {
return ix.r.Bounds()
}
2016-03-05 02:08:16 +03:00
// RemoveAll removes all items from the index.
func (ix *Index) RemoveAll() {
ix.r.RemoveAll()
}
2017-08-11 03:32:40 +03:00
type UintptrInterface struct {
Type uintptr
Ptr uintptr
}
type UnsafePointerInterface struct {
Type uintptr
Ptr unsafe.Pointer
}
func GetUintptrInterface(v interface{}) UintptrInterface {
return *(*UintptrInterface)(unsafe.Pointer(&v))
}
func GetUnsafePointerInterface(v interface{}) UnsafePointerInterface {
return *(*UnsafePointerInterface)(unsafe.Pointer(&v))
}
var rectType = func() uintptr {
var rrrr rtree.Rect
return GetUintptrInterface(&rrrr).Type
}()
func (ix *Index) getRTreeItem(item interface{}) interface{} {
uzi := GetUnsafePointerInterface(item)
if uzi.Type == rectType {
return ix.nr[(*rtree.Rect)(uzi.Ptr)]
2016-03-05 02:08:16 +03:00
}
2017-08-11 03:32:40 +03:00
return item
2016-03-05 02:08:16 +03:00
}
2017-08-11 03:32:40 +03:00
func (ix *Index) NearestNeighbors(lat, lon float64, iterator func(item interface{}) bool) bool {
x, y, _ := normPoint(lat, lon)
2017-08-11 03:32:40 +03:00
return ix.r.NearestNeighbors(x, y, func(item interface{}, dist float64) bool {
return iterator(ix.getRTreeItem(item))
})
}
2016-03-05 02:08:16 +03:00
// Search returns all items that intersect the bounding box.
2017-08-11 03:32:40 +03:00
func (ix *Index) Search(swLat, swLon, neLat, neLon, minZ, maxZ float64,
iterator func(item interface{}) bool,
) bool {
var keepon = true
2017-08-11 03:32:40 +03:00
var idm = make(map[interface{}]bool)
2016-03-05 02:08:16 +03:00
mins, maxs, _ := normRect(swLat, swLon, neLat, neLon)
// Points
if len(mins) == 1 {
// There is only one rectangle.
// It's possible that a r rect may span multiple entries. Check mulm map for spanning rects.
if keepon {
2017-08-11 03:32:40 +03:00
ix.r.Search(mins[0][0], mins[0][1], minZ, maxs[0][0], maxs[0][1], maxZ,
func(v interface{}) bool {
item := ix.getRTreeItem(v)
if len(ix.mulm) > 0 && ix.mulm[item] {
if !idm[item] {
idm[item] = true
keepon = iterator(item)
2016-03-05 02:08:16 +03:00
}
} else {
2017-08-11 03:32:40 +03:00
keepon = iterator(item)
2016-03-05 02:08:16 +03:00
}
2017-08-11 03:32:40 +03:00
return keepon
},
)
2016-03-05 02:08:16 +03:00
}
} else {
// There are multiple rectangles. Duplicates might occur.
for i := range mins {
if keepon {
2017-08-11 03:32:40 +03:00
ix.r.Search(mins[i][0], mins[i][1], minZ, maxs[i][0], maxs[i][1], maxZ,
func(item interface{}) bool {
iitm := ix.getRTreeItem(item)
if iitm != nil {
if ix.mulm[iitm] {
if !idm[iitm] {
idm[iitm] = true
keepon = iterator(iitm)
}
} else {
keepon = iterator(iitm)
2016-03-05 02:08:16 +03:00
}
}
2017-08-11 03:32:40 +03:00
return keepon
},
)
2016-03-05 02:08:16 +03:00
}
}
}
return keepon
2016-03-05 02:08:16 +03:00
}