go.geo icon indicating copy to clipboard operation
go.geo copied to clipboard

Knearest returns duplicate points

Open estenssoros opened this issue 2 years ago • 0 comments

I created a point struct with a uuid and I noticed that k nearest returns the same point multiple times. Is this expected behavior?

code sample:

type MyPoint struct {
	Lng float64
	Lat float64
	Id  uuid.UUID
}

func (p MyPoint) String() string {
	ju, err := json.MarshalIndent(p, "", " ")
	if err != nil {
		panic(err)
	}
	return string(ju)
}

func (p MyPoint) Point() orb.Point {
	return orb.Point{p.Lng, p.Lat}
}

func run() error {
	r := rand.New(rand.NewSource(42))
	qt := quadtree.New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
	for i := 0; i < 1000; i++ {
		qt.Add(MyPoint{r.Float64(), r.Float64(), uuid.New()})
	}
	nearest := qt.KNearest(nil, orb.Point{0.5, 0.5}, 5)
	ju, err := json.MarshalIndent(nearest, "", " ")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(ju))
	return nil
}

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

output

[
 {
  "Lng": 0.4930591659434973,
  "Lat": 0.5196585530161364,
  "Id": "b1e5690a-1c7d-49f5-be4f-c389756af154"
 },
 {
  "Lng": 0.4930591659434973,
  "Lat": 0.5196585530161364,
  "Id": "b1e5690a-1c7d-49f5-be4f-c389756af154"
 },
 {
  "Lng": 0.4930591659434973,
  "Lat": 0.5196585530161364,
  "Id": "b1e5690a-1c7d-49f5-be4f-c389756af154"
 },
 {
  "Lng": 0.5073640535317331,
  "Lat": 0.478560836766942,
  "Id": "691971a9-6097-4591-bd89-6791349025d0"
 },
 {
  "Lng": 0.4930591659434973,
  "Lat": 0.5196585530161364,
  "Id": "b1e5690a-1c7d-49f5-be4f-c389756af154"
 }
]

estenssoros avatar Jan 03 '23 14:01 estenssoros