dhconnelly / rtreego

an R-Tree library for Go

Home Page:http://dhconnelly.github.com/rtreego

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NearestNeighbors not returning correct point

tamal-appsbee opened this issue · comments

Hi,

Thanks for this awesome lib.
I am facing an issue :
I have following points:
keysBody := []byte([{"id": "1","name": "A","location": [88.495873,22.551802]},{"id": "2","name": "B","location": [88.490197,22.552743]},{"id": "3","name": "C","location": [88.492724,22.558270]}])
.................
type Storage struct {
Users map[string][]*user
geoIndex *rtreego.Rtree
nearestNeighbors int
}
.............
geoIndex.Insert(user)
............
lat := 22.562454
lon := 88.494374
...
geoIndex.NearestNeighbors(
20, rtreego.Point{lat, lon},
)

It always return : {"id": "2","name": "B","location": [88.490197,22.552743]} while id 3 (name c) is nearest

am I doing wrong or something ?

For me, the following code prints {[88.492724 22.55827]}:


import (
  "github.com/dhconnelly/rtreego"
  "fmt"
)

const tol = 0.001

type Thing struct {
  loc rtreego.Point
}

func (t Thing) Bounds() *rtreego.Rect {
  return t.loc.ToRect(tol)
}

func main() {
  rt := rtreego.NewTree(2, 3, 3)
  x := Thing{loc:rtreego.Point{88.495873,22.551802}}
  y := Thing{loc:rtreego.Point{88.490197,22.552743}}
  z := Thing{loc:rtreego.Point{88.492724,22.558270}}
  // this looks reversed to me, but the result was the same when flipped
  a := rtreego.Point{22.562454,88.494374}
  rt.Insert(x)
  rt.Insert(y)
  rt.Insert(z)
  // result was the same when using NearestNeighbors(20, a) and printing closest[0]
  closest := rt.NearestNeighbor(a)
  fmt.Println(closest)
}

Could you post the Bounds() function and your user struct definitions?

// user struct
type User struct {
Location rtreego.Point
Uid string
}

// Bounds rectangle with rtree
func (d *User) Bounds() *rtreego.Rect {
return d.Location.ToRect(0.01)
}

// rtree storage
type Storage struct {
Users map[string][]*user
geoIndex *rtreego.Rtree
nearestNeighbors int
}

I think issue with my "tol" value
why your const tol = 0.001 ?

I think you're right. Your points differ by less than your tol = 0.01 (i.e. 88.490, 88.492 and 88.495 would all look like the same value), so the library probably can't distinguish between them properly. Try using 0.001 or 0.0001 and see if you get the correct result.

yes getting correct result. Thank you.

Please tell me what is "tol" ?
As when I use tol = 0.001 correct result coming
but when I using tol=0.0001 wrong result coming

tol (for "tolerance") is the width of the rectangle that is formed from your point -- the library doesn't actually store points because the algorithms involved only use rectangles, and so we have to create small rectangles out of the points before inserting them into the tree (see https://github.com/dhconnelly/rtreego/blob/master/geom.go#L300).

I'm not sure why tol = 0.001 works but tol = 0.0001 doesn't. That seems strange. I'd have to debug this in a lot more detail (especially as I can't reproduce the issue), but for now I'd say to just use the tol value that is producing the correct results for you.

Yes..
Thanks you