Skip to content

Commit

Permalink
expire: ignore changes outside of webmercator boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
olt committed Feb 24, 2025
1 parent afffadb commit d33c1ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions expire/tilelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func init() {

func tileCoord(long, lat float64, zoom int) (float64, float64) {
x, y := proj.WgsToMerc(long, lat)
if x < mercBbox[0] || x > mercBbox[2] || y < mercBbox[1] || y > mercBbox[3] {
return -1, -1
}
res := mercRes[zoom]
x = x - mercBbox[0]
y = mercBbox[3] - y
Expand Down Expand Up @@ -100,6 +103,9 @@ func (tl *TileList) addCoord(long, lat float64) {
const tilePadding = 0.2
tl.mu.Lock()
tileX, tileY := tileCoord(long, lat, tl.maxZoom)
if tileX < 0 {
return
}
for x := uint32(tileX - tilePadding); x <= uint32(tileX+tilePadding); x++ {
for y := uint32(tileY - tilePadding); y <= uint32(tileY+tilePadding); y++ {
tl.tiles[tl.maxZoom][tileKey{x, y}] = struct{}{}
Expand All @@ -124,6 +130,9 @@ func (tl *TileList) expireLine(nodes []osm.Node, zoom int) {
}
x1, y1 := tileCoord(nodes[i].Long, nodes[i].Lat, zoom)
x2, y2 := tileCoord(nodes[i+1].Long, nodes[i+1].Lat, zoom)
if x1 < 0 || x2 < 0 {
return
}
if int(x1) == int(x2) && int(y1) == int(y2) {
tl.tiles[zoom][tileKey{X: uint32(x1), Y: uint32(y1)}] = struct{}{}
} else {
Expand All @@ -140,6 +149,9 @@ func (tl *TileList) expireBox(b bbox, zoom int) {
defer tl.mu.Unlock()
x1, y1 := tileCoord(b.minx, b.maxy, zoom)
x2, y2 := tileCoord(b.maxx, b.miny, zoom)
if x1 < 0 || x2 < 0 {
return
}
for x := uint32(x1); x <= uint32(x2); x++ {
for y := uint32(y1); y <= uint32(y2); y++ {
tl.tiles[zoom][tileKey{x, y}] = struct{}{}
Expand Down Expand Up @@ -233,6 +245,9 @@ func nodesBbox(nodes []osm.Node) bbox {
func numBboxTiles(b bbox, zoom int) int {
x1, y1 := tileCoord(b.minx, b.maxy, zoom)
x2, y2 := tileCoord(b.maxx, b.miny, zoom)
if x1 < 0 || x2 < 0 {
return 0
}
return int(math.Abs((x2 - x1 + 1) * (y2 - y1 + 1)))
}

Expand Down
14 changes: 14 additions & 0 deletions expire/tilelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ func TestTileList_ExpireNodesAdaptive(t *testing.T) {
{Long: 160, Lat: 70},
{Long: -160, Lat: 70},
}, 48, 3, true},

// expire tiles use Webmercator, ignore nodes at the poles
{[]osm.Node{
{Long: 0, Lat: 90},
{Long: 0, Lat: 89},
}, 0, 0, true},
{[]osm.Node{
{Long: 0, Lat: -90},
{Long: 0, Lat: -89},
}, 0, 0, true},
{[]osm.Node{
{Long: -170, Lat: 89},
{Long: 170, Lat: 70},
}, 0, 0, true},
} {
t.Run("", func(t *testing.T) {
tl := NewTileList(14, "")
Expand Down

0 comments on commit d33c1ac

Please sign in to comment.