From 931773de3a5bfee2dc692e6f75ef530056640e81 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Wed, 26 Apr 2023 10:08:59 +0200 Subject: [PATCH 1/2] sketch --- ruff.toml | 1 + src/eoapi/raster/eoapi/raster/app.py | 53 +- .../raster/templates/mosaic-builder.html | 557 ++++++++++++++++++ src/eoapi/vector/eoapi/vector/app.py | 1 - src/eoapi/vector/eoapi/vector/config.py | 1 - 5 files changed, 610 insertions(+), 3 deletions(-) create mode 100644 src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html diff --git a/ruff.toml b/ruff.toml index b8ef5c8..d92bf79 100644 --- a/ruff.toml +++ b/ruff.toml @@ -2,6 +2,7 @@ select = [ "D1", # pydocstyle errors "E", # pycodestyle errors "W", # pycodestyle warnings + "F", # flake8 "C", # flake8-comprehensions "B", # flake8-bugbear ] diff --git a/src/eoapi/raster/eoapi/raster/app.py b/src/eoapi/raster/eoapi/raster/app.py index 254832b..bc81bb9 100644 --- a/src/eoapi/raster/eoapi/raster/app.py +++ b/src/eoapi/raster/eoapi/raster/app.py @@ -4,9 +4,11 @@ from typing import Dict import pystac -from fastapi import Depends, FastAPI, Query +from fastapi import Depends, FastAPI, Path, Query from psycopg import OperationalError +from psycopg.types.json import Jsonb from psycopg_pool import PoolTimeout +from rio_tiler.io import STACReader from starlette.middleware.cors import CORSMiddleware from starlette.requests import Request from starlette.responses import HTMLResponse @@ -59,6 +61,55 @@ add_map_viewer=True, add_mosaic_list=True, ) + + +@mosaic.router.get("/builder", response_class=HTMLResponse) +async def mosaic_builder_ui(request: Request): + """Mosaic Builder Viewer.""" + # TO DO: implement paging + with request.app.state.dbpool.connection() as conn: + with conn.cursor() as cursor: + cursor.execute( + "SELECT * FROM collections;", + ) + collections = [t[2] for t in cursor.fetchall() if t] + + return templates.TemplateResponse( + name="mosaic-builder.html", + context={ + "request": request, + "register_endpoint": mosaic.url_for(request, "register_search"), + "collection_endpoint": str( + request.url_for( + "get_asset_collection_info", collectionId="${collection}" + ) + ), + "collections": collections, + }, + media_type="text/html", + ) + + +# `Secret` endpoint for mosaic builder. Do not need to be public (in the OpenAPI docs) +@app.get("/collections/{collectionId}/assets/metadata", include_in_schema=False) +async def get_asset_collection_info(request: Request, collectionId: str = Path()): + """Collection asset metadata.""" + with request.app.state.dbpool.connection() as conn: + with conn.cursor() as cursor: + args = { + "collections": [collectionId], + "limit": 1, + } + cursor.execute("SELECT * FROM search(%s);", (Jsonb(args),)) + items = cursor.fetchone()[0].get("features", []) + + if items: + with STACReader(None, items[0]) as stac: + return stac.info() + + return {} + + app.include_router(mosaic.router, tags=["Mosaic"], prefix="/mosaic") ############################################################################### diff --git a/src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html b/src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html new file mode 100644 index 0000000..6712f01 --- /dev/null +++ b/src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html @@ -0,0 +1,557 @@ + + + + + Mosaic Builder + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ +
+
+
+
+ + + + diff --git a/src/eoapi/vector/eoapi/vector/app.py b/src/eoapi/vector/eoapi/vector/app.py index 995fe07..0dc78da 100644 --- a/src/eoapi/vector/eoapi/vector/app.py +++ b/src/eoapi/vector/eoapi/vector/app.py @@ -1,6 +1,5 @@ """tipg app.""" -from typing import Any, List import jinja2 from fastapi import FastAPI, Request diff --git a/src/eoapi/vector/eoapi/vector/config.py b/src/eoapi/vector/eoapi/vector/config.py index d4093b3..36098bd 100644 --- a/src/eoapi/vector/eoapi/vector/config.py +++ b/src/eoapi/vector/eoapi/vector/config.py @@ -1,6 +1,5 @@ """API settings.""" -from functools import lru_cache import pydantic From de625e91adc8a009074d6195ff6032cd47def90c Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Wed, 26 Apr 2023 10:19:35 +0200 Subject: [PATCH 2/2] update metadata --- .../eoapi/raster/templates/mosaic-builder.html | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html b/src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html index 6712f01..f679ce6 100644 --- a/src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html +++ b/src/eoapi/raster/eoapi/raster/templates/mosaic-builder.html @@ -226,6 +226,7 @@ +

Mosaic Metadata Specification

@@ -470,7 +471,14 @@ document.getElementById('btn-query').addEventListener('click', () => { // CQL2 query - body = {limit: scope.limit, 'filter-lang': 'cql2-json'} + body = { + 'filter-lang': 'cql2-json', + metadata: {}, + } + + if (scope.collection_info) { + body.metadata.assets = Object.keys(scope.collection_info) + } filter = {op: 'and', args: []} // geom filter @@ -479,8 +487,8 @@ var feat = draws.features[0]; filter.args.push({"op": "s_intersects", "args": [{"property": "geometry"}, feat.geometry]}) - // We add the bbox in the query to be used by titiler-pgstac TileJSON endpoint - body.bbox = turf.bbox(feat); + // We add the bbox in the metadata to be used by titiler-pgstac TileJSON endpoint + body.metadata.bounds = turf.bbox(feat); } // date filters