Skip to content

implement browsable extension validation #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 45 additions & 44 deletions src/stac_api_validator/validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
from typing import Tuple
from typing import Union

import pystac
import yaml
from deepdiff import DeepDiff
from more_itertools import take
from pystac import Catalog
from pystac import Collection
from pystac import Item
from pystac import ItemCollection
Expand Down Expand Up @@ -96,6 +98,7 @@ class Context(Enum):
ITEM_SEARCH_FILTER = "Item Search - Filter Ext"
FEATURES_FILTER = "Features - Filter Ext"
CHILDREN = "Children Ext"
BROWSEABLE = "Browseable Ext"

def __str__(self) -> str:
return self.value
Expand Down Expand Up @@ -528,21 +531,15 @@ def validate_api(

if "browseable" in conformance_classes:
logger.info("Validating STAC API - Browseable conformance class.")
validate_browseable(landing_page_body, errors, warnings)
else:
logger.info("Skipping STAC API - Browseable conformance class.")
validate_browseable(landing_page_body, errors, warnings, r_session)

if "children" in conformance_classes:
logger.info("Validating STAC API - Children conformance class.")
validate_children(landing_page_body, errors, warnings, r_session)
else:
logger.info("Skipping STAC API - Children conformance class.")

if "collections" in conformance_classes:
logger.info("Validating STAC API - Collections conformance class.")
validate_collections(landing_page_body, collection, errors, warnings, r_session)
else:
logger.info("Skipping STAC API - Collections conformance class.")

conforms_to = landing_page_body.get("conformsTo", [])

Expand All @@ -558,8 +555,6 @@ def validate_api(
errors,
r_session,
)
else:
logger.info("Skipping STAC API - Features conformance class.")

if "item-search" in conformance_classes:
logger.info("Validating STAC API - Item Search conformance class.")
Expand All @@ -574,8 +569,6 @@ def validate_api(
conformance_classes=conformance_classes,
r_session=r_session,
)
else:
logger.info("Skipping STAC API - Item Search conformance class.")

if not errors:
try:
Expand Down Expand Up @@ -683,13 +676,53 @@ def validate_core(
r_session=r_session,
)

# this validates, among other things, that the child and item link relations reference
# valid STAC Catalogs, Collections, and/or Items
try:
list(take(1000, Catalog.from_dict(root_body).get_all_items()))
except pystac.errors.STACTypeError as e:
errors += (
f"[{Context.CORE}] Error while traversing Catalog child/item links to find Items: {e} "
"This can be reproduced with 'list(pystac.Catalog.from_file(root_url).get_all_items())'"
)


def validate_browseable(
root_body: Dict[str, Any],
errors: Errors,
warnings: Warnings,
r_session: Session,
) -> None:
logger.info("Browseable validation is not yet implemented.")
# child or item links exist in the root
child_links = links_by_rel(root_body.get("links"), "child")
item_links = links_by_rel(root_body.get("links"), "item")
if not (child_links or item_links):
errors += f"[{Context.BROWSEABLE}] /: Root catalog does not contain any child or item link relations"

# check that at least a few of the items that can be reached from child/item link relations
# can be found through search
try:
for item in take(10, Catalog.from_dict(root_body).get_all_items()):
if link := link_by_rel(root_body.get("links"), "search"):
_, body, _ = retrieve(
Method.GET,
link["href"],
errors,
Context.BROWSEABLE,
params={"ids": item.id, "collections": item.collection},
r_session=r_session,
)
if body and len(body.get("features", [])) != 1:
errors += f"[{Context.BROWSEABLE}] /: Link[rel=children] must href /children"
else:
errors += (
f"[{Context.BROWSEABLE}] /: Link[rel=search] could not be found"
)
except pystac.errors.STACTypeError as e:
errors += (
f"[{Context.BROWSEABLE}] Error while traversing Catalog child/item links to find Items: {e}. "
"This can be reproduced with 'pystac.Catalog.from_file(root_url).get_all_items()'"
)


def validate_children(
Expand Down Expand Up @@ -1093,10 +1126,6 @@ def validate_features(
errors=errors,
r_session=r_session,
)
else:
logger.info(
"Skipping STAC API - Features - Filter Extension conformance class."
)


def validate_item_search(
Expand Down Expand Up @@ -1227,10 +1256,6 @@ def validate_item_search(
errors=errors,
r_session=r_session,
)
else:
logger.info(
"Skipping STAC API - Item Search - Filter Extension conformance class."
)


def validate_filter_queryables(
Expand Down Expand Up @@ -1333,10 +1358,6 @@ def validate_item_search_filter(
logger.info(
"Validating STAC API - Item Search - Filter Extension - CQL2-Text conformance class."
)
else:
logger.info(
"Skipping STAC API - Item Search - Filter Extension - CQL2-Text conformance class."
)

cql2_json_supported = (
"http://www.opengis.net/spec/cql2/1.0/conf/cql2-json" in conforms_to
Expand All @@ -1349,10 +1370,6 @@ def validate_item_search_filter(
logger.info(
"Validating STAC API - Item Search - Filter Extension - CQL2-JSON conformance class."
)
else:
logger.info(
"Skipping STAC API - Item Search - Filter Extension - CQL2-JSON conformance class."
)

basic_cql2_supported = (
"http://www.opengis.net/spec/cql2/1.0/conf/basic-cql2" in conforms_to
Expand All @@ -1365,10 +1382,6 @@ def validate_item_search_filter(
logger.info(
"Validating STAC API - Item Search - Filter Extension - Basic CQL2 conformance class."
)
else:
logger.info(
"Skipping STAC API - Item Search - Filter Extension - Basic CQL2 conformance class."
)

advanced_comparison_operators_supported = (
"http://www.opengis.net/spec/cql2/1.0/conf/advanced-comparison-operators"
Expand All @@ -1379,10 +1392,6 @@ def validate_item_search_filter(
logger.info(
"Validating STAC API - Item Search - Filter Extension - Advanced Comparison Operators conformance class."
)
else:
logger.info(
"Skipping STAC API - Item Search - Filter Extension - Advanced Comparison Operators conformance class."
)

basic_spatial_operators_supported = (
"http://www.opengis.net/spec/cql2/1.0/conf/basic-spatial-operators"
Expand All @@ -1393,10 +1402,6 @@ def validate_item_search_filter(
logger.info(
"Validating STAC API - Item Search - Filter Extension - Basic Spatial Operators conformance class."
)
else:
logger.info(
"Skipping STAC API - Item Search - Filter Extension - Basic Spatial Operators conformance class."
)

temporal_operators_supported = (
"http://www.opengis.net/spec/cql2/1.0/conf/temporal-operators" in conforms_to
Expand All @@ -1406,10 +1411,6 @@ def validate_item_search_filter(
logger.info(
"Validating STAC API - Item Search - Filter Extension - Temporal Operators conformance class."
)
else:
logger.info(
"Skipping STAC API - Item Search - Filter Extension - Temporal Operators conformance class."
)

# todo: validate these
# Spatial Operators: http://www.opengis.net/spec/cql2/1.0/conf/spatial-operators
Expand Down