55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
from pathlib import Path
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response
|
|
from pydantic import BaseModel
|
|
|
|
from ..dependencies import get_vector_mbtiles_service
|
|
from ...services.vector_mbtiles import VectorMBTilesError, VectorMBTilesService
|
|
|
|
|
|
router = APIRouter(tags=["vector-tiles"])
|
|
|
|
|
|
class ActivateVectorMBTiles(BaseModel):
|
|
path: str
|
|
|
|
|
|
@router.post("/tiles/vector/activate")
|
|
async def activate_vector_mbtiles(
|
|
payload: ActivateVectorMBTiles,
|
|
service: VectorMBTilesService = Depends(get_vector_mbtiles_service),
|
|
):
|
|
try:
|
|
info = service.activate(Path(payload.path))
|
|
except VectorMBTilesError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc))
|
|
return {"status": "ok", "path": str(info.path), "format": info.metadata.get("format")}
|
|
|
|
|
|
@router.get("/tiles/vector/metadata")
|
|
async def vector_metadata(service: VectorMBTilesService = Depends(get_vector_mbtiles_service)):
|
|
try:
|
|
return service.get_metadata()
|
|
except VectorMBTilesError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc))
|
|
|
|
|
|
@router.get("/tiles/vector/{z}/{x}/{y}.pbf")
|
|
async def vector_tile(
|
|
z: int,
|
|
x: int,
|
|
y: int,
|
|
service: VectorMBTilesService = Depends(get_vector_mbtiles_service),
|
|
):
|
|
try:
|
|
data = service.get_tile(z=z, x=x, y_xyz=y)
|
|
except KeyError:
|
|
raise HTTPException(status_code=404, detail="tile not found")
|
|
except VectorMBTilesError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc))
|
|
|
|
headers = {}
|
|
if len(data) >= 2 and data[0] == 0x1F and data[1] == 0x8B:
|
|
headers["Content-Encoding"] = "gzip"
|
|
return Response(content=data, media_type="application/x-protobuf", headers=headers)
|
|
|