timvt icon indicating copy to clipboard operation
timvt copied to clipboard

Magic naming for presimplified geometry columns

Open bitner opened this issue 3 years ago • 1 comments

EXPERIMENT

This adds support for a magic naming convention to use a certain geometry column at certain zoom levels.

A column geom_z7 has been added to the test table landsat_wrs in landsat_wrs.sql that has been simlified to a level that is appropriate at zoom levels <= 7.

If the geometry column is set to "geom" either by it being set via the geom parameter or being the first in geometry_columns, this will look for any other geometry columns that start with "geom" and end with _z[0-9]+. It will then look at the current zoom level and use the _z layer if it is appropriate.

So, for the example of landsat_wrs that has two geometry columns "geom" and "geom_z7" it will use the geometry from geom_z7 for zoom levels 1-7 and then switch to using "geom" for zoom levels greater than 7.

bitner avatar Jun 29 '22 19:06 bitner

summary of our slack chat:

I'm a bit worry of adding magic and pretty custom function. IMO we will be better served by using Function. My other concern is that it could also be a Frontend issue.

I get the needs of using different geometry for specific zoom level but this could be resolved by a proxy endpoint.



from timvt import factory
from timvt.dependencies import TileParams
from timvt.resources.enums import MimeTypes
from morecantile import Tile, TileMatrixSet, tms
from fastapi import Path
from dataclasses import dataclass
from starlette.responses import Response

styles = {
    "countries": [
        {"table": "public.countries", "minzoom": 0, "maxzoom": 1, "params": {"geom": "the_geom_z1"}},
        {"table": "public.countries", "minzoom": 1, "maxzoom": 2, "params": {"geom": "the_geom_z2"}},
        {"table": "public.countries", "minzoom": 2, "maxzoom": 3, "params": {"geom": "the_geom_z3"}},
        {"table": "public.countries", "minzoom": 3, "maxzoom": 18, "params": {"geom": "the_geom_z4"}},
    ]
}


def _get_style_info(style: str, zoom: int):
    style_info = styles[style]
    for meta in style_info:
        if zoom > meta["minzoom"] and zoom <= meta["maxzoom"]:
            return meta


@dataclass
class VectorTilerFactory(factory.VectorTilerFactory):

    def register_routes(self):
        """Post Init: register route and configure specific options."""
        super().register_routes()
        self.register_style()

    def register_style(self):

        @self.router.get(
            "/{style}/{z}/{x}/{y}",
            **factory.TILE_RESPONSE_PARAMS,
            tags=["Tiles"],
        )
        async def style(
            request: Request,
            style: str = Path(description="style name."),
            tile: Tile = Depends(TileParams),
        ):
            """Return vector tile."""
            pool = request.app.state.pool

            kwargs = queryparams_to_kwargs(request.query_params)
            tilematrix = tms.get("WebMercatorQuad")

            style_info = _get_style_info(style, tile.z)
            table_name = style_info["table"]
            
            table_catalog = getattr(request.app.state, "table_catalog", {})
            layer = Table(**table_catalog[table_name])

            # params to pass to the Layer
            params = style_info.get("params", {})

            content = await layer.get_tile(pool, tile, tilematrix, **kwargs, ****params)

            return Response(bytes(content), media_type=MimeTypes.pbf.value)

vincentsarago avatar Jun 30 '22 08:06 vincentsarago