Skip to content

Commit 7ad376e

Browse files
committed
add support for raster mbtiles
1 parent 6267264 commit 7ad376e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

server/guppy/endpoints/endpoints_rio_tiler.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def get_tile_for_layer(layer_name: str, style: str, db: Session, z: int, x: int,
3939
t = time.time()
4040
file_path = validate_layer_and_get_file_path(db, layer_name)
4141

42-
result = get_tile(file_path, z, x, y, style, values, colors)
42+
if file_path.endswith('.mbtiles'):
43+
result = get_tile_from_mbtiles(file_path, z, x, y)
44+
else:
45+
result = get_tile(file_path, z, x, y, style, values, colors)
4346
log_cache_info(t)
4447
return result
4548

@@ -124,6 +127,35 @@ def generate_colormap(min_val, max_val, value_points, colors):
124127
return final_colormap
125128

126129

130+
def get_tile_from_mbtiles(file_path: str, z: int, x: int, y: int) -> Response:
131+
"""
132+
Args:
133+
file_path: A string representing the path to the file.
134+
z: An integer representing the zoom level.
135+
x: An integer representing the x-coordinate of the tile.
136+
y: An integer representing the y-coordinate of the tile.
137+
138+
Returns:
139+
A Response object containing the rendered tile image in PNG format, or raises an HTTPException with a status code of 404 and a corresponding detail message.
140+
141+
"""
142+
y = (1 << z) - 1 - y
143+
try:
144+
uri = f'file:{file_path}?mode=ro'
145+
with sqlite3.connect(uri, uri=True) as conn:
146+
cursor = conn.cursor()
147+
cursor.execute("SELECT tile_data FROM tiles WHERE zoom_level=? AND tile_column=? AND tile_row=?", (z, x, y))
148+
tile = cursor.fetchone()
149+
if tile:
150+
return Response(bytes(tile[0]), media_type="image/png")
151+
else:
152+
return None
153+
except Exception as e:
154+
raise HTTPException(status_code=404, detail=str(e))
155+
except TileOutsideBounds:
156+
raise HTTPException(status_code=204, detail=f"Tile out of bounds {z} {x} {y}")
157+
158+
127159
@lru_cache(maxsize=128)
128160
def get_tile(file_path: str, z: int, x: int, y: int, style: str = None, values: str = None, colors: str = None) -> Response:
129161
"""

server/guppy/routes/tiles_router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def get_vector_tile(layer_name: str, z: int, x: int, y: int, db: Session =
2222
@router.get(r"/raster/{layer_name}/{z}/{x}/{y}.png", responses={200: {"content": {"image/png": {}}, "description": "Return an image.", }}, response_class=Response,
2323
description="Read COG and return a png tile")
2424
async def get_raster_tile(layer_name: str, z: int, x: int, y: int,
25-
style: str = Query(..., description=f"Style should be '<b>shader_rgba</b>', '<b>custom</b>' or one of {list(cmap.data.keys())} values. <br><br>"
25+
style: str = Query(default=None, description=f"Style should be '<b>shader_rgba</b>', '<b>custom</b>' or one of {list(cmap.data.keys())} values. <br><br>"
2626
f"If '<b>custom</b>', extra parameters values and colors are needed like:<br> values=1.23,80.35,190.587&colors=255,0,0,255_0,255,0,255_0,0,255,255 <br>"
2727
f"so values are comma seperated, and colors are r,g,b,a and _ seperated."),
2828
values: str = None, colors: str = None,

0 commit comments

Comments
 (0)