-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
diff: use adaptive zoom level for expire tiles to avoid huge lists fo…
…r large geometries
- Loading branch information
Showing
3 changed files
with
128 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,134 @@ | ||
package expire | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"regexp" | ||
"testing" | ||
|
||
osm "github.com/omniscale/go-osm" | ||
) | ||
|
||
func TestTileList_ExpireNodes(t *testing.T) { | ||
tests := []struct { | ||
nodes []osm.Node | ||
expected int | ||
polygon bool | ||
func TestTileList_ExpireNodesAdaptive(t *testing.T) { | ||
for _, test := range []struct { | ||
nodes []osm.Node | ||
expectedNum int | ||
expectedLevel int | ||
closed bool | ||
}{ | ||
// point | ||
{[]osm.Node{{Long: 8.30, Lat: 53.26}}, 1, false}, | ||
{[]osm.Node{{Long: 8.30, Lat: 53.26}}, 1, 14, false}, | ||
|
||
// point + paddings | ||
{[]osm.Node{{Long: 0, Lat: 0}}, 4, false}, | ||
{[]osm.Node{{Long: 0.01, Lat: 0}}, 2, false}, | ||
{[]osm.Node{{Long: 0, Lat: 0.01}}, 2, false}, | ||
{[]osm.Node{{Long: 0.01, Lat: 0.01}}, 1, false}, | ||
{[]osm.Node{{Long: 0, Lat: 0}}, 4, 14, false}, | ||
{[]osm.Node{{Long: 0.01, Lat: 0}}, 2, 14, false}, | ||
{[]osm.Node{{Long: 0, Lat: 0.01}}, 2, 14, false}, | ||
{[]osm.Node{{Long: 0.01, Lat: 0.01}}, 1, 14, false}, | ||
|
||
// line | ||
{[]osm.Node{ | ||
{Long: 8.30, Lat: 53.25}, | ||
{Long: 8.30, Lat: 53.30}, | ||
}, 5, false}, | ||
}, 5, 14, false}, | ||
// same line, but split into multiple segments | ||
{[]osm.Node{ | ||
{Long: 8.30, Lat: 53.25}, | ||
{Long: 8.30, Lat: 53.27}, | ||
{Long: 8.30, Lat: 53.29}, | ||
{Long: 8.30, Lat: 53.30}, | ||
}, 5, false}, | ||
}, 5, 14, false}, | ||
|
||
// L-shape | ||
{[]osm.Node{ | ||
{Long: 8.30, Lat: 53.25}, | ||
{Long: 8.30, Lat: 53.30}, | ||
{Long: 8.35, Lat: 53.30}, | ||
}, 8, false}, | ||
}, 8, 14, false}, | ||
|
||
// closed line (triangle) | ||
// line (triangle) | ||
{[]osm.Node{ | ||
{Long: 8.30, Lat: 53.25}, | ||
{Long: 8.30, Lat: 53.30}, | ||
{Long: 8.35, Lat: 53.30}, | ||
{Long: 8.30, Lat: 53.25}, | ||
}, 11, false}, | ||
// same closed line but polygon (triangle), whole bbox (4x5 tiles) is expired | ||
}, 11, 14, false}, | ||
// same line but closed/polygon (triangle), whole bbox (4x5 tiles) is expired | ||
{[]osm.Node{ | ||
{Long: 8.30, Lat: 53.25}, | ||
{Long: 8.30, Lat: 53.30}, | ||
{Long: 8.35, Lat: 53.30}, | ||
{Long: 8.30, Lat: 53.25}, | ||
}, 20, true}, | ||
}, 20, 14, true}, | ||
|
||
// large triangle, only outline expired for polygons and lines | ||
// large triangle, moved zoom level up | ||
{[]osm.Node{ | ||
{Long: 8.30, Lat: 53.25}, | ||
{Long: 8.30, Lat: 53.90}, | ||
{Long: 8.85, Lat: 53.90}, | ||
{Long: 8.30, Lat: 53.25}, | ||
}, 124, true}, | ||
}, 28, 11, true}, | ||
// same large triangle but as line, moved just one zoom level up to be | ||
// able to follow the outline more precise | ||
{[]osm.Node{ | ||
{Long: 8.30, Lat: 53.25}, | ||
{Long: 8.30, Lat: 53.90}, | ||
{Long: 8.85, Lat: 53.90}, | ||
{Long: 8.30, Lat: 53.25}, | ||
}, 124, false}, | ||
} | ||
for _, test := range tests { | ||
tl := NewTileList(14, "") | ||
tl.ExpireNodes(test.nodes, test.polygon) | ||
if len(tl.tiles) != test.expected { | ||
t.Errorf("expected %d tiles, got %d", test.expected, len(tl.tiles)) | ||
for tk := range tl.tiles { | ||
t.Errorf("\t%v", tk) | ||
}, 63, 13, false}, | ||
// long line, accross world | ||
{[]osm.Node{ | ||
{Long: -170, Lat: -80}, | ||
{Long: 170, Lat: 80}, | ||
}, 17, 4, false}, | ||
// large polygon, accross world | ||
{[]osm.Node{ | ||
{Long: -160, Lat: -70}, | ||
{Long: 160, Lat: -70}, | ||
{Long: 160, Lat: 70}, | ||
{Long: -160, Lat: 70}, | ||
}, 48, 3, true}, | ||
} { | ||
t.Run("", func(t *testing.T) { | ||
tl := NewTileList(14, "") | ||
tl.ExpireNodes(test.nodes, test.closed) | ||
for z := 0; z <= tl.maxZoom; z++ { | ||
expected := 0 | ||
if z == test.expectedLevel { | ||
expected = test.expectedNum | ||
} | ||
if len(tl.tiles[z]) != expected { | ||
t.Errorf("expected %d tiles, got %d in z=%d", expected, len(tl.tiles[z]), z) | ||
for tk := range tl.tiles[z] { | ||
t.Errorf("\t%v", tk) | ||
} | ||
} | ||
} | ||
buf := bytes.Buffer{} | ||
if err := tl.writeTiles(&buf); err != nil { | ||
t.Errorf("error writing tiles list: %s", err) | ||
} | ||
|
||
tileRe := regexp.MustCompile(`^\d+/\d+/\d+$`) | ||
scanner := bufio.NewScanner(&buf) | ||
|
||
lines := 0 | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
lines++ | ||
|
||
if !tileRe.MatchString(line) { | ||
t.Errorf("line %d does is not a tile coordinate: %s", lines, line) | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
t.Fatalf("rrror reading buffer: %v", err) | ||
} | ||
|
||
if lines != test.expectedNum { | ||
t.Errorf("expected %d lines, but got %d", test.expectedNum, lines) | ||
} | ||
} | ||
}) | ||
} | ||
} |