Skip to content

Commit d33c1ac

Browse files
committed
expire: ignore changes outside of webmercator boundaries
1 parent afffadb commit d33c1ac

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

expire/tilelist.go

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ func init() {
3333

3434
func tileCoord(long, lat float64, zoom int) (float64, float64) {
3535
x, y := proj.WgsToMerc(long, lat)
36+
if x < mercBbox[0] || x > mercBbox[2] || y < mercBbox[1] || y > mercBbox[3] {
37+
return -1, -1
38+
}
3639
res := mercRes[zoom]
3740
x = x - mercBbox[0]
3841
y = mercBbox[3] - y
@@ -100,6 +103,9 @@ func (tl *TileList) addCoord(long, lat float64) {
100103
const tilePadding = 0.2
101104
tl.mu.Lock()
102105
tileX, tileY := tileCoord(long, lat, tl.maxZoom)
106+
if tileX < 0 {
107+
return
108+
}
103109
for x := uint32(tileX - tilePadding); x <= uint32(tileX+tilePadding); x++ {
104110
for y := uint32(tileY - tilePadding); y <= uint32(tileY+tilePadding); y++ {
105111
tl.tiles[tl.maxZoom][tileKey{x, y}] = struct{}{}
@@ -124,6 +130,9 @@ func (tl *TileList) expireLine(nodes []osm.Node, zoom int) {
124130
}
125131
x1, y1 := tileCoord(nodes[i].Long, nodes[i].Lat, zoom)
126132
x2, y2 := tileCoord(nodes[i+1].Long, nodes[i+1].Lat, zoom)
133+
if x1 < 0 || x2 < 0 {
134+
return
135+
}
127136
if int(x1) == int(x2) && int(y1) == int(y2) {
128137
tl.tiles[zoom][tileKey{X: uint32(x1), Y: uint32(y1)}] = struct{}{}
129138
} else {
@@ -140,6 +149,9 @@ func (tl *TileList) expireBox(b bbox, zoom int) {
140149
defer tl.mu.Unlock()
141150
x1, y1 := tileCoord(b.minx, b.maxy, zoom)
142151
x2, y2 := tileCoord(b.maxx, b.miny, zoom)
152+
if x1 < 0 || x2 < 0 {
153+
return
154+
}
143155
for x := uint32(x1); x <= uint32(x2); x++ {
144156
for y := uint32(y1); y <= uint32(y2); y++ {
145157
tl.tiles[zoom][tileKey{x, y}] = struct{}{}
@@ -233,6 +245,9 @@ func nodesBbox(nodes []osm.Node) bbox {
233245
func numBboxTiles(b bbox, zoom int) int {
234246
x1, y1 := tileCoord(b.minx, b.maxy, zoom)
235247
x2, y2 := tileCoord(b.maxx, b.miny, zoom)
248+
if x1 < 0 || x2 < 0 {
249+
return 0
250+
}
236251
return int(math.Abs((x2 - x1 + 1) * (y2 - y1 + 1)))
237252
}
238253

expire/tilelist_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ func TestTileList_ExpireNodesAdaptive(t *testing.T) {
8787
{Long: 160, Lat: 70},
8888
{Long: -160, Lat: 70},
8989
}, 48, 3, true},
90+
91+
// expire tiles use Webmercator, ignore nodes at the poles
92+
{[]osm.Node{
93+
{Long: 0, Lat: 90},
94+
{Long: 0, Lat: 89},
95+
}, 0, 0, true},
96+
{[]osm.Node{
97+
{Long: 0, Lat: -90},
98+
{Long: 0, Lat: -89},
99+
}, 0, 0, true},
100+
{[]osm.Node{
101+
{Long: -170, Lat: 89},
102+
{Long: 170, Lat: 70},
103+
}, 0, 0, true},
90104
} {
91105
t.Run("", func(t *testing.T) {
92106
tl := NewTileList(14, "")

0 commit comments

Comments
 (0)