-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4648ae2
commit 266174d
Showing
4 changed files
with
122 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import logging | ||
from fastapi import APIRouter, HTTPException, Depends | ||
from fastapi.responses import StreamingResponse | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy import func | ||
from io import BytesIO | ||
|
||
from db.models import EntityOrm | ||
from db.session import get_session | ||
|
||
router = APIRouter() | ||
logger = logging.getLogger(__name__) | ||
|
||
# ============================================================ | ||
# Helper Funcs | ||
# ============================================================ | ||
|
||
|
||
# Validate tile x/y coordinates at the given zoom level | ||
def tile_is_valid(tile): | ||
size = 2 ** tile["zoom"] | ||
return ( | ||
0 <= tile["x"] < size | ||
and 0 <= tile["y"] < size | ||
and tile["format"] in ["pbf", "mvt"] | ||
) | ||
|
||
|
||
# Build the database query using SQLAlchemy ORM | ||
def build_db_query(tile, session: Session): | ||
envelope = func.ST_TileEnvelope(tile["zoom"], tile["x"], tile["y"]) | ||
bounds = func.ST_MakeEnvelope(-180, -85.0511287798066, 180, 85.0511287798066, 4326) | ||
|
||
geometries = ( | ||
session.query( | ||
EntityOrm.entity, | ||
EntityOrm.name, | ||
EntityOrm.reference, | ||
func.ST_AsMVTGeom(EntityOrm.geometry, envelope), | ||
) | ||
.filter( | ||
EntityOrm.dataset == tile["dataset"], | ||
EntityOrm.geometry.ST_Intersects(envelope), | ||
) | ||
.subquery() | ||
) | ||
|
||
# Build vector tile | ||
tile_data = session.query(func.ST_AsMVT(geometries, tile["dataset"])).scalar() | ||
|
||
return tile_data | ||
|
||
|
||
# ============================================================ | ||
# API Endpoints | ||
# ============================================================ | ||
|
||
|
||
@router.get("/tiles/{dataset}/{z}/{x}/{y}.{fmt}") | ||
async def read_tiles_from_postgres( | ||
dataset: str, | ||
z: int, | ||
x: int, | ||
y: int, | ||
fmt: str, | ||
session: Session = Depends(get_session), | ||
): | ||
|
||
tile = {"dataset": dataset, "zoom": z, "x": x, "y": y, "format": fmt} | ||
if not tile_is_valid(tile): | ||
raise HTTPException(status_code=400, detail=f"Invalid tile path: {tile}") | ||
|
||
tile_data = build_db_query(tile, session) | ||
if not tile_data: | ||
raise HTTPException(status_code=404, detail="Tile data not found") | ||
|
||
pbf_buffer = BytesIO(tile_data) | ||
resp_headers = { | ||
"Access-Control-Allow-Origin": "*", | ||
"Content-Type": "application/vnd.mapbox-vector-tile", | ||
} | ||
|
||
return StreamingResponse( | ||
pbf_buffer, media_type="vnd.mapbox-vector-tile", headers=resp_headers | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters