|
| 1 | +# /// script |
| 2 | +# dependencies = [ |
| 3 | +# "titiler.xarray[full]", |
| 4 | +# "starlette_cramjam", |
| 5 | +# "uvicorn", |
| 6 | +# ] |
| 7 | +# /// |
| 8 | +"""Example of Application.""" |
| 9 | + |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +import numpy |
| 13 | +import xarray |
| 14 | +from fastapi import FastAPI |
| 15 | +from rio_tiler.io.xarray import XarrayReader |
| 16 | +from starlette.middleware.cors import CORSMiddleware |
| 17 | +from starlette_cramjam.middleware import CompressionMiddleware |
| 18 | + |
| 19 | +from titiler.core.dependencies import DefaultDependency |
| 20 | +from titiler.core.errors import DEFAULT_STATUS_CODES, add_exception_handlers |
| 21 | +from titiler.core.factory import AlgorithmFactory, ColorMapFactory, TMSFactory |
| 22 | +from titiler.core.middleware import CacheControlMiddleware |
| 23 | +from titiler.xarray.factory import TilerFactory |
| 24 | + |
| 25 | + |
| 26 | +def XarrayDataArray() -> xarray.DataArray: |
| 27 | + """Custom Dependency which return a DataArray.""" |
| 28 | + arr = numpy.linspace(1, 1000, 1000 * 2000).reshape(1, 1000, 2000) |
| 29 | + data = xarray.DataArray( |
| 30 | + arr, |
| 31 | + dims=("time", "y", "x"), |
| 32 | + coords={ |
| 33 | + "x": numpy.arange(-170, 170, 0.17), |
| 34 | + "y": numpy.arange(-80, 80, 0.16), |
| 35 | + "time": [datetime(2022, 1, 1)], |
| 36 | + }, |
| 37 | + ) |
| 38 | + data.attrs.update({"valid_min": arr.min(), "valid_max": arr.max(), "fill_value": 0}) |
| 39 | + data.rio.write_crs("epsg:4326", inplace=True) |
| 40 | + return data |
| 41 | + |
| 42 | + |
| 43 | +app = FastAPI( |
| 44 | + title="TiTiler with support of Multidimensional dataset", |
| 45 | + openapi_url="/api", |
| 46 | + docs_url="/api.html", |
| 47 | + version="0.1.0", |
| 48 | +) |
| 49 | + |
| 50 | + |
| 51 | +md = TilerFactory( |
| 52 | + router_prefix="/md", |
| 53 | + # Use rio-tiler XarrayReader which accept xarray.DataArray as input |
| 54 | + reader=XarrayReader, |
| 55 | + # Use our custom dependency which return a xarray.DataArray |
| 56 | + path_dependency=XarrayDataArray, |
| 57 | + # Set the reader_dependency to `empty` |
| 58 | + reader_dependency=DefaultDependency, |
| 59 | +) |
| 60 | +app.include_router(md.router, prefix="/md", tags=["Multi Dimensional"]) |
| 61 | + |
| 62 | +# TileMatrixSets endpoints |
| 63 | +app.include_router(TMSFactory().router, tags=["Tiling Schemes"]) |
| 64 | + |
| 65 | +############################################################################### |
| 66 | +# Algorithms endpoints |
| 67 | +app.include_router( |
| 68 | + AlgorithmFactory().router, |
| 69 | + tags=["Algorithms"], |
| 70 | +) |
| 71 | + |
| 72 | +# Colormaps endpoints |
| 73 | +app.include_router( |
| 74 | + ColorMapFactory().router, |
| 75 | + tags=["ColorMaps"], |
| 76 | +) |
| 77 | + |
| 78 | +add_exception_handlers(app, DEFAULT_STATUS_CODES) |
| 79 | + |
| 80 | +# Set all CORS enabled origins |
| 81 | +app.add_middleware( |
| 82 | + CORSMiddleware, |
| 83 | + allow_origins="*", |
| 84 | + allow_credentials=True, |
| 85 | + allow_methods=["GET"], |
| 86 | + allow_headers=["*"], |
| 87 | +) |
| 88 | + |
| 89 | +app.add_middleware( |
| 90 | + CompressionMiddleware, |
| 91 | + minimum_size=0, |
| 92 | + exclude_mediatype={ |
| 93 | + "image/jpeg", |
| 94 | + "image/jpg", |
| 95 | + "image/png", |
| 96 | + "image/jp2", |
| 97 | + "image/webp", |
| 98 | + }, |
| 99 | + compression_level=6, |
| 100 | +) |
| 101 | + |
| 102 | +app.add_middleware( |
| 103 | + CacheControlMiddleware, |
| 104 | + cachecontrol="public, max-age=3600", |
| 105 | + exclude_path={r"/healthz"}, |
| 106 | +) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + import uvicorn |
| 111 | + |
| 112 | + uvicorn.run(app=app, host="127.0.0.1", port=8080, log_level="info") |
0 commit comments