2018-10-11 00:25:40 +03:00
|
|
|
package collection
|
|
|
|
|
|
|
|
import (
|
2019-02-13 22:43:38 +03:00
|
|
|
ifbtree "github.com/tidwall/btree"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/geojson"
|
2018-11-06 13:40:52 +03:00
|
|
|
"github.com/tidwall/geojson/geo"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/geojson/geometry"
|
2019-02-13 22:43:38 +03:00
|
|
|
"github.com/tidwall/tile38/internal/collection/btree"
|
|
|
|
"github.com/tidwall/tile38/internal/collection/item"
|
|
|
|
"github.com/tidwall/tile38/internal/collection/rtree"
|
2018-10-11 00:25:40 +03:00
|
|
|
)
|
|
|
|
|
2018-11-02 16:09:56 +03:00
|
|
|
// Cursor allows for quickly paging through Scan, Within, Intersects, and Nearby
|
|
|
|
type Cursor interface {
|
|
|
|
Offset() uint64
|
|
|
|
Step(count uint64)
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// Collection represents a collection of geojson objects.
|
|
|
|
type Collection struct {
|
2019-02-13 22:43:38 +03:00
|
|
|
items btree.BTree // items sorted by keys
|
|
|
|
index rtree.BoxTree // items geospatially indexed
|
|
|
|
values *ifbtree.BTree // items sorted by value+key
|
2019-02-16 00:02:04 +03:00
|
|
|
packed bool
|
2019-02-12 01:27:46 +03:00
|
|
|
fieldMap map[string]int
|
|
|
|
weight int
|
|
|
|
points int
|
|
|
|
objects int // geometry count
|
|
|
|
nobjects int // non-geometry count
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var counter uint64
|
|
|
|
|
|
|
|
// New creates an empty collection
|
2019-02-16 00:02:04 +03:00
|
|
|
func New(packed bool) *Collection {
|
2018-10-11 00:25:40 +03:00
|
|
|
col := &Collection{
|
2019-02-16 00:02:04 +03:00
|
|
|
packed: packed,
|
2019-02-13 22:43:38 +03:00
|
|
|
values: ifbtree.New(16, nil),
|
2018-10-11 00:25:40 +03:00
|
|
|
fieldMap: make(map[string]int),
|
|
|
|
}
|
|
|
|
return col
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of objects in collection.
|
|
|
|
func (c *Collection) Count() int {
|
|
|
|
return c.objects + c.nobjects
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringCount returns the number of string values.
|
|
|
|
func (c *Collection) StringCount() int {
|
|
|
|
return c.nobjects
|
|
|
|
}
|
|
|
|
|
|
|
|
// PointCount returns the number of points (lat/lon coordinates) in collection.
|
|
|
|
func (c *Collection) PointCount() int {
|
|
|
|
return c.points
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalWeight calculates the in-memory cost of the collection in bytes.
|
|
|
|
func (c *Collection) TotalWeight() int {
|
|
|
|
return c.weight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds returns the bounds of all the items in the collection.
|
|
|
|
func (c *Collection) Bounds() (minX, minY, maxX, maxY float64) {
|
|
|
|
min, max := c.index.Bounds()
|
|
|
|
if len(min) >= 2 && len(max) >= 2 {
|
|
|
|
return min[0], min[1], max[0], max[1]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func objIsSpatial(obj geojson.Object) bool {
|
|
|
|
_, ok := obj.(geojson.Spatial)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:43:38 +03:00
|
|
|
func (c *Collection) addItem(item *item.Item) {
|
|
|
|
if objIsSpatial(item.Obj()) {
|
|
|
|
if !item.Obj().Empty() {
|
|
|
|
rect := item.Obj().Rect()
|
|
|
|
c.index.Insert(
|
|
|
|
[]float64{rect.Min.X, rect.Min.Y},
|
|
|
|
[]float64{rect.Max.X, rect.Max.Y},
|
|
|
|
item)
|
|
|
|
}
|
2019-02-12 01:27:46 +03:00
|
|
|
c.objects++
|
|
|
|
} else {
|
|
|
|
c.values.ReplaceOrInsert(item)
|
|
|
|
c.nobjects++
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
weight, points := item.WeightAndPoints()
|
2019-02-12 01:27:46 +03:00
|
|
|
c.weight += weight
|
|
|
|
c.points += points
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:43:38 +03:00
|
|
|
func (c *Collection) delItem(item *item.Item) {
|
|
|
|
if objIsSpatial(item.Obj()) {
|
|
|
|
if !item.Obj().Empty() {
|
|
|
|
rect := item.Obj().Rect()
|
|
|
|
c.index.Delete(
|
|
|
|
[]float64{rect.Min.X, rect.Min.Y},
|
|
|
|
[]float64{rect.Max.X, rect.Max.Y},
|
|
|
|
item)
|
|
|
|
}
|
2019-02-12 01:27:46 +03:00
|
|
|
c.objects--
|
|
|
|
} else {
|
|
|
|
c.values.Delete(item)
|
|
|
|
c.nobjects--
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
weight, points := item.WeightAndPoints()
|
2019-02-12 01:27:46 +03:00
|
|
|
c.weight -= weight
|
|
|
|
c.points -= points
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// Set adds or replaces an object in the collection and returns the fields
|
|
|
|
// array. If an item with the same id is already in the collection then the
|
|
|
|
// new item will adopt the old item's fields.
|
|
|
|
// The fields argument is optional.
|
|
|
|
// The return values are the old object, the old fields, and the new fields
|
|
|
|
func (c *Collection) Set(
|
|
|
|
id string, obj geojson.Object, fields []string, values []float64,
|
|
|
|
) (
|
2019-02-15 18:26:55 +03:00
|
|
|
oldObj geojson.Object, oldFields *Fields, newFields *Fields,
|
2018-10-11 00:25:40 +03:00
|
|
|
) {
|
2019-02-13 01:24:22 +03:00
|
|
|
// create the new item
|
2019-02-16 00:02:04 +03:00
|
|
|
newItem := item.New(id, obj, c.packed)
|
2018-10-11 00:25:40 +03:00
|
|
|
|
|
|
|
// add the new item to main btree and remove the old one if needed
|
2019-02-15 18:26:55 +03:00
|
|
|
var oldItem *item.Item
|
|
|
|
oldItemV, ok := c.items.Set(newItem)
|
2018-10-11 00:25:40 +03:00
|
|
|
if ok {
|
2019-02-15 18:26:55 +03:00
|
|
|
oldItem = oldItemV
|
2019-02-13 22:43:38 +03:00
|
|
|
oldObj = oldItem.Obj()
|
2018-10-11 00:25:40 +03:00
|
|
|
|
2019-02-12 01:27:46 +03:00
|
|
|
// remove old item from indexes
|
|
|
|
c.delItem(oldItem)
|
2019-02-15 18:26:55 +03:00
|
|
|
if oldItem.HasFields() {
|
2019-02-12 01:27:46 +03:00
|
|
|
// merge old and new fields
|
2019-02-15 18:26:55 +03:00
|
|
|
newItem.CopyOverFields(oldItem)
|
2019-02-12 01:27:46 +03:00
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
2019-02-12 01:27:46 +03:00
|
|
|
if fields == nil && len(values) > 0 {
|
|
|
|
// directly set the field values, from copy
|
2019-02-15 18:26:55 +03:00
|
|
|
newItem.CopyOverFields(values)
|
2019-02-12 01:27:46 +03:00
|
|
|
} else if len(fields) > 0 {
|
|
|
|
// add new field to new item
|
2019-02-15 18:26:55 +03:00
|
|
|
c.setFields(newItem, fields, values, false)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
2019-02-12 01:27:46 +03:00
|
|
|
|
|
|
|
// add new item to indexes
|
2019-02-15 18:26:55 +03:00
|
|
|
c.addItem(newItem)
|
2019-02-13 01:24:22 +03:00
|
|
|
// fmt.Printf("!!! %#v\n", oldObj)
|
2019-02-12 01:27:46 +03:00
|
|
|
|
2019-02-15 18:26:55 +03:00
|
|
|
return oldObj, itemFields(oldItem), itemFields(newItem)
|
2019-02-13 22:43:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) setFields(
|
|
|
|
item *item.Item, fieldNames []string, fieldValues []float64, updateWeight bool,
|
|
|
|
) (updatedCount int) {
|
|
|
|
for i, fieldName := range fieldNames {
|
|
|
|
var fieldValue float64
|
|
|
|
if i < len(fieldValues) {
|
|
|
|
fieldValue = fieldValues[i]
|
|
|
|
}
|
|
|
|
if c.setField(item, fieldName, fieldValue, updateWeight) {
|
|
|
|
updatedCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return updatedCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) setField(
|
|
|
|
item *item.Item, fieldName string, fieldValue float64, updateWeight bool,
|
|
|
|
) (updated bool) {
|
|
|
|
idx, ok := c.fieldMap[fieldName]
|
|
|
|
if !ok {
|
|
|
|
idx = len(c.fieldMap)
|
|
|
|
c.fieldMap[fieldName] = idx
|
|
|
|
}
|
|
|
|
var pweight int
|
|
|
|
if updateWeight {
|
|
|
|
pweight, _ = item.WeightAndPoints()
|
|
|
|
}
|
|
|
|
updated = item.SetField(idx, fieldValue)
|
|
|
|
if updateWeight && updated {
|
|
|
|
nweight, _ := item.WeightAndPoints()
|
|
|
|
c.weight = c.weight - pweight + nweight
|
|
|
|
}
|
|
|
|
return updated
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes an object and returns it.
|
|
|
|
// If the object does not exist then the 'ok' return value will be false.
|
|
|
|
func (c *Collection) Delete(id string) (
|
2019-02-15 18:26:55 +03:00
|
|
|
obj geojson.Object, fields *Fields, ok bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) {
|
|
|
|
oldItemV, ok := c.items.Delete(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, false
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
oldItem := oldItemV
|
2018-10-11 00:25:40 +03:00
|
|
|
|
2019-02-12 01:27:46 +03:00
|
|
|
c.delItem(oldItem)
|
|
|
|
|
2019-02-15 18:26:55 +03:00
|
|
|
return oldItem.Obj(), itemFields(oldItem), true
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns an object.
|
|
|
|
// If the object does not exist then the 'ok' return value will be false.
|
|
|
|
func (c *Collection) Get(id string) (
|
2019-02-15 18:26:55 +03:00
|
|
|
obj geojson.Object, fields *Fields, ok bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) {
|
|
|
|
itemV, ok := c.items.Get(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, false
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
item := itemV
|
2019-02-12 01:27:46 +03:00
|
|
|
|
2019-02-15 18:26:55 +03:00
|
|
|
return item.Obj(), itemFields(item), true
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetField set a field value for an object and returns that object.
|
|
|
|
// If the object does not exist then the 'ok' return value will be false.
|
2019-02-12 01:27:46 +03:00
|
|
|
func (c *Collection) SetField(id, fieldName string, fieldValue float64) (
|
2019-02-15 18:26:55 +03:00
|
|
|
obj geojson.Object, fields *Fields, updated bool, ok bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) {
|
|
|
|
itemV, ok := c.items.Get(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, false, false
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
item := itemV
|
2019-02-12 01:27:46 +03:00
|
|
|
updated = c.setField(item, fieldName, fieldValue, true)
|
2019-02-15 18:26:55 +03:00
|
|
|
return item.Obj(), itemFields(item), updated, true
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFields is similar to SetField, just setting multiple fields at once
|
|
|
|
func (c *Collection) SetFields(
|
2019-02-12 01:27:46 +03:00
|
|
|
id string, fieldNames []string, fieldValues []float64,
|
2019-02-15 18:26:55 +03:00
|
|
|
) (obj geojson.Object, fields *Fields, updatedCount int, ok bool) {
|
2018-10-11 00:25:40 +03:00
|
|
|
itemV, ok := c.items.Get(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, 0, false
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
item := itemV
|
2019-02-12 01:27:46 +03:00
|
|
|
|
|
|
|
updatedCount = c.setFields(item, fieldNames, fieldValues, true)
|
|
|
|
|
2019-02-15 18:26:55 +03:00
|
|
|
return item.Obj(), itemFields(item), updatedCount, true
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FieldMap return a maps of the field names.
|
|
|
|
func (c *Collection) FieldMap() map[string]int {
|
|
|
|
return c.fieldMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldArr return an array representation of the field names.
|
|
|
|
func (c *Collection) FieldArr() []string {
|
|
|
|
arr := make([]string, len(c.fieldMap))
|
|
|
|
for field, i := range c.fieldMap {
|
|
|
|
arr[i] = field
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan iterates though the collection ids.
|
2018-11-02 16:09:56 +03:00
|
|
|
func (c *Collection) Scan(desc bool, cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
iter := func(item *item.Item) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2019-02-15 18:26:55 +03:00
|
|
|
keepon = iterator(item.ID(), item.Obj(), itemFields(item))
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
if desc {
|
|
|
|
c.items.Reverse(iter)
|
|
|
|
} else {
|
|
|
|
c.items.Scan(iter)
|
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanRange iterates though the collection starting with specified id.
|
2018-11-02 16:09:56 +03:00
|
|
|
func (c *Collection) ScanRange(start, end string, desc bool, cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
iter := func(item *item.Item) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if !desc {
|
2019-02-13 22:43:38 +03:00
|
|
|
if item.ID() >= end {
|
2018-10-11 00:25:40 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-13 22:43:38 +03:00
|
|
|
if item.ID() <= end {
|
2018-10-11 00:25:40 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 18:26:55 +03:00
|
|
|
keepon = iterator(item.ID(), item.Obj(), itemFields(item))
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
if desc {
|
|
|
|
c.items.Descend(start, iter)
|
|
|
|
} else {
|
|
|
|
c.items.Ascend(start, iter)
|
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchValues iterates though the collection values.
|
2018-11-02 16:09:56 +03:00
|
|
|
func (c *Collection) SearchValues(desc bool, cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
iter := func(v ifbtree.Item) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
iitm := v.(*item.Item)
|
2019-02-15 18:26:55 +03:00
|
|
|
keepon = iterator(iitm.ID(), iitm.Obj(), itemFields(iitm))
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
if desc {
|
|
|
|
c.values.Descend(iter)
|
|
|
|
} else {
|
|
|
|
c.values.Ascend(iter)
|
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchValuesRange iterates though the collection values.
|
|
|
|
func (c *Collection) SearchValuesRange(start, end string, desc bool,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
iter := func(v ifbtree.Item) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
iitm := v.(*item.Item)
|
2019-02-15 18:26:55 +03:00
|
|
|
keepon = iterator(iitm.ID(), iitm.Obj(), itemFields(iitm))
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
if desc {
|
2019-02-12 22:06:24 +03:00
|
|
|
c.values.DescendRange(
|
2019-02-16 00:02:04 +03:00
|
|
|
item.New("", String(start), false),
|
|
|
|
item.New("", String(end), false),
|
|
|
|
iter,
|
2019-02-12 22:06:24 +03:00
|
|
|
)
|
2018-10-11 00:25:40 +03:00
|
|
|
} else {
|
2019-02-12 22:06:24 +03:00
|
|
|
c.values.AscendRange(
|
2019-02-16 00:02:04 +03:00
|
|
|
item.New("", String(start), false),
|
|
|
|
item.New("", String(end), false),
|
|
|
|
iter,
|
2019-02-12 22:06:24 +03:00
|
|
|
)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanGreaterOrEqual iterates though the collection starting with specified id.
|
|
|
|
func (c *Collection) ScanGreaterOrEqual(id string, desc bool,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-02 16:09:56 +03:00
|
|
|
var count uint64
|
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
iter := func(item *item.Item) bool {
|
2018-11-02 16:09:56 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2019-02-15 18:26:55 +03:00
|
|
|
keepon = iterator(item.ID(), item.Obj(), itemFields(item))
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
if desc {
|
|
|
|
c.items.Descend(id, iter)
|
|
|
|
} else {
|
|
|
|
c.items.Ascend(id, iter)
|
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) geoSearch(
|
|
|
|
rect geometry.Rect,
|
2019-02-15 18:26:55 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
alive := true
|
|
|
|
c.index.Search(
|
|
|
|
[]float64{rect.Min.X, rect.Min.Y},
|
|
|
|
[]float64{rect.Max.X, rect.Max.Y},
|
2019-02-13 22:43:38 +03:00
|
|
|
func(_, _ []float64, itemv *item.Item) bool {
|
|
|
|
item := itemv
|
2019-02-15 18:26:55 +03:00
|
|
|
alive = iter(item.ID(), item.Obj(), itemFields(item))
|
2018-10-11 00:25:40 +03:00
|
|
|
return alive
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) geoSparse(
|
|
|
|
obj geojson.Object, sparse uint8,
|
2019-02-15 18:26:55 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields *Fields) (match, ok bool),
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
matches := make(map[string]bool)
|
|
|
|
alive := true
|
|
|
|
c.geoSparseInner(obj.Rect(), sparse,
|
2019-02-15 18:26:55 +03:00
|
|
|
func(id string, o geojson.Object, fields *Fields) (
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok bool,
|
|
|
|
) {
|
|
|
|
ok = true
|
|
|
|
if !matches[id] {
|
|
|
|
match, ok = iter(id, o, fields)
|
|
|
|
if match {
|
|
|
|
matches[id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match, ok
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|
|
|
|
func (c *Collection) geoSparseInner(
|
|
|
|
rect geometry.Rect, sparse uint8,
|
2019-02-15 18:26:55 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields *Fields) (match, ok bool),
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
if sparse > 0 {
|
|
|
|
w := rect.Max.X - rect.Min.X
|
|
|
|
h := rect.Max.Y - rect.Min.Y
|
|
|
|
quads := [4]geometry.Rect{
|
|
|
|
geometry.Rect{
|
|
|
|
Min: geometry.Point{X: rect.Min.X, Y: rect.Min.Y + h/2},
|
|
|
|
Max: geometry.Point{X: rect.Min.X + w/2, Y: rect.Max.Y},
|
|
|
|
},
|
|
|
|
geometry.Rect{
|
|
|
|
Min: geometry.Point{X: rect.Min.X + w/2, Y: rect.Min.Y + h/2},
|
|
|
|
Max: geometry.Point{X: rect.Max.X, Y: rect.Max.Y},
|
|
|
|
},
|
|
|
|
geometry.Rect{
|
|
|
|
Min: geometry.Point{X: rect.Min.X, Y: rect.Min.Y},
|
|
|
|
Max: geometry.Point{X: rect.Min.X + w/2, Y: rect.Min.Y + h/2},
|
|
|
|
},
|
|
|
|
geometry.Rect{
|
|
|
|
Min: geometry.Point{X: rect.Min.X + w/2, Y: rect.Min.Y},
|
|
|
|
Max: geometry.Point{X: rect.Max.X, Y: rect.Min.Y + h/2},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, quad := range quads {
|
|
|
|
if !c.geoSparseInner(quad, sparse-1, iter) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
alive := true
|
|
|
|
c.geoSearch(rect,
|
2019-02-15 18:26:55 +03:00
|
|
|
func(id string, obj geojson.Object, fields *Fields) bool {
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok := iter(id, obj, fields)
|
|
|
|
if !ok {
|
|
|
|
alive = false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !match
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|
|
|
|
|
|
|
|
// Within returns all object that are fully contained within an object or
|
|
|
|
// bounding box. Set obj to nil in order to use the bounding box.
|
|
|
|
func (c *Collection) Within(
|
2018-11-01 01:01:55 +03:00
|
|
|
obj geojson.Object,
|
|
|
|
sparse uint8,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if sparse > 0 {
|
|
|
|
return c.geoSparse(obj, sparse,
|
2019-02-15 18:26:55 +03:00
|
|
|
func(id string, o geojson.Object, fields *Fields) (
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok bool,
|
|
|
|
) {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return false, true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if match = o.Within(obj); match {
|
|
|
|
ok = iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return match, ok
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return c.geoSearch(obj.Rect(),
|
2019-02-15 18:26:55 +03:00
|
|
|
func(id string, o geojson.Object, fields *Fields) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if o.Within(obj) {
|
|
|
|
return iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersects returns all object that are intersect an object or bounding box.
|
|
|
|
// Set obj to nil in order to use the bounding box.
|
|
|
|
func (c *Collection) Intersects(
|
2018-11-01 01:01:55 +03:00
|
|
|
obj geojson.Object,
|
|
|
|
sparse uint8,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if sparse > 0 {
|
|
|
|
return c.geoSparse(obj, sparse,
|
2019-02-15 18:26:55 +03:00
|
|
|
func(id string, o geojson.Object, fields *Fields) (
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok bool,
|
|
|
|
) {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return false, true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if match = o.Intersects(obj); match {
|
|
|
|
ok = iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return match, ok
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return c.geoSearch(obj.Rect(),
|
2019-02-15 18:26:55 +03:00
|
|
|
func(id string, o geojson.Object, fields *Fields) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if o.Intersects(obj) {
|
|
|
|
return iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nearby returns the nearest neighbors
|
|
|
|
func (c *Collection) Nearby(
|
|
|
|
target geojson.Object,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-02-15 18:26:55 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields *Fields) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
2018-11-11 16:26:23 +03:00
|
|
|
// First look to see if there's at least one candidate in the circle's
|
|
|
|
// outer rectangle. This is a fast-fail operation.
|
2018-11-06 13:40:52 +03:00
|
|
|
if circle, ok := target.(*geojson.Circle); ok {
|
|
|
|
meters := circle.Meters()
|
|
|
|
if meters > 0 {
|
|
|
|
center := circle.Center()
|
|
|
|
minLat, minLon, maxLat, maxLon :=
|
|
|
|
geo.RectFromCenter(center.Y, center.X, meters)
|
|
|
|
var exists bool
|
|
|
|
c.index.Search(
|
|
|
|
[]float64{minLon, minLat},
|
|
|
|
[]float64{maxLon, maxLat},
|
2019-02-13 22:43:38 +03:00
|
|
|
func(_, _ []float64, itemv *item.Item) bool {
|
2018-11-06 13:40:52 +03:00
|
|
|
exists = true
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if !exists {
|
2018-11-11 16:26:23 +03:00
|
|
|
// no candidates
|
2018-11-06 13:40:52 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 16:26:23 +03:00
|
|
|
// do the kNN operation
|
2018-10-11 00:25:40 +03:00
|
|
|
alive := true
|
|
|
|
center := target.Center()
|
2018-11-01 01:01:55 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
c.index.Nearby(
|
|
|
|
[]float64{center.X, center.Y},
|
|
|
|
[]float64{center.X, center.Y},
|
2019-02-13 22:43:38 +03:00
|
|
|
func(_, _ []float64, itemv *item.Item) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2018-11-02 16:09:56 +03:00
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
2019-02-13 22:43:38 +03:00
|
|
|
item := itemv
|
2019-02-15 18:26:55 +03:00
|
|
|
alive = iter(item.ID(), item.Obj(), itemFields(item))
|
2018-10-11 00:25:40 +03:00
|
|
|
return alive
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|