87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// GetMbtilesByName 获取指定名称的MBTiles瓦片
|
|
// @Summary 获取指定名称的MBTiles瓦片
|
|
// @Description 获取指定名称的MBTiles瓦片
|
|
// @Tags MBTiles
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param name path string true "MBTiles名称"
|
|
// @Success 200 {object} model.TileJSON
|
|
// @Router /mbtiles/{name} [get]
|
|
func (a *API) GetMbtilesByName(c echo.Context) error {
|
|
name := c.Param("name")
|
|
|
|
if a.store.MBTiles[name] == nil {
|
|
return c.String(http.StatusBadRequest, "MBTiles not found")
|
|
}
|
|
|
|
data, err := a.store.MBTiles[name].GetTileJSON()
|
|
data.Tiles = []string{fmt.Sprintf("http://localhost:8080/api/v1/mbtiles/%s/{z}/{x}/{y}", name)}
|
|
if err != nil {
|
|
return c.String(http.StatusBadRequest, "Failed to get metadata")
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, data)
|
|
}
|
|
|
|
// GetMbtilesByNameAndZAndXAndY 获取指定名称、z、x、y的MBTiles瓦片
|
|
// @Summary 获取指定名称、z、x、y的MBTiles瓦片
|
|
// @Description 获取指定名称、z、x、y的MBTiles瓦片
|
|
// @Tags MBTiles
|
|
// @Accept json
|
|
// @Produce application/octet-stream
|
|
// @Param name path string true "MBTiles名称"
|
|
// @Param z path string true "z值"
|
|
// @Param x path string true "x值"
|
|
// @Param y path string true "y值"
|
|
// @Success 200 {string} string "成功"
|
|
// @Router /mbtiles/{name}/{z}/{x}/{y} [get]
|
|
func (a *API) GetMbtilesByNameAndZAndXAndY(c echo.Context) error {
|
|
name := c.Param("name")
|
|
|
|
if a.store.MBTiles[name] == nil {
|
|
return c.String(http.StatusBadRequest, "MBTiles not found")
|
|
}
|
|
|
|
z := c.Param("z")
|
|
zValue, err := strconv.Atoi(z)
|
|
if err != nil {
|
|
return c.String(http.StatusBadRequest, "Invalid z value")
|
|
}
|
|
x := c.Param("x")
|
|
xValue, err := strconv.Atoi(x)
|
|
if err != nil {
|
|
return c.String(http.StatusBadRequest, "Invalid x value")
|
|
}
|
|
y := c.Param("y")
|
|
yValue, err := strconv.Atoi(y)
|
|
if err != nil {
|
|
return c.String(http.StatusBadRequest, "Invalid y value")
|
|
}
|
|
|
|
if a.store.MBTiles[name] == nil {
|
|
return c.String(http.StatusBadRequest, "MBTiles not found")
|
|
}
|
|
|
|
data, resType, err := a.store.MBTiles[name].GetTile(zValue, xValue, yValue)
|
|
|
|
if err != nil {
|
|
return c.String(http.StatusNotFound, "Tile not found")
|
|
}
|
|
|
|
if len(data) > 0 && data[0] == 0x1f && data[1] == 0x8b {
|
|
c.Response().Header().Set("Content-Encoding", "gzip")
|
|
}
|
|
|
|
return c.Blob(http.StatusOK, resType, data)
|
|
}
|