2019-06-11 00:47:42 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/tidwall/geojson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BinaryOp byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
NOOP BinaryOp = iota
|
|
|
|
AND
|
|
|
|
OR
|
2019-06-12 03:13:33 +03:00
|
|
|
tokenAND = "and"
|
|
|
|
tokenOR = "or"
|
|
|
|
tokenNOT = "not"
|
|
|
|
tokenLParen = "("
|
|
|
|
tokenRParen = ")"
|
2019-06-11 00:47:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// areaExpression is either an object or operator+children
|
|
|
|
type areaExpression struct {
|
2019-06-13 20:56:33 +03:00
|
|
|
negate bool
|
|
|
|
obj geojson.Object
|
|
|
|
op BinaryOp
|
2019-06-11 00:47:42 +03:00
|
|
|
children children
|
|
|
|
}
|
|
|
|
|
|
|
|
type children []*areaExpression
|
|
|
|
|
2019-06-13 20:56:33 +03:00
|
|
|
// String representation, helpful in logging.
|
2019-06-12 03:13:33 +03:00
|
|
|
func (e *areaExpression) String() (res string) {
|
2019-06-11 00:47:42 +03:00
|
|
|
if e.obj != nil {
|
2019-06-12 03:13:33 +03:00
|
|
|
res = e.obj.String()
|
|
|
|
} else {
|
|
|
|
var chStrings []string
|
|
|
|
for _, c := range e.children {
|
|
|
|
chStrings = append(chStrings, c.String())
|
|
|
|
}
|
|
|
|
switch e.op {
|
|
|
|
case NOOP:
|
|
|
|
res = "empty operator"
|
|
|
|
case AND:
|
|
|
|
res = "(" + strings.Join(chStrings, " "+tokenAND+" ") + ")"
|
|
|
|
case OR:
|
|
|
|
res = "(" + strings.Join(chStrings, " "+tokenOR+" ") + ")"
|
|
|
|
default:
|
|
|
|
res = "unknown operator"
|
|
|
|
}
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
if e.negate {
|
|
|
|
res = tokenNOT + " " + res
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
return
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return boolean value modulo negate field of the expression.
|
2019-06-12 03:13:33 +03:00
|
|
|
func (e *areaExpression) maybeNegate(val bool) bool {
|
2019-06-11 00:47:42 +03:00
|
|
|
if e.negate {
|
|
|
|
return !val
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2019-06-12 03:13:33 +03:00
|
|
|
// Methods for testing an areaExpression against the spatial object
|
2019-06-13 20:56:33 +03:00
|
|
|
func (e *areaExpression) testObject(
|
|
|
|
o geojson.Object,
|
|
|
|
objObjTest func(o1, o2 geojson.Object) bool,
|
|
|
|
exprObjTest func(ae *areaExpression, ob geojson.Object) bool,
|
|
|
|
) bool {
|
2019-06-11 00:47:42 +03:00
|
|
|
if e.obj != nil {
|
2019-06-13 20:56:33 +03:00
|
|
|
return objObjTest(e.obj, o)
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
switch e.op {
|
|
|
|
case AND:
|
|
|
|
for _, c := range e.children {
|
2019-06-13 20:56:33 +03:00
|
|
|
if !exprObjTest(c, o) {
|
2019-06-12 03:13:33 +03:00
|
|
|
return false
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
return true
|
2019-06-11 00:47:42 +03:00
|
|
|
case OR:
|
|
|
|
for _, c := range e.children {
|
2019-06-13 20:56:33 +03:00
|
|
|
if exprObjTest(c, o) {
|
2019-06-12 03:13:33 +03:00
|
|
|
return true
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
return false
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
return false
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-13 20:56:33 +03:00
|
|
|
func (e *areaExpression) rawIntersects(o geojson.Object) bool {
|
|
|
|
return e.testObject(o, geojson.Object.Intersects, (*areaExpression).Intersects)
|
|
|
|
}
|
|
|
|
|
2019-06-12 03:13:33 +03:00
|
|
|
func (e *areaExpression) rawContains(o geojson.Object) bool {
|
2019-06-13 20:56:33 +03:00
|
|
|
return e.testObject(o, geojson.Object.Contains, (*areaExpression).Contains)
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-12 03:13:33 +03:00
|
|
|
func (e *areaExpression) rawWithin(o geojson.Object) bool {
|
2019-06-13 20:56:33 +03:00
|
|
|
return e.testObject(o, geojson.Object.Within, (*areaExpression).Within)
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-12 03:13:33 +03:00
|
|
|
func (e *areaExpression) Intersects(o geojson.Object) bool {
|
|
|
|
return e.maybeNegate(e.rawIntersects(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *areaExpression) Contains(o geojson.Object) bool {
|
|
|
|
return e.maybeNegate(e.rawContains(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *areaExpression) Within(o geojson.Object) bool {
|
|
|
|
return e.maybeNegate(e.rawWithin(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Methods for testing an areaExpression against another areaExpression
|
2019-06-13 20:56:33 +03:00
|
|
|
func (e *areaExpression) testExpression(
|
|
|
|
oe *areaExpression,
|
|
|
|
exprObjTest func(ae *areaExpression, ob geojson.Object) bool,
|
|
|
|
rawExprExprTest func(ae1, ae2 *areaExpression) bool,
|
|
|
|
exprExprTest func(ae1, ae2 *areaExpression) bool,
|
|
|
|
) bool {
|
2019-06-12 03:13:33 +03:00
|
|
|
if oe.negate {
|
2019-06-13 20:56:33 +03:00
|
|
|
e2 := &areaExpression{negate: !e.negate, obj: e.obj, op: e.op, children: e.children}
|
|
|
|
oe2 := &areaExpression{negate: false, obj: oe.obj, op: oe.op, children: oe.children}
|
|
|
|
return exprExprTest(e2, oe2)
|
2019-06-12 03:13:33 +03:00
|
|
|
}
|
2019-06-11 00:47:42 +03:00
|
|
|
if oe.obj != nil {
|
2019-06-13 20:56:33 +03:00
|
|
|
return exprObjTest(e, oe.obj)
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
switch oe.op {
|
|
|
|
case AND:
|
|
|
|
for _, c := range oe.children {
|
2019-06-13 20:56:33 +03:00
|
|
|
if !rawExprExprTest(e, c) {
|
2019-06-12 03:13:33 +03:00
|
|
|
return false
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
return true
|
2019-06-11 00:47:42 +03:00
|
|
|
case OR:
|
|
|
|
for _, c := range oe.children {
|
2019-06-13 20:56:33 +03:00
|
|
|
if rawExprExprTest(e, c) {
|
2019-06-12 03:13:33 +03:00
|
|
|
return true
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
return false
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
2019-06-12 03:13:33 +03:00
|
|
|
return false
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-13 20:56:33 +03:00
|
|
|
func (e *areaExpression) rawIntersectsExpr(oe *areaExpression) bool {
|
|
|
|
return e.testExpression(
|
|
|
|
oe,
|
|
|
|
(*areaExpression).rawIntersects,
|
|
|
|
(*areaExpression).rawIntersectsExpr,
|
|
|
|
(*areaExpression).IntersectsExpr)
|
|
|
|
}
|
|
|
|
|
2019-06-12 03:13:33 +03:00
|
|
|
func (e *areaExpression) rawWithinExpr(oe *areaExpression) bool {
|
2019-06-13 20:56:33 +03:00
|
|
|
return e.testExpression(
|
|
|
|
oe,
|
|
|
|
(*areaExpression).rawWithin,
|
|
|
|
(*areaExpression).rawWithinExpr,
|
|
|
|
(*areaExpression).WithinExpr)
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-12 03:13:33 +03:00
|
|
|
func (e *areaExpression) rawContainsExpr(oe *areaExpression) bool {
|
2019-06-13 20:56:33 +03:00
|
|
|
return e.testExpression(
|
|
|
|
oe,
|
|
|
|
(*areaExpression).rawContains,
|
|
|
|
(*areaExpression).rawContainsExpr,
|
|
|
|
(*areaExpression).ContainsExpr)
|
2019-06-12 03:13:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *areaExpression) IntersectsExpr(oe *areaExpression) bool {
|
|
|
|
return e.maybeNegate(e.rawIntersectsExpr(oe))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *areaExpression) WithinExpr(oe *areaExpression) bool {
|
|
|
|
return e.maybeNegate(e.rawWithinExpr(oe))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *areaExpression) ContainsExpr(oe *areaExpression) bool {
|
|
|
|
return e.maybeNegate(e.rawContainsExpr(oe))
|
2019-06-11 00:47:42 +03:00
|
|
|
}
|