Skip to content

Commit 65b2513

Browse files
committed
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'`.
1 parent 614aa4c commit 65b2513

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "4b29124d-9e52-4124-8b9f-efb60762c0dc",
6+
"metadata": {},
7+
"source": [
8+
"# Creating asset summaries"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "d38f9eb9-e10e-4903-9772-8b887e3cdef5",
14+
"metadata": {},
15+
"source": [
16+
"We'll need a *LINZ extension schema validator:*"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"id": "7b2d67c6-3313-4023-9752-49d133d86d59",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"from json import load\n",
27+
"from urllib.request import urlopen\n",
28+
"\n",
29+
"from jsonschema import Draft7Validator\n",
30+
"\n",
31+
"with urlopen(\"https://stac.linz.govt.nz/v0.0.10/linz/schema.json\") as schema_pointer:\n",
32+
" schema = load(schema_pointer)\n",
33+
"\n",
34+
"validator = Draft7Validator(schema)"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"id": "5e32302e-6714-494f-bbe0-23a60d9b383e",
40+
"metadata": {
41+
"tags": []
42+
},
43+
"source": [
44+
"The *collection example* is valid:"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 2,
50+
"id": "3931b743-885d-47ec-aff4-48faa8c6da5b",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"with urlopen(\"https://stac.linz.govt.nz/v0.0.10/linz/examples/collection.json\") as example_pointer:\n",
55+
" example = load(example_pointer)\n",
56+
"\n",
57+
"validator.validate(example)"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"id": "f0e21a05-f00f-4923-a457-b3a7f25907d8",
63+
"metadata": {},
64+
"source": [
65+
"Can we *summarise assets?*"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 3,
71+
"id": "76ec8b0b-6d5a-4902-a0b1-6b1415b577f3",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"example[\"summaries\"][\"assets\"] = {\n",
76+
" \"created\": {\"minimum\": \"1999-01-01T00:00:00Z\", \"maximum\": \"2010-01-01T00:00:00Z\"},\n",
77+
" \"updated\": {\"minimum\": \"1999-01-02T00:00:00Z\", \"maximum\": \"2010-01-02T00:00:00Z\"},\n",
78+
"}\n",
79+
"validator.validate(example)"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"id": "3cb90d1c-d25f-46e5-9885-c3d4f303ef09",
85+
"metadata": {},
86+
"source": [
87+
"Yes, but *are such properties validated as normal summaries?* What if there's an invalid datetime or unexpected structure?"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 4,
93+
"id": "2f043e5c-11a9-4ad8-9d6b-4ce8fe4cb1c0",
94+
"metadata": {},
95+
"outputs": [],
96+
"source": [
97+
"example[\"summaries\"][\"assets\"] = {\n",
98+
" \"created\": {\"minimum\": \"not a datetime\", \"maximum\": \"2010-01-01T00:00:00Z\"},\n",
99+
" \"updated\": None,\n",
100+
"}\n",
101+
"validator.validate(example)"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"id": "16355abb-5da3-4486-9ae7-4f450330c508",
107+
"metadata": {},
108+
"source": [
109+
"Oops, that should've caused a *validation exception!* Let's instead extend the extension schema to validate asset summaries as normal summaries:"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 5,
115+
"id": "4cd36c8b-ac96-4ea3-a4a2-0db92ef07675",
116+
"metadata": {},
117+
"outputs": [
118+
{
119+
"ename": "ValidationError",
120+
"evalue": "None is not valid under any of the given schemas\n\nFailed validating 'anyOf' in schema['allOf'][3]['properties']['summaries']['properties']['assets']['additionalProperties']:\n {'anyOf': [{'allOf': [{'$ref': 'http://json-schema.org/draft-07/schema'}],\n 'minProperties': 1,\n 'title': 'JSON Schema',\n 'type': 'object'},\n {'properties': {'maximum': {'title': 'Maximum value',\n 'type': ['number', 'string']},\n 'minimum': {'title': 'Minimum value',\n 'type': ['number', 'string']}},\n 'required': ['minimum', 'maximum'],\n 'title': 'Range',\n 'type': 'object'},\n {'items': {'description': 'For each field only the original '\n 'data type of the property can '\n 'occur (except for arrays), but '\n \"we can't validate that in JSON \"\n 'Schema yet. See the sumamry '\n 'description in the STAC '\n 'specification for details.'},\n 'minItems': 1,\n 'title': 'Set of values',\n 'type': 'array'}]}\n\nOn instance['summaries']['assets']['updated']:\n None",
121+
"output_type": "error",
122+
"traceback": [
123+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
124+
"\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)",
125+
"\u001b[0;32m<ipython-input-5-a52ff90324ea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\"$ref\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"https://schemas.stacspec.org/v1.0.0/collection-spec/json-schema/collection.json#definitions/summaries\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m }\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mvalidator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalidate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexample\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
126+
"\u001b[0;32m/nix/store/flgn97ypbp74y7yvilld9wgggh1fzjwp-python3.8-jsonschema-3.2.0/lib/python3.8/site-packages/jsonschema/validators.py\u001b[0m in \u001b[0;36mvalidate\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 351\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mvalidate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 352\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0merror\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miter_errors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 353\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 354\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 355\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mis_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minstance\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
127+
"\u001b[0;31mValidationError\u001b[0m: None is not valid under any of the given schemas\n\nFailed validating 'anyOf' in schema['allOf'][3]['properties']['summaries']['properties']['assets']['additionalProperties']:\n {'anyOf': [{'allOf': [{'$ref': 'http://json-schema.org/draft-07/schema'}],\n 'minProperties': 1,\n 'title': 'JSON Schema',\n 'type': 'object'},\n {'properties': {'maximum': {'title': 'Maximum value',\n 'type': ['number', 'string']},\n 'minimum': {'title': 'Minimum value',\n 'type': ['number', 'string']}},\n 'required': ['minimum', 'maximum'],\n 'title': 'Range',\n 'type': 'object'},\n {'items': {'description': 'For each field only the original '\n 'data type of the property can '\n 'occur (except for arrays), but '\n \"we can't validate that in JSON \"\n 'Schema yet. See the sumamry '\n 'description in the STAC '\n 'specification for details.'},\n 'minItems': 1,\n 'title': 'Set of values',\n 'type': 'array'}]}\n\nOn instance['summaries']['assets']['updated']:\n None"
128+
]
129+
}
130+
],
131+
"source": [
132+
"schema[\"definitions\"][\"fields\"][\"properties\"][\"summaries\"][\"properties\"][\"assets\"] = {\n",
133+
" \"$ref\": \"https://schemas.stacspec.org/v1.0.0/collection-spec/json-schema/collection.json#definitions/summaries\"\n",
134+
"}\n",
135+
"validator.validate(example)"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"id": "bdaa981e-5c50-4f9f-a7d2-6b0bef7c314f",
141+
"metadata": {},
142+
"source": [
143+
"Looks like that worked – the assets summary is validated like any other summary. Let's make sure by reverting to the valid summary:"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": null,
149+
"id": "8e7cfdb5-1232-40fd-957b-9b01f7448401",
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"example[\"summaries\"][\"assets\"] = {\n",
154+
" \"created\": {\"minimum\": \"1999-01-01T00:00:00Z\", \"maximum\": \"2010-01-01T00:00:00Z\"},\n",
155+
" \"updated\": {\"minimum\": \"1999-01-02T00:00:00Z\", \"maximum\": \"2010-01-02T00:00:00Z\"},\n",
156+
"}\n",
157+
"validator.validate(example)"
158+
]
159+
},
160+
{
161+
"cell_type": "markdown",
162+
"id": "6ee3378f-5507-4831-8611-ce56c52304ac",
163+
"metadata": {},
164+
"source": [
165+
"And we're golden!"
166+
]
167+
}
168+
],
169+
"metadata": {
170+
"kernelspec": {
171+
"display_name": "Python3 - stac",
172+
"language": "python",
173+
"name": "ipython_stac"
174+
},
175+
"language_info": {
176+
"codemirror_mode": {
177+
"name": "ipython",
178+
"version": 3
179+
},
180+
"file_extension": ".py",
181+
"mimetype": "text/x-python",
182+
"name": "python",
183+
"nbconvert_exporter": "python",
184+
"pygments_lexer": "ipython3",
185+
"version": "3.8.9"
186+
}
187+
},
188+
"nbformat": 4,
189+
"nbformat_minor": 5
190+
}

0 commit comments

Comments
 (0)