From b81e3c8b21ba1e40895938a833b74670ab57bd1b Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Thu, 17 Jan 2019 15:37:33 +0100 Subject: [PATCH] speed improvement --- db/sqlite.go | 32 ++++++++++++++++---------------- tilerenderer/renderer.go | 27 +++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/db/sqlite.go b/db/sqlite.go index dfb554f..cdac3d9 100644 --- a/db/sqlite.go +++ b/db/sqlite.go @@ -104,28 +104,28 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) { return nil, nil } +func sortAsc(a, b int) (int, int){ + if a > b { + return b, a + } else { + return a, b + } +} func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error){ poslist := make([]interface{}, 0) - if pos1.X != pos2.X { - return 0, errors.New("x does not line up") - } + minX, maxX := sortAsc(pos1.X, pos2.X) + minY, maxY := sortAsc(pos1.Y, pos2.Y) + minZ, maxZ := sortAsc(pos1.Z, pos2.Z) - if pos1.Z != pos2.Z { - return 0, errors.New("z does not line up") - } - - minY := pos1.Y - maxY := pos2.Y - - if minY > maxY { - minY, maxY = maxY, minY - } - - for y := minY; y <= maxY; y++ { - poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(pos1.X, y, pos1.Z))) + for x := minX; x <= maxX; x++ { + for y := minY; y <= maxY; y++ { + for z := minZ; z <= maxZ; z++ { + poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(x,y,z))) + } + } } getBlocksQuery := "select count(*) from blocks b where b.pos in (?" + strings.Repeat(",?", len(poslist)-1) + ")" diff --git a/tilerenderer/renderer.go b/tilerenderer/renderer.go index 14f1660..d01623d 100644 --- a/tilerenderer/renderer.go +++ b/tilerenderer/renderer.go @@ -55,6 +55,11 @@ func (tr *TileRenderer) Render(tc coords.TileCoords) ([]byte, error) { return nil, err } + if img == nil { + //empty tile + return nil, nil + } + buf := new(bytes.Buffer) png.Encode(buf, img) @@ -95,12 +100,17 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error) return nil, errors.New("No layer found") } + if tc.Zoom > 13 || tc.Zoom < 1 { + return nil, errors.New("Invalid zoom") + } + if tc.Zoom == 13 { //max zoomed in on mapblock level mbr := coords.GetMapBlockRangeFromTile(tc, 0) mbr.Pos1.Y = layer.From mbr.Pos2.Y = layer.To + //count blocks count, err := tr.dba.CountBlocks(mbr.Pos1, mbr.Pos2) if err != nil { @@ -114,8 +124,21 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error) return tr.mapblockrenderer.Render(mbr.Pos1, mbr.Pos2) } - if tc.Zoom > 13 || tc.Zoom < 1 { - return nil, errors.New("Invalid zoom") + if tc.Zoom == 12 { + //count blocks and ignore empty tiles + mbr := coords.GetMapBlockRangeFromTile(tc, 0) + mbr.Pos1.Y = layer.From + mbr.Pos2.Y = layer.To + + count, err := tr.dba.CountBlocks(mbr.Pos1, mbr.Pos2) + + if err != nil { + return nil, err + } + + if count == 0 { + return nil, nil + } } //zoom 1-12