From 35cda0669b46df7a2eebf42e3feec85beed6b524 Mon Sep 17 00:00:00 2001 From: Victor Engmark Date: Wed, 10 Nov 2021 14:44:15 +1300 Subject: [PATCH] feat: Demonstrate asset datetime summary investigation Looks like it would be simple to extend the `summaries` property to allow asset-specific summaries by referring back to the definition of the `summaries` property. You can try the notebook yourself by running `nix-shell --run 'jupyter lab extensions/linz/notebooks/asset-datetime-summary.ipynb'`. --- .../notebooks/asset-datetime-summary.ipynb | 191 ++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 extensions/linz/notebooks/asset-datetime-summary.ipynb diff --git a/extensions/linz/notebooks/asset-datetime-summary.ipynb b/extensions/linz/notebooks/asset-datetime-summary.ipynb new file mode 100644 index 00000000..5673ddd3 --- /dev/null +++ b/extensions/linz/notebooks/asset-datetime-summary.ipynb @@ -0,0 +1,191 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4b29124d-9e52-4124-8b9f-efb60762c0dc", + "metadata": {}, + "source": [ + "# Creating asset summaries" + ] + }, + { + "cell_type": "markdown", + "id": "d38f9eb9-e10e-4903-9772-8b887e3cdef5", + "metadata": {}, + "source": [ + "We'll need a *LINZ extension schema validator:*" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7b2d67c6-3313-4023-9752-49d133d86d59", + "metadata": {}, + "outputs": [], + "source": [ + "from json import load\n", + "from sys import stderr\n", + "from urllib.request import urlopen\n", + "\n", + "from jsonschema import Draft7Validator\n", + "\n", + "with urlopen(\"https://stac.linz.govt.nz/v0.0.10/linz/schema.json\") as schema_pointer:\n", + " schema = load(schema_pointer)\n", + "\n", + "validator = Draft7Validator(schema)\n", + "\n", + "def validate(instance: str) -> None:\n", + " for error in validator.iter_errors(instance):\n", + " print(error.message, file=stderr)" + ] + }, + { + "cell_type": "markdown", + "id": "5e32302e-6714-494f-bbe0-23a60d9b383e", + "metadata": { + "tags": [] + }, + "source": [ + "The *collection example* is valid:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3931b743-885d-47ec-aff4-48faa8c6da5b", + "metadata": {}, + "outputs": [], + "source": [ + "with urlopen(\"https://stac.linz.govt.nz/v0.0.10/linz/examples/collection.json\") as example_pointer:\n", + " example = load(example_pointer)\n", + "\n", + "validate(example)" + ] + }, + { + "cell_type": "markdown", + "id": "f0e21a05-f00f-4923-a457-b3a7f25907d8", + "metadata": {}, + "source": [ + "Can we *summarise assets?*" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "76ec8b0b-6d5a-4902-a0b1-6b1415b577f3", + "metadata": {}, + "outputs": [], + "source": [ + "example[\"summaries\"][\"assets\"] = {\n", + " \"created\": {\"minimum\": \"1999-01-01T00:00:00Z\", \"maximum\": \"2010-01-01T00:00:00Z\"},\n", + " \"updated\": {\"minimum\": \"1999-01-02T00:00:00Z\", \"maximum\": \"2010-01-02T00:00:00Z\"},\n", + "}\n", + "validate(example)" + ] + }, + { + "cell_type": "markdown", + "id": "3cb90d1c-d25f-46e5-9885-c3d4f303ef09", + "metadata": {}, + "source": [ + "Yes, but *are such properties validated as normal summaries?* What if there's an invalid datetime or unexpected structure?" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2f043e5c-11a9-4ad8-9d6b-4ce8fe4cb1c0", + "metadata": {}, + "outputs": [], + "source": [ + "example[\"summaries\"][\"assets\"] = {\n", + " \"created\": None,\n", + " \"updated\": {\"minimum\": \"1999-01-02T00:00:00Z\"},\n", + "}\n", + "validate(example)" + ] + }, + { + "cell_type": "markdown", + "id": "16355abb-5da3-4486-9ae7-4f450330c508", + "metadata": {}, + "source": [ + "Oops, that should've caused a *validation exception!* Let's instead extend the extension schema to validate asset summaries as normal summaries:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "4cd36c8b-ac96-4ea3-a4a2-0db92ef07675", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "None is not valid under any of the given schemas\n", + "{'minimum': '1999-01-02T00:00:00Z'} is not valid under any of the given schemas\n" + ] + } + ], + "source": [ + "schema[\"definitions\"][\"fields\"][\"properties\"][\"summaries\"][\"properties\"][\"assets\"] = {\n", + " \"$ref\": \"https://schemas.stacspec.org/v1.0.0/collection-spec/json-schema/collection.json#definitions/summaries\"\n", + "}\n", + "validate(example)" + ] + }, + { + "cell_type": "markdown", + "id": "bdaa981e-5c50-4f9f-a7d2-6b0bef7c314f", + "metadata": {}, + "source": [ + "Looks like that worked – the assets summary is validated like any other summary. Let's make sure by reverting to the valid summary:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "8e7cfdb5-1232-40fd-957b-9b01f7448401", + "metadata": {}, + "outputs": [], + "source": [ + "example[\"summaries\"][\"assets\"] = {\n", + " \"created\": {\"minimum\": \"1999-01-01T00:00:00Z\", \"maximum\": \"2010-01-01T00:00:00Z\"},\n", + " \"updated\": {\"minimum\": \"1999-01-02T00:00:00Z\", \"maximum\": \"2010-01-02T00:00:00Z\"},\n", + "}\n", + "validate(example)" + ] + }, + { + "cell_type": "markdown", + "id": "6ee3378f-5507-4831-8611-ce56c52304ac", + "metadata": {}, + "source": [ + "And we're golden!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python3 - stac", + "language": "python", + "name": "ipython_stac" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}