mapserver/server/web/tiles.go

62 lines
1.2 KiB
Go
Raw Normal View History

2019-01-18 12:44:10 +03:00
package web
import (
2019-02-15 11:08:22 +03:00
"github.com/prometheus/client_golang/prometheus"
2019-01-23 15:13:32 +03:00
"image/color"
2019-01-18 13:04:37 +03:00
"mapserver/app"
"mapserver/coords"
2019-01-23 14:56:45 +03:00
"mapserver/tilerenderer"
2019-01-18 13:04:37 +03:00
"net/http"
"strconv"
"strings"
2019-02-08 16:25:07 +03:00
)
2019-01-18 12:44:10 +03:00
type Tiles struct {
2019-02-07 21:48:05 +03:00
ctx *app.App
blank []byte
}
func (t *Tiles) Init() {
t.blank = tilerenderer.CreateBlankTile(color.RGBA{255, 255, 255, 255})
2019-01-18 12:44:10 +03:00
}
2019-01-18 13:04:37 +03:00
func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/tile/")
2019-01-28 12:42:07 +03:00
// {layerId}/x/y/zoom
2019-01-18 13:04:37 +03:00
parts := strings.Split(str, "/")
if len(parts) != 4 {
resp.WriteHeader(500)
resp.Write([]byte("wrong number of arguments"))
return
}
2019-01-18 12:44:10 +03:00
2019-02-15 10:36:48 +03:00
timer := prometheus.NewTimer(tileServeDuration)
defer timer.ObserveDuration()
2019-01-18 13:04:37 +03:00
layerid, _ := strconv.Atoi(parts[0])
x, _ := strconv.Atoi(parts[1])
y, _ := strconv.Atoi(parts[2])
zoom, _ := strconv.Atoi(parts[3])
2019-01-18 12:44:10 +03:00
2019-01-18 13:04:37 +03:00
c := coords.NewTileCoords(x, y, zoom, layerid)
2019-02-09 20:05:40 +03:00
tile, err := t.ctx.TileDB.GetTile(c)
2019-01-18 12:44:10 +03:00
2019-01-18 13:04:37 +03:00
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
} else {
resp.Header().Add("content-type", "image/png")
2019-01-21 13:21:56 +03:00
if tile == nil {
2019-02-07 21:48:05 +03:00
resp.Write(t.blank)
2019-01-23 14:56:45 +03:00
//TODO: cache/layer color
2019-01-21 13:21:56 +03:00
} else {
2019-02-09 20:05:40 +03:00
tilesCumulativeSize.Add(float64(len(tile)))
resp.Write(tile)
2019-01-21 15:27:36 +03:00
2019-01-21 13:21:56 +03:00
}
2019-01-18 13:04:37 +03:00
}
2019-01-18 12:44:10 +03:00
}