Magic naming for presimplified geometry columns#88
Draft
Conversation
Member
|
summary of our slack chat: I'm a bit worry of adding 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) |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.