diff --git a/morecantile/scripts/cli.py b/morecantile/scripts/cli.py index f5cb96f..d5eb0d5 100644 --- a/morecantile/scripts/cli.py +++ b/morecantile/scripts/cli.py @@ -2,6 +2,7 @@ import json import logging +import pathlib import sys import click @@ -411,7 +412,7 @@ def tiles(ctx, zoom, input, identifier, seq, tms): # noqa: C901 def tms(identifier): """Print TMS JSON.""" tms = morecantile.tms.get(identifier) - click.echo(tms.json(exclude_none=True)) + click.echo(tms.model_dump_json(exclude_none=True)) ################################################################################ @@ -465,7 +466,7 @@ def custom( extent_crs=CRS.from_epsg(extent_epsg) if extent_epsg else None, title=title or "Custom TileMatrixSet", ) - click.echo(tms.json(exclude_none=True)) + click.echo(tms.model_dump_json(exclude_none=True)) ################################################################################ @@ -608,3 +609,58 @@ def tms_to_geojson( # noqa: C901 "features": features, } click.echo(json.dumps(feature_collection, **dump_kwds)) + + +################################################################################ +# The `viz`` command. +@cli.command(short_help="Visualize a TMS") +@click.argument("input", type=click.File(mode="r"), default="-", required=False) +@click.option( + "--host", + type=str, + default="127.0.0.1", + help="Webserver host url (default: 127.0.0.1)", +) +@click.option("--port", type=int, default=8080, help="Webserver port (default: 8080)") +def viz(input, host, port): + """Visualize A TMS.""" + try: + import uvicorn + from fastapi import FastAPI + from fastapi.requests import Request + from fastapi.responses import HTMLResponse + from fastapi.templating import Jinja2Templates + + except ImportError: # pragma: nocover + FastAPI = None # type: ignore + Request = None # type: ignore + HTMLResponse = None # type: ignore + Jinja2Templates = None # type: ignore + uvicorn = None # type: ignore + + assert FastAPI, "'fastapi' needs to be installed in the python environemment to use `viz` command" + assert uvicorn, "'uvicorn' needs to be installed in the python environemment to use `viz` command" + + template_dir = str(pathlib.Path(__file__).parent.joinpath("templates")) + templates = Jinja2Templates(directory=template_dir) + + tms = morecantile.TileMatrixSet(**json.load(input)) + + app = FastAPI() + + @app.get( + "/", + response_class=HTMLResponse, + ) + async def viewer(request: Request): + """Handle /index.html.""" + return templates.TemplateResponse( + name="index.html", + context={ + "request": request, + "tms": tms, + }, + media_type="text/html", + ) + + uvicorn.run(app=app, host=host, port=port, log_level="info") diff --git a/morecantile/scripts/templates/index.html b/morecantile/scripts/templates/index.html new file mode 100644 index 0000000..9a2b106 --- /dev/null +++ b/morecantile/scripts/templates/index.html @@ -0,0 +1,253 @@ + + + + + TileMatrixSet viewer + + + + + + + + + + + + + + + + + +
+
+
+
+ +
+
+
+
+
+ + + + +