Skip to content

Commit

Permalink
adding logic to expand bbox to match python implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tingold committed Dec 21, 2024
1 parent f01b3ea commit 961d5cd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
43 changes: 32 additions & 11 deletions majortom/mtgrid.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package majortom

import (
"errors"
"github.com/paulmach/orb"
"github.com/paulmach/orb/planar"
"github.com/pierrre/geohash"
"math"
"sync"
Expand Down Expand Up @@ -158,23 +158,44 @@ func (g *MajorTomGrid) CellFromId(id string) (*GridCell, error) {
Max: orb.Point{box.Lon.Max, box.Lat.Max},
}
p := b.ToPolygon()
centroid := b.Center()

cells, err := g.GenerateGridCells(&p)
if err != nil {
return nil, err
}
leastDist := math.MaxFloat64
var closestCell *GridCell
for _, cell := range cells {
if cell.Id() == id {
return &cell, nil
} else {
dist := planar.Distance(cell.Bound().Center(), centroid)
if dist < leastDist {
leastDist = dist
closestCell = &cell
}
}
}
return closestCell, nil
//expand bbox by 10% if nothing is found
p = expandBound(b, 10).ToPolygon()
cells, err = g.GenerateGridCells(p)
if err != nil {
return nil, err
}
for _, cell := range cells {
if cell.Id() == id {
return &cell, nil
}
}
return nil, errors.New("cell not found")
}

func expandBound(bound orb.Bound, percentage float64) orb.Bound {
// Calculate the width and height of the bounding box.
width := bound.Max[0] - bound.Min[0]
height := bound.Max[1] - bound.Min[1]

// Calculate the expansion amounts for each dimension.
expandX := width * (percentage / 100.0)
expandY := height * (percentage / 100.0)

// Create a new expanded bounding box.
newBound := orb.Bound{
Min: orb.Point{bound.Min[0] - expandX/2, bound.Min[1] - expandY/2},
Max: orb.Point{bound.Max[0] + expandX/2, bound.Max[1] + expandY/2},
}

return newBound
}
10 changes: 10 additions & 0 deletions majortom/mtgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,13 @@ func TestSmallGrid(t *testing.T) {
}
}
}

func TestOddTile(t *testing.T) {
g := NewGrid(320, true)
cell, err := g.CellFromId("qr330j8p802")
if err != nil {
t.FailNow()
} else {
t.Logf("Expected: qr330j8p802, Got: %s", cell.Id())
}
}

0 comments on commit 961d5cd

Please sign in to comment.