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

create titiler namespaces packages #284

Merged
merged 18 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox
pip install setuptools wheel twine

- name: Set tag version
id: tag
Expand All @@ -82,15 +82,16 @@ jobs:
- name: Set module version
id: module
# https://stackoverflow.com/questions/58177786/get-the-current-pushed-tag-in-github-actions
run: echo ::set-output name=version::$(python setup.py --version)
run: echo ::set-output name=version::$(python titiler/core/setup.py --version)

- name: Build and publish
if: steps.tag.outputs.tag == steps.module.outputs.version
env:
TOXENV: release
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: tox
run: |
scripts/publish

publish-docker:
needs: [tests]
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ repos:
]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.770
rev: v0.812
hooks:
- id: mypy
language_version: python3.8
args: ["--no-strict-optional", "--ignore-missing-imports"]
args: ["--no-strict-optional", "--ignore-missing-imports", "--explicit-package-bases", "--namespace-packages"]
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 0 additions & 5 deletions MANIFEST.in

This file was deleted.

17 changes: 17 additions & 0 deletions scripts/publish
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

SUBPACKAGE_DIRS=(
"core"
"mosaic"
"application"
)

for PACKAGE_DIR in "${SUBPACKAGE_DIRS[@]}"
do
echo "publishing titiler-${PACKAGE_DIR}"
pushd ./titiler/${PACKAGE_DIR}
rm -rf dist
python setup.py sdist
# twine upload dist/*
popd
done
44 changes: 40 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,53 @@ current_version = 0.2.0
commit = True
tag = True
tag_name = {new_version}
parse =
parse =
(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
((?P<pre>a|b|rc)(?P<prenum>\d+))?
serialize =
serialize =
{major}.{minor}.{patch}{pre}{prenum}
{major}.{minor}.{patch}

[bumpversion:file:setup.py]
[bumpversion:titiler/application:setup.py]
search = version="{current_version}"
replace = version="{new_version}"

[bumpversion:file:titiler/__init__.py]
[bumpversion:file:titiler/application/titiler/application/version.py]
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
search = __version__ = "{current_version}"
replace = __version__ = "{new_version}"

[bumpversion:titiler/core:setup.py]
search = version="{current_version}"
replace = version="{new_version}"

[bumpversion:file:titiler/core/titiler/core/version.py]
search = __version__ = "{current_version}"
replace = __version__ = "{new_version}"


[bumpversion:titiler/mosaic:setup.py]
search = version="{current_version}"
replace = version="{new_version}"

[bumpversion:file:titiler/mosaic/titiler/mosaic/version.py]
search = __version__ = "{current_version}"
replace = __version__ = "{new_version}"


# Linter configs
[isort]
profile=black
known_first_party = titiler
forced_separate = fastapi,starlette
known_third_party = rasterio,morecantile,rio_tiler_crs,rio_tiler,cogeo_mosaic,brotli_asgi,geojson_pydantic
default_section = THIRDPARTY

[flake8]
ignore = D203
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist
max-complexity = 12
max-line-length = 90

[mypy]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the linters config to setup.cfg

no_strict_optional = true
ignore_missing_imports = True
3 changes: 0 additions & 3 deletions titiler/__init__.py

This file was deleted.

4 changes: 4 additions & 0 deletions titiler/application/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include titiler/application/templates/*.html

recursive-exclude tests *

1 change: 1 addition & 0 deletions titiler/application/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## titiler-application
46 changes: 46 additions & 0 deletions titiler/application/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Setup titiler-application."""

from setuptools import find_namespace_packages, setup

with open("README.md") as f:
long_description = f.read()

inst_reqs = [
"titiler-core",
"titiler-mosaic",
"brotli-asgi>=1.0.0",
"python-dotenv",
]
extra_reqs = {
"test": ["pytest", "pytest-cov", "pytest-asyncio", "requests"],
"server": ["uvicorn[standard]>=0.12.0,<0.14.0"],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uvicorn is optional (e.g when running the application in AWS Lambda you don't need Uvicorn)

}


setup(
name="titiler-application",
version="0.2.0",
description=u"A modern dynamic tile server built on top of FastAPI and Rasterio/GDAL.",
long_description=long_description,
long_description_content_type="text/markdown",
python_requires=">=3.6",
classifiers=[
"Intended Audience :: Information Technology",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
keywords="COG STAC MosaicJSON FastAPI",
author=u"Vincent Sarago",
author_email="[email protected]",
url="https://github.com/developmentseed/titiler",
license="MIT",
packages=find_namespace_packages(exclude=["tests*"]),
package_data={"titiler": ["application/templates/*.html"]},
include_package_data=True,
zip_safe=False,
install_requires=inst_reqs,
extras_require=extra_reqs,
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def set_env(monkeypatch):
@pytest.fixture(autouse=True)
def app(set_env) -> TestClient:
"""Create App."""
from titiler.main import app
from titiler.application.main import app

return TestClient(app)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,14 @@ def test_point(rio, app):


def test_file_not_found_error(app):
"""raise 404 when file is not found."""
response = app.get("/cog/info?url=foo.tif")
assert response.status_code == 404


@patch("rio_tiler.io.cogeo.rasterio")
def test_tile_outside_bounds_error(rio, app):
"""raise 404 when tile is not found."""
rio.open = mock_rasterio_open

response = app.get("/cog/tiles/15/0/0?url=https://myurl.com/cog.tif&rescale=0,1000")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cogeo_mosaic.backends import FileBackend
from cogeo_mosaic.mosaic import MosaicJSON

from titiler.models.mapbox import TileJSON
from titiler.core.models.mapbox import TileJSON

from ..conftest import DATA_DIR, parse_img, read_json_fixture

Expand Down Expand Up @@ -210,6 +210,7 @@ def test_wmts(app):


def test_mosaic_auth_error(app):
"""Raise auth error 401."""
response = app.get("/mosaicjson", params={"url": "s3://bucket/mosaic.json"})
assert response.status_code == 401

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Test titiler.main.app."""
"""Test titiler.application.main.app."""


def test_health(app):
Expand Down
3 changes: 3 additions & 0 deletions titiler/application/titiler/application/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""titiler.application"""

from .version import __version__ # noqa
98 changes: 98 additions & 0 deletions titiler/application/titiler/application/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""TiTiler demo custon dependencies."""

import json
from enum import Enum
from typing import Dict, Optional

import morecantile
from morecantile import tms
from morecantile.models import TileMatrixSet
from rasterio.crs import CRS
from rio_tiler.colormap import cmap, parse_color

from fastapi import HTTPException, Query

from starlette.templating import Jinja2Templates

try:
import importlib.resources as pkg_resources
except ImportError:
import importlib_resources as pkg_resources # type: ignore


with pkg_resources.path(__package__, "templates") as p:
templates = Jinja2Templates(directory=str(p))


# colors from https://daac.ornl.gov/ABOVE/guides/Annual_Landcover_ABoVE.html
above_cmap = {
1: [58, 102, 24, 255], # Evergreen Forest
2: [100, 177, 41, 255], # Deciduous Forest
3: [177, 177, 41, 255], # Shrubland
4: [221, 203, 154, 255], # Herbaceous
5: [218, 203, 47, 255], # Sparely Vegetated
6: [177, 177, 177, 255], # Barren
7: [175, 255, 205, 255], # Fen
8: [239, 255, 192, 255], # Bog
9: [144, 255, 255, 255], # Shallows/Littoral
10: [29, 0, 250, 255], # Water
}
cmap = cmap.register({"above": above_cmap})

ColorMapName = Enum( # type: ignore
"ColorMapName", [(a, a) for a in sorted(cmap.list())]
)

# CUSTOM TMS for EPSG:3413
EPSG3413 = morecantile.TileMatrixSet.custom(
(-4194300, -4194300, 4194300, 4194300),
CRS.from_epsg(3413),
identifier="EPSG3413",
matrix_scale=[2, 2],
)

# CUSTOM TMS for EPSG:6933
# info from https://epsg.io/6933
EPSG6933 = morecantile.TileMatrixSet.custom(
(-17357881.81713629, -7324184.56362408, 17357881.81713629, 7324184.56362408),
CRS.from_epsg(6933),
identifier="EPSG6933",
matrix_scale=[1, 1],
)
tms = tms.register([EPSG3413, EPSG6933])

TileMatrixSetName = Enum( # type: ignore
"TileMatrixSetName", [(a, a) for a in sorted(tms.list())]
)


def ColorMapParams(
colormap_name: ColorMapName = Query(None, description="Colormap name"),
colormap: str = Query(None, description="JSON encoded custom Colormap"),
) -> Optional[Dict]:
"""Colormap Dependency."""
if colormap_name:
return cmap.get(colormap_name.value)

if colormap:
try:
return json.loads(
colormap,
object_hook=lambda x: {int(k): parse_color(v) for k, v in x.items()},
)
except json.JSONDecodeError:
raise HTTPException(
status_code=400, detail="Could not parse the colormap value."
)

return None


def TMSParams(
TileMatrixSetId: TileMatrixSetName = Query(
TileMatrixSetName.WebMercatorQuad, # type: ignore
description="TileMatrixSet Name (default: 'WebMercatorQuad')",
)
) -> TileMatrixSet:
"""TileMatrixSet Dependency."""
return tms.get(TileMatrixSetId.name)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved all custom code to the application part

20 changes: 13 additions & 7 deletions titiler/main.py → ...r/application/titiler/application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

from brotli_asgi import BrotliMiddleware

from . import __version__ as titiler_version
from . import settings
from .endpoints import cog, mosaic, stac, tms
from .errors import DEFAULT_STATUS_CODES, add_exception_handlers
from .middleware import CacheControlMiddleware, LoggerMiddleware, TotalTimeMiddleware
from .templates import templates
from titiler.application.custom import templates
from titiler.application.middleware import (
CacheControlMiddleware,
LoggerMiddleware,
TotalTimeMiddleware,
)
from titiler.application.routers import cog, mosaic, stac, tms
from titiler.application.settings import ApiSettings
from titiler.application.version import __version__ as titiler_version
from titiler.core.errors import DEFAULT_STATUS_CODES, add_exception_handlers
from titiler.mosaic.errors import MOSAIC_STATUS_CODES

from fastapi import FastAPI

Expand All @@ -21,7 +26,7 @@
logging.getLogger("botocore.utils").disabled = True
logging.getLogger("rio-tiler").setLevel(logging.ERROR)

api_settings = settings.ApiSettings()
api_settings = ApiSettings()

app = FastAPI(
title=api_settings.name,
Expand All @@ -42,6 +47,7 @@

app.include_router(tms.router, tags=["TileMatrixSets"])
add_exception_handlers(app, DEFAULT_STATUS_CODES)
add_exception_handlers(app, MOSAIC_STATUS_CODES)


# Set all CORS enabled origins
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""titiler.application.routers"""
Loading