-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for dataset categories (for now optional)
Categories will be added to keywords. PiperOrigin-RevId: 729274084
- Loading branch information
1 parent
7c3daa6
commit 3f538cd
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Checks that the "gee:categories" field is valid.""" | ||
|
||
from typing import Iterator | ||
|
||
from checker import stac | ||
|
||
# Use dashes, not underscores, as the HTML catalog converts | ||
# underscores to dashes anyway. | ||
CATEGORIES = frozenset([ | ||
'agriculture', | ||
'atmosphere', | ||
'climate', | ||
'cryosphere', | ||
'ecosystems', | ||
'elevation-topography', | ||
'fire', | ||
'forest-biomass', | ||
'hydrology', | ||
'infrastructure-boundaries', | ||
'landuse-landcover', | ||
'oceanography', | ||
'orthophotos', | ||
'plant-productivity', | ||
'population', | ||
'precipitation', | ||
'satellite-imagery', | ||
'soil', | ||
'vegetation-indices', | ||
]) | ||
|
||
GEE_CATEGORIES = 'gee:categories' | ||
|
||
|
||
class Check(stac.NodeCheck): | ||
"""Checks the gee:categories field.""" | ||
|
||
name = 'gee_categories' | ||
|
||
@classmethod | ||
def run(cls, node: stac.Node) -> Iterator[stac.Issue]: | ||
if node.type == stac.StacType.CATALOG: | ||
return | ||
|
||
if GEE_CATEGORIES not in node.stac: | ||
# TODO(b/397988701): require categories when all datasets have them | ||
# yield cls.new_issue(node, f'Missing {GEE_CATEGORIES}') | ||
return | ||
|
||
categories = node.stac[GEE_CATEGORIES] | ||
if not categories: | ||
yield cls.new_issue(node, f'"{GEE_CATEGORIES}" must not be empty') | ||
return | ||
|
||
if not isinstance(categories, list): | ||
yield cls.new_issue(node, f'"{GEE_CATEGORIES}" must be a list of strings') | ||
return | ||
|
||
extra_categories = set(categories) - CATEGORIES | ||
for extra in extra_categories: | ||
yield cls.new_issue( | ||
node, f'Found unknown category "{extra}" in "{GEE_CATEGORIES}"' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from absl.testing import absltest | ||
from checker import test_utils | ||
from checker.node import gee_categories | ||
|
||
|
||
class GeeCategoiresTest(test_utils.NodeTest): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.check = gee_categories.Check | ||
|
||
def test_catalog(self): | ||
self.assert_catalog({}) | ||
|
||
def test_not_a_list(self): | ||
self.assert_collection( | ||
{'gee:categories': 77}, '"gee:categories" must be a list of strings' | ||
) | ||
|
||
def test_missing_ok(self): | ||
self.assert_collection({}) | ||
|
||
# TODO(b/397988701): require categories when all datasets have them | ||
# def test_empty_categories_is_bad(self): | ||
# self.assert_collection({'gee:categories': []}, 'must not be empty') | ||
|
||
def test_valid_category(self): | ||
self.assert_collection({'gee:categories': ['soil']}) | ||
|
||
def test_bad_category(self): | ||
self.assert_collection( | ||
{'gee:categories': ['bogus']}, | ||
'Found unknown category "bogus" in "gee:categories"', | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
absltest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters