fix: distance if point and object have the same coordinates

This commit is contained in:
Benjamin Ramser 2021-03-19 09:42:38 +01:00
parent 53af1e2306
commit 2a8b98778b
4 changed files with 26 additions and 10 deletions

View File

@ -164,9 +164,12 @@ func fenceMatch(
break break
} }
sw.mu.Lock() sw.mu.Lock()
var distance float64 var meters float64
distance := Distance{false, meters}
if fence.distance && fence.obj != nil { if fence.distance && fence.obj != nil {
distance = details.obj.Distance(fence.obj) meters = details.obj.Distance(fence.obj)
distance.ready = true
distance.meters = meters
} }
sw.fmap = details.fmap sw.fmap = details.fmap
sw.fullFields = true sw.fullFields = true

View File

@ -60,12 +60,18 @@ type scanWriter struct {
respOut resp.Value respOut resp.Value
} }
// Distance ...
type Distance struct {
ready bool
meters float64
}
// ScanWriterParams ... // ScanWriterParams ...
type ScanWriterParams struct { type ScanWriterParams struct {
id string id string
o geojson.Object o geojson.Object
fields []float64 fields []float64
distance float64 distance Distance
noLock bool noLock bool
ignoreGlobMatch bool ignoreGlobMatch bool
clip geojson.Object clip geojson.Object
@ -433,8 +439,8 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool {
wr.WriteString(jsfields) wr.WriteString(jsfields)
if opts.distance > 0 { if opts.distance.ready {
wr.WriteString(`,"distance":` + strconv.FormatFloat(opts.distance, 'f', -1, 64)) wr.WriteString(`,"distance":` + strconv.FormatFloat(opts.distance.meters, 'f', -1, 64))
} }
wr.WriteString(`}`) wr.WriteString(`}`)
@ -496,8 +502,8 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool {
vals = append(vals, resp.ArrayValue(fvals)) vals = append(vals, resp.ArrayValue(fvals))
} }
} }
if opts.distance > 0 { if opts.distance.ready {
vals = append(vals, resp.FloatValue(opts.distance)) vals = append(vals, resp.FloatValue(opts.distance.meters))
} }
sw.values = append(sw.values, resp.ArrayValue(vals)) sw.values = append(sw.values, resp.ArrayValue(vals))

View File

@ -372,14 +372,18 @@ func (server *Server) cmdNearby(msg *Message) (res resp.Value, err error) {
if sw.col != nil { if sw.col != nil {
iter := func(id string, o geojson.Object, fields []float64, dist float64) bool { iter := func(id string, o geojson.Object, fields []float64, dist float64) bool {
meters := 0.0 meters := 0.0
distance := Distance{false, meters}
if s.distance { if s.distance {
meters = geo.DistanceFromHaversine(dist) meters = geo.DistanceFromHaversine(dist)
distance.ready = true
distance.meters = meters
} }
return sw.writeObject(ScanWriterParams{ return sw.writeObject(ScanWriterParams{
id: id, id: id,
o: o, o: o,
fields: fields, fields: fields,
distance: meters, distance: distance,
noLock: true, noLock: true,
ignoreGlobMatch: true, ignoreGlobMatch: true,
skipTesting: true, skipTesting: true,

View File

@ -28,10 +28,13 @@ func keys_KNN_test(mc *mockServer) error {
{"SET", "mykey", "3", "POINT", 12, 19}, {"OK"}, {"SET", "mykey", "3", "POINT", 12, 19}, {"OK"},
{"SET", "mykey", "4", "POINT", -5, 5}, {"OK"}, {"SET", "mykey", "4", "POINT", -5, 5}, {"OK"},
{"SET", "mykey", "5", "POINT", 33, 21}, {"OK"}, {"SET", "mykey", "5", "POINT", 33, 21}, {"OK"},
{"SET", "mykey", "6", "POINT", 52, 13}, {"OK"},
{"NEARBY", "mykey", "LIMIT", 10, "POINTS", "POINT", 20, 20}, { {"NEARBY", "mykey", "LIMIT", 10, "POINTS", "POINT", 20, 20}, {
"[0 [[2 [19 19]] [3 [12 19]] [5 [33 21]] [1 [5 5]] [4 [-5 5]]]]"}, "[0 [[2 [19 19]] [3 [12 19]] [5 [33 21]] [1 [5 5]] [4 [-5 5]] [6 [52 13]]]]"},
{"NEARBY", "mykey", "LIMIT", 10, "IDS", "POINT", 20, 20, 4000000}, {"[0 [2 3 5 1 4]]"}, {"NEARBY", "mykey", "LIMIT", 10, "IDS", "POINT", 20, 20, 4000000}, {"[0 [2 3 5 1 4 6]]"},
{"NEARBY", "mykey", "LIMIT", 10, "IDS", "POINT", 20, 20, 1500000}, {"[0 [2 3 5]]"}, {"NEARBY", "mykey", "LIMIT", 10, "IDS", "POINT", 20, 20, 1500000}, {"[0 [2 3 5]]"},
{"NEARBY", "mykey", "LIMIT", 10, "DISTANCE", "POINT", 52, 13, 100}, {`[0 [[6 {"type":"Point","coordinates":[13,52]} 0]]]`},
{"NEARBY", "mykey", "LIMIT", 10, "DISTANCE", "POINT", 52.1, 13.1, 100000}, {`[0 [[6 {"type":"Point","coordinates":[13,52]} 13053.885940801563]]]`},
}) })
} }