Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NearestNeighbors not returning correct point #29

Closed
tamal-appsbee opened this issue Oct 27, 2018 · 6 comments
Closed

NearestNeighbors not returning correct point #29

tamal-appsbee opened this issue Oct 27, 2018 · 6 comments

Comments

@tamal-appsbee
Copy link

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 ?

@dhconnelly
Copy link
Owner

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?

@tamal-appsbee
Copy link
Author

tamal-appsbee commented Oct 27, 2018

// 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 ?

@dhconnelly
Copy link
Owner

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.

@tamal-appsbee
Copy link
Author

tamal-appsbee commented Oct 28, 2018

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

@dhconnelly
Copy link
Owner

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.

@tamal-appsbee
Copy link
Author

Yes..
Thanks you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants