2016-03-05 02:08:16 +03:00
|
|
|
package collection
|
|
|
|
|
|
|
|
import (
|
2017-01-13 19:31:35 +03:00
|
|
|
"math"
|
|
|
|
|
2016-07-10 05:44:28 +03:00
|
|
|
"github.com/tidwall/btree"
|
2018-04-19 18:43:32 +03:00
|
|
|
"github.com/tidwall/tile38/pkg/geojson"
|
|
|
|
"github.com/tidwall/tile38/pkg/index"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
2016-07-10 05:44:28 +03:00
|
|
|
const (
|
|
|
|
idOrdered = 0
|
2016-07-10 23:23:50 +03:00
|
|
|
valueOrdered = 1
|
2016-07-10 05:44:28 +03:00
|
|
|
)
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
type itemT struct {
|
2016-05-24 15:06:19 +03:00
|
|
|
id string
|
|
|
|
object geojson.Object
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-09-12 07:25:09 +03:00
|
|
|
func (i *itemT) Less(item btree.Item, ctx interface{}) bool {
|
2016-07-10 05:44:28 +03:00
|
|
|
switch ctx {
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
case idOrdered:
|
|
|
|
return i.id < item.(*itemT).id
|
|
|
|
case valueOrdered:
|
2016-07-10 23:23:50 +03:00
|
|
|
i1, i2 := i.object.String(), item.(*itemT).object.String()
|
|
|
|
if i1 < i2 {
|
2016-07-10 05:44:28 +03:00
|
|
|
return true
|
|
|
|
}
|
2016-07-10 23:23:50 +03:00
|
|
|
if i1 > i2 {
|
2016-07-10 05:44:28 +03:00
|
|
|
return false
|
|
|
|
}
|
2016-07-10 23:23:50 +03:00
|
|
|
// the values match so we will compare the ids, which are always unique.
|
2016-07-10 05:44:28 +03:00
|
|
|
return i.id < item.(*itemT).id
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-10-03 21:37:16 +03:00
|
|
|
func (i *itemT) Rect() (minX, minY, minZ, maxX, maxY, maxZ float64) {
|
2016-05-24 15:06:19 +03:00
|
|
|
bbox := i.object.CalculatedBBox()
|
2016-10-03 21:37:16 +03:00
|
|
|
return bbox.Min.X, bbox.Min.Y, bbox.Min.Z, bbox.Max.X, bbox.Max.Y, bbox.Max.Z
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-10-03 21:37:16 +03:00
|
|
|
func (i *itemT) Point() (x, y, z float64) {
|
|
|
|
x, y, z, _, _, _ = i.Rect()
|
2016-03-05 02:08:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collection represents a collection of geojson objects.
|
|
|
|
type Collection struct {
|
2016-12-31 19:29:02 +03:00
|
|
|
items *btree.BTree // items sorted by keys
|
|
|
|
values *btree.BTree // items sorted by value+key
|
|
|
|
index *index.Index // items geospatially indexed
|
|
|
|
fieldMap map[string]int
|
|
|
|
fieldValues map[string][]float64
|
|
|
|
weight int
|
|
|
|
points int
|
|
|
|
objects int // geometry count
|
|
|
|
nobjects int // non-geometry count
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var counter uint64
|
|
|
|
|
|
|
|
// New creates an empty collection
|
|
|
|
func New() *Collection {
|
|
|
|
col := &Collection{
|
|
|
|
index: index.New(),
|
2016-12-31 19:29:02 +03:00
|
|
|
items: btree.New(128, idOrdered),
|
|
|
|
values: btree.New(128, valueOrdered),
|
2016-03-05 02:08:16 +03:00
|
|
|
fieldMap: make(map[string]int),
|
|
|
|
}
|
|
|
|
return col
|
|
|
|
}
|
|
|
|
|
2016-12-31 19:29:02 +03:00
|
|
|
func (c *Collection) setFieldValues(id string, values []float64) {
|
|
|
|
if c.fieldValues == nil {
|
|
|
|
c.fieldValues = make(map[string][]float64)
|
|
|
|
}
|
|
|
|
c.fieldValues[id] = values
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) getFieldValues(id string) (values []float64) {
|
|
|
|
if c.fieldValues == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.fieldValues[id]
|
|
|
|
}
|
|
|
|
func (c *Collection) deleteFieldValues(id string) {
|
|
|
|
if c.fieldValues != nil {
|
|
|
|
delete(c.fieldValues, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// Count returns the number of objects in collection.
|
2016-07-13 07:59:36 +03:00
|
|
|
func (c *Collection) Count() int {
|
|
|
|
return c.objects + c.nobjects
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-10-25 01:35:47 +03:00
|
|
|
// StringCount returns the number of string values.
|
|
|
|
func (c *Collection) StringCount() int {
|
|
|
|
return c.nobjects
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// 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 {
|
2016-07-10 05:44:28 +03:00
|
|
|
return c.weight
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-08-19 18:33:58 +03:00
|
|
|
// Bounds returns the bounds of all the items in the collection.
|
2017-08-11 03:32:40 +03:00
|
|
|
func (c *Collection) Bounds() (minX, minY, maxX, maxY float64) {
|
2016-08-19 18:33:58 +03:00
|
|
|
return c.index.Bounds()
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// ReplaceOrInsert 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) ReplaceOrInsert(id string, obj geojson.Object, fields []string, values []float64) (oldObject geojson.Object, oldFields []float64, newFields []float64) {
|
2016-12-31 04:12:18 +03:00
|
|
|
var oldItem *itemT
|
|
|
|
var newItem *itemT = &itemT{id: id, object: obj}
|
|
|
|
// add the new item to main btree and remove the old one if needed
|
|
|
|
oldItemPtr := c.items.ReplaceOrInsert(newItem)
|
|
|
|
if oldItemPtr != nil {
|
|
|
|
// the old item was removed, now let's remove from the rtree
|
|
|
|
// or strings tree.
|
|
|
|
oldItem = oldItemPtr.(*itemT)
|
|
|
|
if obj.IsGeometry() {
|
|
|
|
// geometry
|
|
|
|
c.index.Remove(oldItem)
|
|
|
|
c.objects--
|
|
|
|
} else {
|
|
|
|
// string
|
|
|
|
c.values.Delete(oldItem)
|
|
|
|
c.nobjects--
|
|
|
|
}
|
|
|
|
// decrement the point count
|
|
|
|
c.points -= oldItem.object.PositionCount()
|
|
|
|
|
|
|
|
// decrement the weights
|
2016-12-31 19:29:02 +03:00
|
|
|
c.weight -= len(c.getFieldValues(id)) * 8
|
2016-12-31 04:12:18 +03:00
|
|
|
c.weight -= oldItem.object.Weight() + len(oldItem.id)
|
|
|
|
|
|
|
|
// references
|
2016-05-24 15:06:19 +03:00
|
|
|
oldObject = oldItem.object
|
2016-12-31 19:29:02 +03:00
|
|
|
oldFields = c.getFieldValues(id)
|
|
|
|
newFields = oldFields
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-12-31 04:12:18 +03:00
|
|
|
// insert the new item into the rtree or strings tree.
|
|
|
|
if obj.IsGeometry() {
|
|
|
|
c.index.Insert(newItem)
|
|
|
|
c.objects++
|
|
|
|
} else {
|
|
|
|
c.values.ReplaceOrInsert(newItem)
|
|
|
|
c.nobjects++
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-12-31 04:12:18 +03:00
|
|
|
// increment the point count
|
|
|
|
c.points += obj.PositionCount()
|
2016-03-30 19:32:38 +03:00
|
|
|
|
2016-12-31 04:12:18 +03:00
|
|
|
// add the new weights
|
2016-12-31 19:29:02 +03:00
|
|
|
c.weight += len(newFields) * 8
|
2016-12-31 04:12:18 +03:00
|
|
|
c.weight += obj.Weight() + len(id)
|
|
|
|
if fields == nil {
|
|
|
|
if len(values) > 0 {
|
|
|
|
// directly set the field values, update weight
|
2016-12-31 19:29:02 +03:00
|
|
|
c.weight -= len(newFields) * 8
|
|
|
|
newFields = values
|
|
|
|
c.setFieldValues(id, newFields)
|
|
|
|
c.weight += len(newFields) * 8
|
2016-12-31 04:12:18 +03:00
|
|
|
}
|
2016-03-30 19:32:38 +03:00
|
|
|
} else {
|
2016-12-31 19:39:39 +03:00
|
|
|
//if len(fields) == 0 {
|
|
|
|
// panic("if fields is empty, make it nil")
|
|
|
|
//}
|
2016-03-30 19:32:38 +03:00
|
|
|
// map field name to value
|
|
|
|
for i, field := range fields {
|
2016-12-31 04:12:18 +03:00
|
|
|
c.setField(newItem, field, values[i])
|
2016-03-30 19:32:38 +03:00
|
|
|
}
|
2016-12-31 19:29:02 +03:00
|
|
|
newFields = c.getFieldValues(id)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-12-31 19:29:02 +03:00
|
|
|
return oldObject, oldFields, newFields
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 19:29:02 +03:00
|
|
|
// Remove removes an object and returns it.
|
|
|
|
// If the object does not exist then the 'ok' return value will be false.
|
|
|
|
func (c *Collection) Remove(id string) (obj geojson.Object, fields []float64, ok bool) {
|
2016-05-24 15:06:19 +03:00
|
|
|
i := c.items.Delete(&itemT{id: id})
|
2016-03-05 02:08:16 +03:00
|
|
|
if i == nil {
|
2016-12-31 19:29:02 +03:00
|
|
|
return nil, nil, false
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-12-31 19:29:02 +03:00
|
|
|
item := i.(*itemT)
|
2016-07-10 05:44:28 +03:00
|
|
|
if item.object.IsGeometry() {
|
|
|
|
c.index.Remove(item)
|
2016-07-10 23:23:50 +03:00
|
|
|
c.objects--
|
2016-07-10 05:44:28 +03:00
|
|
|
} else {
|
|
|
|
c.values.Delete(item)
|
2016-07-10 23:23:50 +03:00
|
|
|
c.nobjects--
|
2016-07-10 05:44:28 +03:00
|
|
|
}
|
2016-12-31 19:29:02 +03:00
|
|
|
fields = c.getFieldValues(id)
|
|
|
|
c.deleteFieldValues(id)
|
|
|
|
c.weight -= len(fields) * 8
|
2016-05-24 15:06:19 +03:00
|
|
|
c.weight -= item.object.Weight() + len(item.id)
|
|
|
|
c.points -= item.object.PositionCount()
|
2016-12-31 19:29:02 +03:00
|
|
|
return item.object, fields, true
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 19:29:02 +03:00
|
|
|
// Get returns an object.
|
2016-03-05 02:08:16 +03:00
|
|
|
// If the object does not exist then the 'ok' return value will be false.
|
2016-12-31 19:29:02 +03:00
|
|
|
func (c *Collection) Get(id string) (obj geojson.Object, fields []float64, ok bool) {
|
2016-05-24 15:06:19 +03:00
|
|
|
i := c.items.Get(&itemT{id: id})
|
2016-03-05 02:08:16 +03:00
|
|
|
if i == nil {
|
|
|
|
return nil, nil, false
|
|
|
|
}
|
|
|
|
item := i.(*itemT)
|
2016-12-31 19:29:02 +03:00
|
|
|
return item.object, c.getFieldValues(id), true
|
2016-03-05 02:08:16 +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.
|
2016-03-28 18:57:41 +03:00
|
|
|
func (c *Collection) SetField(id, field string, value float64) (obj geojson.Object, fields []float64, updated bool, ok bool) {
|
2016-05-24 15:06:19 +03:00
|
|
|
i := c.items.Get(&itemT{id: id})
|
2016-03-05 02:08:16 +03:00
|
|
|
if i == nil {
|
|
|
|
ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item := i.(*itemT)
|
2016-03-28 18:57:41 +03:00
|
|
|
updated = c.setField(item, field, value)
|
2016-12-31 19:29:02 +03:00
|
|
|
return item.object, c.getFieldValues(id), updated, true
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:50:03 +03:00
|
|
|
// SetFields is similar to SetField, just setting multiple fields at once
|
|
|
|
func (c *Collection) SetFields(id string, in_fields []string, in_values []float64) (
|
2017-11-11 01:31:38 +03:00
|
|
|
obj geojson.Object, fields []float64, updated_count int, ok bool,
|
2017-11-03 21:50:03 +03:00
|
|
|
) {
|
|
|
|
i := c.items.Get(&itemT{id: id})
|
|
|
|
if i == nil {
|
|
|
|
ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item := i.(*itemT)
|
|
|
|
for idx, field := range in_fields {
|
2017-11-11 01:31:38 +03:00
|
|
|
if c.setField(item, field, in_values[idx]) {
|
|
|
|
updated_count++
|
|
|
|
}
|
2017-11-03 21:50:03 +03:00
|
|
|
}
|
2017-11-11 01:31:38 +03:00
|
|
|
return item.object, c.getFieldValues(id), updated_count, true
|
2017-11-03 21:50:03 +03:00
|
|
|
}
|
|
|
|
|
2016-03-28 18:57:41 +03:00
|
|
|
func (c *Collection) setField(item *itemT, field string, value float64) (updated bool) {
|
2016-03-05 02:08:16 +03:00
|
|
|
idx, ok := c.fieldMap[field]
|
|
|
|
if !ok {
|
|
|
|
idx = len(c.fieldMap)
|
|
|
|
c.fieldMap[field] = idx
|
|
|
|
}
|
2016-12-31 19:29:02 +03:00
|
|
|
fields := c.getFieldValues(item.id)
|
|
|
|
c.weight -= len(fields) * 8
|
|
|
|
for idx >= len(fields) {
|
|
|
|
fields = append(fields, 0)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-12-31 19:29:02 +03:00
|
|
|
c.weight += len(fields) * 8
|
|
|
|
ovalue := fields[idx]
|
|
|
|
fields[idx] = value
|
|
|
|
c.setFieldValues(item.id, fields)
|
2016-03-28 18:57:41 +03:00
|
|
|
return ovalue != value
|
2016-03-05 02:08:16 +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
|
|
|
|
}
|
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
// Scan iterates though the collection ids.
|
|
|
|
func (c *Collection) Scan(desc bool,
|
2016-07-12 22:18:16 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields []float64) bool,
|
2017-07-24 18:26:10 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2016-07-12 22:18:16 +03:00
|
|
|
iter := func(item btree.Item) bool {
|
2017-07-24 18:26:10 +03:00
|
|
|
iitm := item.(*itemT)
|
|
|
|
keepon = iterator(iitm.id, iitm.object, c.getFieldValues(iitm.id))
|
|
|
|
return keepon
|
2016-07-12 22:18:16 +03:00
|
|
|
}
|
|
|
|
if desc {
|
|
|
|
c.items.Descend(iter)
|
|
|
|
} else {
|
|
|
|
c.items.Ascend(iter)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
// ScanGreaterOrEqual iterates though the collection starting with specified id.
|
|
|
|
func (c *Collection) ScanRange(start, end string, desc bool,
|
2016-07-12 22:18:16 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields []float64) bool,
|
2017-07-24 18:26:10 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2016-07-12 22:18:16 +03:00
|
|
|
iter := func(item btree.Item) bool {
|
2017-07-24 18:26:10 +03:00
|
|
|
iitm := item.(*itemT)
|
|
|
|
keepon = iterator(iitm.id, iitm.object, c.getFieldValues(iitm.id))
|
|
|
|
return keepon
|
2016-07-12 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if desc {
|
|
|
|
c.items.DescendRange(&itemT{id: start}, &itemT{id: end}, iter)
|
|
|
|
} else {
|
|
|
|
c.items.AscendRange(&itemT{id: start}, &itemT{id: end}, iter)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-07-12 22:18:16 +03:00
|
|
|
}
|
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
// SearchValues iterates though the collection values.
|
|
|
|
func (c *Collection) SearchValues(desc bool,
|
2016-07-13 06:11:02 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields []float64) bool,
|
2017-07-24 18:26:10 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2016-07-13 06:11:02 +03:00
|
|
|
iter := func(item btree.Item) bool {
|
2017-07-24 18:26:10 +03:00
|
|
|
iitm := item.(*itemT)
|
|
|
|
keepon = iterator(iitm.id, iitm.object, c.getFieldValues(iitm.id))
|
|
|
|
return keepon
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
|
|
|
if desc {
|
|
|
|
c.values.Descend(iter)
|
|
|
|
} else {
|
|
|
|
c.values.Ascend(iter)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
// SearchValuesRange iterates though the collection values.
|
|
|
|
func (c *Collection) SearchValuesRange(start, end string, desc bool,
|
2016-07-13 06:11:02 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields []float64) bool,
|
2017-07-24 18:26:10 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2016-07-13 06:11:02 +03:00
|
|
|
iter := func(item btree.Item) bool {
|
2017-07-24 18:26:10 +03:00
|
|
|
iitm := item.(*itemT)
|
|
|
|
keepon = iterator(iitm.id, iitm.object, c.getFieldValues(iitm.id))
|
|
|
|
return keepon
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
|
|
|
if desc {
|
|
|
|
c.values.DescendRange(&itemT{object: geojson.String(start)}, &itemT{object: geojson.String(end)}, iter)
|
|
|
|
} else {
|
|
|
|
c.values.AscendRange(&itemT{object: geojson.String(start)}, &itemT{object: geojson.String(end)}, iter)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-07-13 06:11:02 +03:00
|
|
|
}
|
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
// ScanGreaterOrEqual iterates though the collection starting with specified id.
|
|
|
|
func (c *Collection) ScanGreaterOrEqual(id string, desc bool,
|
2016-07-12 22:18:16 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields []float64) bool,
|
2017-07-24 18:26:10 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2016-07-12 22:18:16 +03:00
|
|
|
iter := func(item btree.Item) bool {
|
2017-07-24 18:26:10 +03:00
|
|
|
iitm := item.(*itemT)
|
|
|
|
keepon = iterator(iitm.id, iitm.object, c.getFieldValues(iitm.id))
|
|
|
|
return keepon
|
2016-07-12 22:18:16 +03:00
|
|
|
}
|
|
|
|
if desc {
|
|
|
|
c.items.DescendLessOrEqual(&itemT{id: id}, iter)
|
|
|
|
} else {
|
|
|
|
c.items.AscendGreaterOrEqual(&itemT{id: id}, iter)
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
func (c *Collection) geoSearch(bbox geojson.BBox, iterator func(id string, obj geojson.Object, fields []float64) bool) bool {
|
2017-08-11 03:32:40 +03:00
|
|
|
return c.index.Search(bbox.Min.Y, bbox.Min.X, bbox.Max.Y, bbox.Max.X, bbox.Min.Z, bbox.Max.Z, func(item interface{}) bool {
|
|
|
|
iitm := item.(*itemT)
|
2016-12-31 19:29:02 +03:00
|
|
|
if !iterator(iitm.id, iitm.object, c.getFieldValues(iitm.id)) {
|
2016-03-05 02:08:16 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nearby returns all object that are nearby a point.
|
2017-07-24 18:26:10 +03:00
|
|
|
func (c *Collection) Nearby(sparse uint8, lat, lon, meters, minZ, maxZ float64, iterator func(id string, obj geojson.Object, fields []float64) bool) bool {
|
|
|
|
var keepon = true
|
2016-03-05 02:08:16 +03:00
|
|
|
center := geojson.Position{X: lon, Y: lat, Z: 0}
|
|
|
|
bbox := geojson.BBoxesFromCenter(lat, lon, meters)
|
|
|
|
bboxes := bbox.Sparse(sparse)
|
|
|
|
if sparse > 0 {
|
|
|
|
for _, bbox := range bboxes {
|
2016-10-03 21:37:16 +03:00
|
|
|
bbox.Min.Z, bbox.Max.Z = minZ, maxZ
|
2017-07-24 18:26:10 +03:00
|
|
|
keepon = c.geoSearch(bbox, func(id string, obj geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if obj.Nearby(center, meters) {
|
|
|
|
if iterator(id, obj, fields) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2017-07-24 18:26:10 +03:00
|
|
|
if !keepon {
|
|
|
|
break
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-10-03 21:37:16 +03:00
|
|
|
bbox.Min.Z, bbox.Max.Z = minZ, maxZ
|
2017-07-24 18:26:10 +03:00
|
|
|
return c.geoSearch(bbox, func(id string, obj geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if obj.Nearby(center, meters) {
|
|
|
|
return iterator(id, obj, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-07-24 18:26:10 +03:00
|
|
|
func (c *Collection) Within(sparse uint8, obj geojson.Object, minLat, minLon, maxLat, maxLon, minZ, maxZ float64, iterator func(id string, obj geojson.Object, fields []float64) bool) bool {
|
|
|
|
var keepon = true
|
2016-03-05 02:08:16 +03:00
|
|
|
var bbox geojson.BBox
|
|
|
|
if obj != nil {
|
|
|
|
bbox = obj.CalculatedBBox()
|
2017-01-13 19:31:35 +03:00
|
|
|
if minZ == math.Inf(-1) && maxZ == math.Inf(+1) {
|
|
|
|
if bbox.Min.Z == 0 && bbox.Max.Z == 0 {
|
|
|
|
bbox.Min.Z = minZ
|
|
|
|
bbox.Max.Z = maxZ
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
} else {
|
2016-10-03 21:37:16 +03:00
|
|
|
bbox = geojson.BBox{Min: geojson.Position{X: minLon, Y: minLat, Z: minZ}, Max: geojson.Position{X: maxLon, Y: maxLat, Z: maxZ}}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
bboxes := bbox.Sparse(sparse)
|
|
|
|
if sparse > 0 {
|
|
|
|
for _, bbox := range bboxes {
|
|
|
|
if obj != nil {
|
2017-07-24 18:26:10 +03:00
|
|
|
keepon = c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if o.Within(obj) {
|
|
|
|
if iterator(id, o, fields) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
if keepon {
|
|
|
|
keepon = c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
|
|
|
if o.WithinBBox(bbox) {
|
|
|
|
if iterator(id, o, fields) {
|
|
|
|
return false
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !keepon {
|
|
|
|
break
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
if obj != nil {
|
2017-07-24 18:26:10 +03:00
|
|
|
return c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if o.Within(obj) {
|
|
|
|
return iterator(id, o, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if o.WithinBBox(bbox) {
|
|
|
|
return iterator(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.
|
2017-07-24 18:26:10 +03:00
|
|
|
func (c *Collection) Intersects(sparse uint8, obj geojson.Object, minLat, minLon, maxLat, maxLon, minZ, maxZ float64, iterator func(id string, obj geojson.Object, fields []float64) bool) bool {
|
|
|
|
var keepon = true
|
2016-03-05 02:08:16 +03:00
|
|
|
var bbox geojson.BBox
|
|
|
|
if obj != nil {
|
|
|
|
bbox = obj.CalculatedBBox()
|
2017-01-13 19:31:35 +03:00
|
|
|
if minZ == math.Inf(-1) && maxZ == math.Inf(+1) {
|
|
|
|
if bbox.Min.Z == 0 && bbox.Max.Z == 0 {
|
|
|
|
bbox.Min.Z = minZ
|
|
|
|
bbox.Max.Z = maxZ
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
} else {
|
2016-10-03 21:37:16 +03:00
|
|
|
bbox = geojson.BBox{Min: geojson.Position{X: minLon, Y: minLat, Z: minZ}, Max: geojson.Position{X: maxLon, Y: maxLat, Z: maxZ}}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
var bboxes []geojson.BBox
|
|
|
|
if sparse > 0 {
|
|
|
|
split := 1 << sparse
|
|
|
|
xpart := (bbox.Max.X - bbox.Min.X) / float64(split)
|
|
|
|
ypart := (bbox.Max.Y - bbox.Min.Y) / float64(split)
|
|
|
|
for y := bbox.Min.Y; y < bbox.Max.Y; y += ypart {
|
|
|
|
for x := bbox.Min.X; x < bbox.Max.X; x += xpart {
|
|
|
|
bboxes = append(bboxes, geojson.BBox{
|
2016-10-03 21:37:16 +03:00
|
|
|
Min: geojson.Position{X: x, Y: y, Z: minZ},
|
|
|
|
Max: geojson.Position{X: x + xpart, Y: y + ypart, Z: maxZ},
|
2016-03-05 02:08:16 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, bbox := range bboxes {
|
|
|
|
if obj != nil {
|
2017-07-24 18:26:10 +03:00
|
|
|
keepon = c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if o.Intersects(obj) {
|
|
|
|
if iterator(id, o, fields) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
if keepon {
|
|
|
|
keepon = c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
|
|
|
if o.IntersectsBBox(bbox) {
|
|
|
|
if iterator(id, o, fields) {
|
|
|
|
return false
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !keepon {
|
|
|
|
break
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return keepon
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
if obj != nil {
|
2017-07-24 18:26:10 +03:00
|
|
|
return c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if o.Intersects(obj) {
|
|
|
|
return iterator(id, o, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2017-07-24 18:26:10 +03:00
|
|
|
return c.geoSearch(bbox, func(id string, o geojson.Object, fields []float64) bool {
|
2016-03-05 02:08:16 +03:00
|
|
|
if o.IntersectsBBox(bbox) {
|
|
|
|
return iterator(id, o, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2017-01-31 02:41:12 +03:00
|
|
|
|
2017-07-24 18:26:10 +03:00
|
|
|
func (c *Collection) NearestNeighbors(lat, lon float64, iterator func(id string, obj geojson.Object, fields []float64) bool) bool {
|
2017-08-11 03:32:40 +03:00
|
|
|
return c.index.NearestNeighbors(lat, lon, func(item interface{}) bool {
|
2017-01-31 02:41:12 +03:00
|
|
|
var iitm *itemT
|
|
|
|
iitm, ok := item.(*itemT)
|
|
|
|
if !ok {
|
|
|
|
return true // just ignore
|
|
|
|
}
|
|
|
|
if !iterator(iitm.id, iitm.object, c.getFieldValues(iitm.id)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|