Git Product home page Git Product logo

Comments (16)

tidwall avatar tidwall commented on September 18, 2024

Right now BuntDB is limited to intersection searches on rectangles and points.

If what you are wanting is to find all points that are within a specified radius, such as:

Then may I suggest this approach.

1. Calculate the bounding box around the circle

2. Get all the points in the rectangle

3. Perform the haversine calculation on each point


This is pretty much how Tile38 does it. Below is some code that you might find helpful.

Calculate Bounds of a Geo Circle
Haversine Formula

from buntdb.

leearmstrong avatar leearmstrong commented on September 18, 2024

Thanks for the feedback. I’m actually just after the nearest point so might just look at implementing a kd-tree instead.

I thought Tile38 did that sort of query with a KNN query?

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

Ah. At the moment BuntDB doesn’t have a kNN operation.

from buntdb.

leearmstrong avatar leearmstrong commented on September 18, 2024

Thanks @tidwall, tile38 does at the moment doesn't it?

To use this I suppose I'd need to port the R-Tree across to BuntDB?

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

Yes, Tile38 has support for kNN searches.

BuntDB uses an R-Tree already, so it’s possible to add kNN without modifying the underlying data structure. I just haven’t gotten around to adding the operation yet.

from buntdb.

leearmstrong avatar leearmstrong commented on September 18, 2024

Ok thanks, will fork away 👍

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

Sounds good. A couple of pointers.

BuntDB uses this R-Tree package.
https://github.com/tidwall/rtree

Tile38 uses this one and includes the kNN code
https://github.com/tidwall/tile38/tree/master/index/

They have an entirely different codebases.
Tile38 rtree is strictly 2 dimensions while the BuntDB is a composite of 20 dimensions.

from buntdb.

leearmstrong avatar leearmstrong commented on September 18, 2024

Wow, 20!!! For a lat/lon I'll only need the 2 dimensions.

I'll see if I can tackle it but suspect I might end up just creating a bounding box around my test point and do a check and sort them by distance that way

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

I just updated the https://github.com/tidwall/rtree project to include kNN support. And I added a Nearby function to BuntDB.

from buntdb.

leearmstrong avatar leearmstrong commented on September 18, 2024

Oh nice, thank you that is awesome. I had started a kd-tree implementation and then realised it was suited to a curved globe!

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

You're welcome. :)

from buntdb.

leearmstrong avatar leearmstrong commented on September 18, 2024

What value is the distance operator?

I created a real simple example...
https://gist.github.com/leearmstrong/8ff311e5ffb4e6075e24f31737f2c06d

This logs

2018/01/15 10:53:37 Searching from coord [-0.919 50.819]
2018/01/15 10:53:37 aircraft:TEST:pos [-0.919 50.919] 0.009999999999998864

I make the distance 11km.

Is the distance not relative but I can be guaranteed they will be returned in order and I need to calculate the distance myself?

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

The distance param could use a little more description, or maybe I should just removed it to avoid confusion.

It's the distance of the bounding boxes. In your case it's the distance of the two 2D points squared. The unit is the same as what was inputed, in your case degrees (lat/lon).

To get the distance on earth in meters (not in degrees) you should ignore the distance and calculate using haversine yourself.

bunt.View(func(tx *buntdb.Tx) error {
	tx.Nearby("locationIndex", locationString, func(key, val string, dist float64) bool {
		meters := haversine(locationString, val)
		return false
	})
	return nil
})

from buntdb.

leearmstrong avatar leearmstrong commented on September 18, 2024

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

You're welcome. I'll just leave it for now and try to come up with a better description.

from buntdb.

tidwall avatar tidwall commented on September 18, 2024

I added a description of the dist param to the Nearby function comment.

from buntdb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.