1
0
forked from MTSR/mapserver

speed improvement

This commit is contained in:
NatureFreshMilk 2019-01-17 15:37:33 +01:00
parent 5be4319aa6
commit b81e3c8b21
2 changed files with 41 additions and 18 deletions

View File

@ -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) + ")"

View File

@ -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