mapserver/tilerenderer/renderer.go

212 lines
3.8 KiB
Go
Raw Normal View History

2019-01-11 18:00:40 +03:00
package tilerenderer
import (
2019-01-17 13:20:03 +03:00
"errors"
2019-01-13 18:37:03 +03:00
"image"
2019-01-17 13:38:13 +03:00
"time"
2019-01-17 13:20:03 +03:00
"bytes"
"image/png"
"image/draw"
2019-01-17 17:14:13 +03:00
"mapserver/db"
2019-01-13 18:37:03 +03:00
"mapserver/coords"
"mapserver/mapblockrenderer"
2019-01-17 13:20:03 +03:00
"mapserver/layerconfig"
"mapserver/tiledb"
2019-01-17 13:27:31 +03:00
"github.com/sirupsen/logrus"
2019-01-11 18:00:40 +03:00
)
type TileRenderer struct {
2019-01-13 18:37:03 +03:00
mapblockrenderer *mapblockrenderer.MapBlockRenderer
2019-01-17 13:20:03 +03:00
layers []layerconfig.Layer
tdb tiledb.DBAccessor
2019-01-17 17:14:13 +03:00
dba db.DBAccessor
2019-01-11 18:00:40 +03:00
}
2019-01-17 13:20:03 +03:00
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
tdb tiledb.DBAccessor,
2019-01-17 17:14:13 +03:00
dba db.DBAccessor,
2019-01-17 13:20:03 +03:00
layers []layerconfig.Layer) *TileRenderer {
2019-01-13 18:37:03 +03:00
return &TileRenderer{
mapblockrenderer: mapblockrenderer,
2019-01-17 13:20:03 +03:00
layers: layers,
tdb: tdb,
2019-01-17 17:14:13 +03:00
dba: dba,
2019-01-13 18:37:03 +03:00
}
2019-01-11 18:00:40 +03:00
}
2019-01-17 13:20:03 +03:00
const (
IMG_SIZE = 256
)
func (tr *TileRenderer) Render(tc coords.TileCoords) ([]byte, error) {
//Check cache
tile, err := tr.tdb.GetTile(tc)
if err != nil {
return nil, err
}
if tile == nil {
//No tile in db
img, err := tr.RenderImage(tc)
if err != nil {
return nil, err
}
2019-01-17 17:37:33 +03:00
if img == nil {
//empty tile
return nil, nil
}
2019-01-17 13:20:03 +03:00
buf := new(bytes.Buffer)
png.Encode(buf, img)
return buf.Bytes(), nil
}
return tile.Data, nil
}
func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error) {
cachedtile, err := tr.tdb.GetTile(tc)
if err != nil {
return nil, err
}
if cachedtile != nil {
reader := bytes.NewReader(cachedtile.Data)
cachedimg, err := png.Decode(reader)
if err != nil {
return nil, err
}
return cachedimg.(*image.NRGBA), nil
}
2019-01-17 13:27:31 +03:00
log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("RenderImage")
2019-01-17 13:20:03 +03:00
var layer *layerconfig.Layer
for _, l := range(tr.layers) {
if l.Id == tc.LayerId {
layer = &l
}
}
if layer == nil {
return nil, errors.New("No layer found")
}
2019-01-17 17:37:33 +03:00
if tc.Zoom > 13 || tc.Zoom < 1 {
return nil, errors.New("Invalid zoom")
}
2019-01-17 12:04:09 +03:00
if tc.Zoom == 13 {
//max zoomed in on mapblock level
2019-01-17 13:20:03 +03:00
mbr := coords.GetMapBlockRangeFromTile(tc, 0)
mbr.Pos1.Y = layer.From
mbr.Pos2.Y = layer.To
2019-01-17 17:37:33 +03:00
//count blocks
2019-01-17 17:14:13 +03:00
count, err := tr.dba.CountBlocks(mbr.Pos1, mbr.Pos2)
if err != nil {
return nil, err
}
if count == 0 {
return nil, nil
}
2019-01-17 13:20:03 +03:00
return tr.mapblockrenderer.Render(mbr.Pos1, mbr.Pos2)
}
2019-01-17 17:37:33 +03:00
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
}
2019-01-17 13:20:03 +03:00
}
//zoom 1-12
quads := tc.GetZoomedQuadrantsFromTile()
upperLeft, err := tr.RenderImage(quads.UpperLeft)
if err != nil {
return nil, err
}
2019-01-17 12:04:09 +03:00
2019-01-17 13:20:03 +03:00
upperRight, err := tr.RenderImage(quads.UpperRight)
if err != nil {
return nil, err
2019-01-17 12:04:09 +03:00
}
2019-01-17 13:20:03 +03:00
lowerLeft, err := tr.RenderImage(quads.LowerLeft)
if err != nil {
return nil, err
}
lowerRight, err := tr.RenderImage(quads.LowerRight)
if err != nil {
return nil, err
}
2019-01-17 18:18:50 +03:00
isEmpty := upperLeft == nil && upperRight == nil && lowerLeft == nil && lowerRight == nil
2019-01-17 18:35:28 +03:00
if isEmpty && (tc.Zoom == 11 || tc.Zoom == 10) {
2019-01-17 18:18:50 +03:00
//don't cache empty zoomed tiles
return nil, nil
}
2019-01-17 13:20:03 +03:00
img := image.NewNRGBA(
image.Rectangle{
image.Point{0, 0},
image.Point{IMG_SIZE, IMG_SIZE},
},
)
rect := image.Rect(0, 0, 128, 128)
2019-01-17 13:27:31 +03:00
if upperLeft != nil {
draw.Draw(img, rect, upperLeft, image.ZP, draw.Src)
}
2019-01-17 13:20:03 +03:00
rect = image.Rect(128, 0, 256, 128)
2019-01-17 13:27:31 +03:00
if upperRight != nil {
draw.Draw(img, rect, upperRight, image.ZP, draw.Src)
}
2019-01-17 13:20:03 +03:00
rect = image.Rect(0, 128, 128, 256)
2019-01-17 13:27:31 +03:00
if lowerLeft != nil {
draw.Draw(img, rect, lowerLeft, image.ZP, draw.Src)
}
2019-01-17 13:20:03 +03:00
rect = image.Rect(128, 128, 256, 256)
2019-01-17 13:27:31 +03:00
if lowerRight != nil {
draw.Draw(img, rect, lowerRight, image.ZP, draw.Src)
}
2019-01-17 13:20:03 +03:00
2019-01-17 13:38:13 +03:00
buf := new(bytes.Buffer)
if img != nil {
png.Encode(buf, img)
}
2019-01-17 18:09:11 +03:00
2019-01-17 13:38:13 +03:00
tile := tiledb.Tile{Pos: tc, Data: buf.Bytes(), Mtime: time.Now().Unix()}
tr.tdb.SetTile(&tile)
2019-01-17 13:20:03 +03:00
return img, nil
2019-01-11 18:00:40 +03:00
}