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

41 lines
793 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"
"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)
data, err := t.ctx.Tilerenderer.Render(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")
resp.Write(data)
}
2019-01-18 12:44:10 +03:00
}