1
0
forked from MTSR/mapserver

perf and update fixes

This commit is contained in:
Thomas Rudin 2019-02-07 19:48:05 +01:00
parent a5a281cc21
commit 438ca52211
5 changed files with 41 additions and 50 deletions

View File

@ -37,7 +37,7 @@ var RealtimeTileLayer = L.TileLayer.extend({
createTile: function(coords){
var tile = document.createElement('img');
tile.src = this.getTileSource(coords.x, coords.y, coords.z);
tile.src = this.getTileSource(coords.x, coords.y, coords.z, true);
tile.id = this.getImageId(coords.x, coords.y, coords.z);
return tile;
}

View File

@ -44,63 +44,48 @@ const (
IMG_SIZE = 256
)
func (tr *TileRenderer) Render(tc *coords.TileCoords, recursionDepth int) ([]byte, error) {
func (tr *TileRenderer) Render(tc *coords.TileCoords) ([]byte, error) {
//No tile in db
img, data, err := tr.RenderImage(tc, 2)
//Check cache
tile, err := tr.tdb.GetTile(tc)
if err != nil {
return nil, err
}
if tile == nil {
if recursionDepth == 0 {
log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("Skip image")
return nil, nil
}
//No tile in db
img, data, err := tr.RenderImage(tc, recursionDepth)
if err != nil {
return nil, err
}
if img == nil {
//empty tile
return nil, nil
}
return data, nil
if img == nil {
//empty tile
return nil, nil
}
return tile.Data, nil
return data, nil
}
func (tr *TileRenderer) RenderImage(tc *coords.TileCoords, recursionDepth int) (*image.NRGBA, []byte, error) {
cachedtile, err := tr.tdb.GetTile(tc)
if err != nil {
return nil, nil, err
}
if cachedtile != nil {
reader := bytes.NewReader(cachedtile.Data)
cachedimg, err := png.Decode(reader)
if recursionDepth < 2 {
cachedtile, err := tr.tdb.GetTile(tc)
if err != nil {
return nil, nil, err
}
rect := image.Rectangle{
image.Point{0, 0},
image.Point{IMG_SIZE, IMG_SIZE},
if cachedtile != nil {
reader := bytes.NewReader(cachedtile.Data)
cachedimg, err := png.Decode(reader)
if err != nil {
return nil, nil, err
}
rect := image.Rectangle{
image.Point{0, 0},
image.Point{IMG_SIZE, IMG_SIZE},
}
img := image.NewNRGBA(rect)
draw.Draw(img, rect, cachedimg, image.ZP, draw.Src)
log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("Cached image")
return img, cachedtile.Data, nil
}
img := image.NewNRGBA(rect)
draw.Draw(img, rect, cachedimg, image.ZP, draw.Src)
log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("Cached image")
return img, cachedtile.Data, nil
}
if recursionDepth == 0 {

View File

@ -7,11 +7,8 @@ import (
func worker(ctx *app.App, coords <-chan *coords.TileCoords) {
for tc := range coords {
//remove tile
ctx.Objectdb.RemoveTile(tc)
//render tile
_, err := ctx.Tilerenderer.Render(tc, 5)
_, err := ctx.Tilerenderer.Render(tc)
if err != nil {
panic(err)
}

View File

@ -1,11 +1,12 @@
package web
import (
"github.com/sirupsen/logrus"
"mapserver/app"
"mapserver/vfs"
"net/http"
"strconv"
"github.com/sirupsen/logrus"
)
func Serve(ctx *app.App) {
@ -18,7 +19,10 @@ func Serve(ctx *app.App) {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev)))
mux.Handle("/api/tile/", &Tiles{ctx: ctx})
tiles := &Tiles{ctx: ctx}
tiles.Init()
mux.Handle("/api/tile/", tiles)
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
mux.Handle("/api/minetest", &Minetest{ctx: ctx})
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})

View File

@ -11,7 +11,12 @@ import (
)
type Tiles struct {
ctx *app.App
ctx *app.App
blank []byte
}
func (t *Tiles) Init() {
t.blank = tilerenderer.CreateBlankTile(color.RGBA{255, 255, 255, 255})
}
func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
@ -40,7 +45,7 @@ func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("content-type", "image/png")
if tile == nil {
resp.Write(tilerenderer.CreateBlankTile(color.RGBA{255, 255, 255, 255}))
resp.Write(t.blank)
//TODO: cache/layer color
} else {