XYZ vs TMS tiling scheme #582
Unanswered
guillemc23
asked this question in
Q&A
Replies: 3 comments
-
Hi @guillemc23 Sadly there is no easy way to do this, I think you'll have to create custom TilerFactory like from typing import Optional, Literal, List, Tuple
from dataclasses import dataclass
from fastapi import Depends, Query, Path
from titiler.core import factory
from titiler.core.factory import img_endpoint_params
from titiler.core.resources.enums import ImageType
from titiler.core.dependencies import RescalingParams
from starlette.responses import Response
@dataclass
class TilerFactory(factory.TilerFactory):
def tile(self): # noqa: C901
"""Register /tiles endpoint."""
@self.router.get(r"/tiles/{z}/{x}/{y}", **img_endpoint_params)
@self.router.get(r"/tiles/{z}/{x}/{y}.{format}", **img_endpoint_params)
@self.router.get(r"/tiles/{z}/{x}/{y}@{scale}x", **img_endpoint_params)
@self.router.get(r"/tiles/{z}/{x}/{y}@{scale}x.{format}", **img_endpoint_params)
@self.router.get(r"/tiles/{TileMatrixSetId}/{z}/{x}/{y}", **img_endpoint_params)
@self.router.get(
r"/tiles/{TileMatrixSetId}/{z}/{x}/{y}.{format}", **img_endpoint_params
)
@self.router.get(
r"/tiles/{TileMatrixSetId}/{z}/{x}/{y}@{scale}x", **img_endpoint_params
)
@self.router.get(
r"/tiles/{TileMatrixSetId}/{z}/{x}/{y}@{scale}x.{format}",
**img_endpoint_params,
)
def tile(
z: int = Path(..., ge=0, le=30, description="TMS tiles's zoom level"),
x: int = Path(..., description="TMS tiles's column"),
y: int = Path(..., description="TMS tiles's row"),
TileMatrixSetId: Literal[tuple(self.supported_tms.list())] = Query(
self.default_tms,
description=f"TileMatrixSet Name (default: '{self.default_tms}')",
),
scale: int = Query(
1, gt=0, lt=4, description="Tile size scale. 1=256x256, 2=512x512..."
),
format: ImageType = Query(
None, description="Output image type. Default is auto."
),
src_path=Depends(self.path_dependency),
layer_params=Depends(self.layer_dependency),
dataset_params=Depends(self.dataset_dependency),
buffer: Optional[float] = Query(
None,
gt=0,
title="Tile buffer.",
description="Buffer on each side of the given tile. It must be a multiple of `0.5`. Output **tilesize** will be expanded to `tilesize + 2 * buffer` (e.g 0.5 = 257x257, 1.0 = 258x258).",
),
post_process=Depends(self.process_dependency),
rescale: Optional[List[Tuple[float, ...]]] = Depends(RescalingParams),
color_formula: Optional[str] = Query(
None,
title="Color Formula",
description="rio-color formula (info: https://github.com/mapbox/rio-color)",
),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
env=Depends(self.environment_dependency),
scheme: Literal[("xyz", "tms")] Query("tms", description="Tile Schema (default to xyz).")
):
"""Create map tile from a dataset."""
if schema == "tms":
y = (2 ** z) - y - 1
tms = self.supported_tms.get(TileMatrixSetId)
with rasterio.Env(**env):
with self.reader(src_path, tms=tms, **reader_params) as src_dst:
image = src_dst.tile(
x,
y,
z,
tilesize=scale * 256,
buffer=buffer,
**layer_params,
**dataset_params,
)
dst_colormap = getattr(src_dst, "colormap", None)
if post_process:
image = post_process(image)
if rescale:
image.rescale(rescale)
if color_formula:
image.apply_color_formula(color_formula)
if not format:
format = ImageType.jpeg if image.mask.all() else ImageType.png
content = image.render(
img_format=format.driver,
colormap=colormap or dst_colormap,
**format.profile,
**render_params,
)
return Response(content, media_type=format.mediatype) |
Beta Was this translation helpful? Give feedback.
0 replies
-
well I've started a PR over #583. I'm not sure if it will get merged soon, I would need more 👀 and feedback 🙏 |
Beta Was this translation helpful? Give feedback.
0 replies
-
It is easy to do the work in frontend. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm running a Titiler server but my frontend is asking for XYZ tiles due to retrocompatibility with Google tiles. Is there any way to perform the conversion directly on my backend when asking for tiles using
/tiles/{z}/{x}/{y}
? I've tried using the/tiles/{z}/{x}/-{y}
as I saw it somewhere else but it didn't do the trick.Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions