Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies to work with Titiler 0.15 #109

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
58 changes: 25 additions & 33 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name = "timvt"
description = "A lightweight PostGIS based dynamic vector tile server."
readme = "README.md"
requires-python = ">=3.8"
license = {file = "LICENSE"}
license = { file = "LICENSE" }
authors = [
{name = "Vincent Sarago", email = "[email protected]"},
{name = "David Bitner", email = "[email protected]"},
{ name = "Vincent Sarago", email = "[email protected]" },
{ name = "David Bitner", email = "[email protected]" },
]
keywords = ["FastAPI", "MVT", "POSTGIS"]
classifiers = [
Expand All @@ -26,9 +26,10 @@ dependencies = [
"buildpg>=0.3",
"fastapi>=0.87",
"jinja2>=2.11.2,<4.0.0",
"morecantile>=3.1,<4.0",
"morecantile>=5.0,<6.0",
"starlette-cramjam>=0.3,<0.4",
"importlib_resources>=1.1.0; python_version < '3.9'",
"pydantic-settings>=2.0.3",
"typing_extensions; python_version < '3.9.2'",
]

Expand All @@ -46,12 +47,8 @@ test = [
"numpy",
"sqlalchemy>=1.1,<1.4",
]
dev = [
"pre-commit",
]
server = [
"uvicorn[standard]>=0.12.0,<0.19.0",
]
dev = ["pre-commit"]
server = ["uvicorn[standard]>=0.12.0,<0.19.0"]
docs = [
"nbconvert",
"mkdocs",
Expand All @@ -71,22 +68,22 @@ path = "timvt/__init__.py"

[tool.hatch.build.targets.sdist]
exclude = [
"/tests",
"/dockerfiles",
"/docs",
"/demo",
"/data",
"docker-compose.yml",
"CONTRIBUTING.md",
"CHANGES.md",
".pytest_cache",
".history",
".github",
".env.example",
".bumpversion.cfg",
".flake8",
".gitignore",
".pre-commit-config.yaml",
"/tests",
"/dockerfiles",
"/docs",
"/demo",
"/data",
"docker-compose.yml",
"CONTRIBUTING.md",
"CHANGES.md",
".pytest_cache",
".history",
".github",
".env.example",
".bumpversion.cfg",
".flake8",
".gitignore",
".pre-commit-config.yaml",
]

[build-system]
Expand All @@ -96,13 +93,8 @@ build-backend = "hatchling.build"
[tool.isort]
profile = "black"
known_first_party = ["timvt"]
known_third_party = [
"morecantile",
]
forced_separate = [
"fastapi",
"starlette",
]
known_third_party = ["morecantile"]
forced_separate = ["fastapi", "starlette"]
default_section = "THIRDPARTY"

[tool.mypy]
Expand Down
2 changes: 1 addition & 1 deletion timvt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""timvt."""

__version__ = "0.8.0a3"
__version__ = "0.8.0a4"
5 changes: 2 additions & 3 deletions timvt/db.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""timvt.db: database events."""

from os import getenv
from typing import Any, Optional

import orjson
from buildpg import asyncpg

from fastapi import FastAPI
from timvt.dbmodel import get_table_index
from timvt.settings import PostgresSettings

from fastapi import FastAPI


async def con_init(conn):
"""Use json for json returns."""
Expand Down
5 changes: 3 additions & 2 deletions timvt/dbmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from buildpg import asyncpg
from pydantic import BaseModel, Field

from timvt.settings import TableSettings


Expand Down Expand Up @@ -237,7 +236,9 @@ async def get_table_index(
jsonb_build_object(
'name', attname,
'type', "type",
'description', description
'description', description,
'min', NULL,
'max', NULL
)
) FILTER (WHERE type LIKE 'timestamp%'), '[]'::jsonb) as datetime_columns,
coalesce(jsonb_agg(
Expand Down
30 changes: 13 additions & 17 deletions timvt/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@
from typing import Any, Callable, Dict, List, Literal, Optional
from urllib.parse import urlencode

from fastapi import APIRouter, Depends, Path, Query
from fastapi.params import Param
from morecantile import Tile, TileMatrixSet
from morecantile import tms as morecantile_tms
from morecantile.defaults import TileMatrixSets

from timvt.dependencies import LayerParams, TileParams
from timvt.layer import Function, Layer, Table
from timvt.models.mapbox import TileJSON
from timvt.models.OGC import TileMatrixSetList
from timvt.resources.enums import MimeTypes

from fastapi import APIRouter, Depends, Path, Query

from starlette.datastructures import QueryParams
from starlette.requests import Request
from starlette.responses import HTMLResponse, Response
from starlette.routing import NoMatchFound
from starlette.templating import Jinja2Templates
from timvt.dependencies import LayerParams, TileParams
from timvt.layer import Function, Layer, Table
from timvt.models.mapbox import TileJSON
from timvt.models.OGC import TileMatrixSetList
from timvt.resources.enums import MimeTypes

try:
from importlib.resources import files as resources_files # type: ignore
Expand Down Expand Up @@ -114,10 +112,9 @@ def register_tiles(self):
async def tile(
request: Request,
tile: Tile = Depends(TileParams),
TileMatrixSetId: Literal[tuple(self.supported_tms.list())] = Query(
self.default_tms,
description=f"TileMatrixSet Name (default: '{self.default_tms}')",
),
TileMatrixSetId: Literal[
tuple(self.supported_tms.list())
] = self.default_tms,
layer=Depends(self.layer_dependency),
):
"""Return vector tile."""
Expand Down Expand Up @@ -146,10 +143,9 @@ async def tile(
async def tilejson(
request: Request,
layer=Depends(self.layer_dependency),
TileMatrixSetId: Literal[tuple(self.supported_tms.list())] = Query(
self.default_tms,
description=f"TileMatrixSet Name (default: '{self.default_tms}')",
),
TileMatrixSetId: Literal[
tuple(self.supported_tms.list())
] = self.default_tms,
minzoom: Optional[int] = Query(
None, description="Overwrite default minzoom."
),
Expand Down
34 changes: 23 additions & 11 deletions timvt/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from typing import Any, ClassVar, Dict, List, Optional

import morecantile
from buildpg import Func
from buildpg import Func, RawDangerous
from buildpg import Var as pg_variable
from buildpg import asyncpg, clauses, funcs, render, select_fields
from pydantic import BaseModel, root_validator

from timvt.dbmodel import Table as DBTable
from timvt.errors import (
InvalidGeometryColumnName,
Expand Down Expand Up @@ -38,12 +37,12 @@ class Layer(BaseModel, metaclass=abc.ABCMeta):
id: str
bounds: List[float] = [-180, -90, 180, 90]
crs: str = "http://www.opengis.net/def/crs/EPSG/0/4326"
title: Optional[str]
description: Optional[str]
title: Optional[str] = None
description: Optional[str] = None
minzoom: int = tile_settings.default_minzoom
maxzoom: int = tile_settings.default_maxzoom
default_tms: str = tile_settings.default_tms
tileurl: Optional[str]
tileurl: Optional[str] = None

@abc.abstractmethod
async def get_tile(
Expand Down Expand Up @@ -89,15 +88,22 @@ class Table(Layer, DBTable):

type: str = "Table"

@root_validator
@root_validator(pre=True)
def bounds_default(cls, values):
"""Get default bounds from the first geometry columns."""
geoms = values.get("geometry_columns")
if geoms:
# Get the Extent of all the bounds
minx, miny, maxx, maxy = zip(*[geom.bounds for geom in geoms])
def get_bounds(geom):
bounds = getattr(geom, "bounds", None)
if bounds is None:
bounds = geom["bounds"]
return bounds

minx, miny, maxx, maxy = zip(*[get_bounds(geom) for geom in geoms])
values["bounds"] = [min(minx), min(miny), max(maxx), max(maxy)]
values["crs"] = f"http://www.opengis.net/def/crs/EPSG/0/{geoms[0].srid}"
srid = geoms[0]["srid"]
values["crs"] = f"http://www.opengis.net/def/crs/EPSG/0/{srid}"

return values

Expand Down Expand Up @@ -151,6 +157,12 @@ async def get_tile(
tms_srid = tms.crs.to_epsg()
tms_proj = tms.crs.to_proj4()

# This may be more valid but it doesn't add quotes around the column names
# _fields = select_fields(*cols)

_fields = [f'"{f}"' for f in cols]
_fields = ", ".join(_fields)

async with pool.acquire() as conn:
sql_query = """
WITH
Expand Down Expand Up @@ -203,7 +215,7 @@ async def get_tile(
sql_query,
tablename=pg_variable(self.id),
geometry_column=pg_variable(geometry_column.name),
fields=select_fields(*cols),
fields=RawDangerous(_fields),
xmin=bbox.left,
ymin=bbox.bottom,
xmax=bbox.right,
Expand Down Expand Up @@ -239,9 +251,9 @@ class Function(Layer):
type: str = "Function"
sql: str
function_name: Optional[str]
options: Optional[List[Dict[str, Any]]]
options: Optional[List[Dict[str, Any]]] = None

@root_validator
@root_validator(pre=True)
def function_name_default(cls, values):
"""Define default function's name to be same as id."""
function_name = values.get("function_name")
Expand Down
14 changes: 7 additions & 7 deletions timvt/models/mapbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ class TileJSON(BaseModel):

tilejson: str = "2.2.0"
name: Optional[str]
description: Optional[str]
description: Optional[str] = None
version: str = "1.0.0"
attribution: Optional[str]
template: Optional[str]
legend: Optional[str]
attribution: Optional[str] = None
template: Optional[str] = None
legend: Optional[str] = None
scheme: SchemeEnum = SchemeEnum.xyz
tiles: List[str]
grids: Optional[List[str]]
data: Optional[List[str]]
grids: Optional[List[str]] = None
data: Optional[List[str]] = None
minzoom: int = Field(0, ge=0, le=30)
maxzoom: int = Field(30, ge=0, le=30)
bounds: List[float] = [-180, -90, 180, 90]
center: Optional[Tuple[float, float, int]]

@root_validator
@root_validator(pre=True)
def compute_center(cls, values):
"""Compute center if it does not exist."""
bounds = values["bounds"]
Expand Down
Loading