1
0
forked from MTSR/mapserver
mapserver/server/web/tiles.go

51 lines
968 B
Go
Raw Normal View History

2019-01-18 12:44:10 +03:00
package web
import (
2019-01-18 13:04:37 +03:00
"mapserver/app"
"mapserver/coords"
2019-01-23 14:56:45 +03:00
"mapserver/tilerenderer"
"image/color"
2019-01-18 13:04:37 +03:00
"net/http"
"strconv"
"strings"
2019-01-18 12:44:10 +03:00
)
type Tiles struct {
2019-01-18 13:04:37 +03:00
ctx *app.App
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/")
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-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-01-21 17:13:33 +03:00
tile, err := t.ctx.Objectdb.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-01-23 14:56:45 +03:00
resp.Write(tilerenderer.CreateBlankTile(color.RGBA{0,0,0,0}))
//TODO: cache/layer color
2019-01-21 13:21:56 +03:00
} else {
resp.Write(tile.Data)
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
}