-
Notifications
You must be signed in to change notification settings - Fork 3
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
Pv/grid extension #13
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
800cdd1
configure to use pre-commit with black, etc.
588bd96
remove unused import
eb5ef78
don't run 3.10 in ci
0e8b10c
add grid extension
f7200ea
remove file
0652aec
remove file
5a7cb96
Merge branch 'main' into pv/grid-extension
bea65e8
remove 3.7 from matrix
a381d41
remove 3.7 from matrix
bc0e3b7
remove files
865aefe
fix python_requires
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,107 @@ | ||
"""Implements the :stac-ext:`Grid Extension <grid>`.""" | ||
|
||
import re | ||
from typing import Any, Dict, Optional, Pattern, Set, Union, cast | ||
|
||
import pystac | ||
from pystac.extensions.base import ExtensionManagementMixin, PropertiesExtension | ||
from pystac.extensions.hooks import ExtensionHooks | ||
|
||
SCHEMA_URI: str = "https://stac-extensions.github.io/grid/v1.0.0/schema.json" | ||
PREFIX: str = "grid:" | ||
|
||
# Field names | ||
CODE_PROP: str = PREFIX + "code" # required | ||
|
||
CODE_REGEX: str = r"[A-Z]+-[-_.A-Za-z0-9]+" | ||
CODE_PATTERN: Pattern[str] = re.compile(CODE_REGEX) | ||
|
||
|
||
def validated_code(v: str) -> str: | ||
if not isinstance(v, str): | ||
raise ValueError("Invalid Grid code: must be str") | ||
if not CODE_PATTERN.fullmatch(v): | ||
raise ValueError( | ||
f"Invalid Grid code: {v}" f" does not match the regex {CODE_REGEX}" | ||
) | ||
return v | ||
|
||
|
||
class GridExtension( | ||
PropertiesExtension, | ||
ExtensionManagementMixin[Union[pystac.Item, pystac.Collection]], | ||
): | ||
"""A concrete implementation of :class:`GridExtension` on an :class:`~pystac.Item` | ||
that extends the properties of the Item to include properties defined in the | ||
:stac-ext:`Grid Extension <grid>`. | ||
|
||
This class should generally not be instantiated directly. Instead, call | ||
:meth:`GridExtension.ext` on an :class:`~pystac.Item` to extend it. | ||
|
||
.. code-block:: python | ||
|
||
>>> item: pystac.Item = ... | ||
>>> proj_ext = GridExtension.ext(item) | ||
""" | ||
|
||
item: pystac.Item | ||
"""The :class:`~pystac.Item` being extended.""" | ||
|
||
properties: Dict[str, Any] | ||
"""The :class:`~pystac.Item` properties, including extension properties.""" | ||
|
||
def __init__(self, item: pystac.Item): | ||
self.item = item | ||
self.properties = item.properties | ||
|
||
def __repr__(self) -> str: | ||
return "<ItemGridExtension Item id={}>".format(self.item.id) | ||
|
||
def apply(self, code: str) -> None: | ||
"""Applies Grid extension properties to the extended Item. | ||
|
||
Args: | ||
code : REQUIRED. The code of the Item's grid location. | ||
""" | ||
self.code = validated_code(code) | ||
|
||
@property | ||
def code(self) -> Optional[str]: | ||
"""Get or sets the latitude band of the datasource.""" | ||
return self._get_property(CODE_PROP, str) | ||
|
||
@code.setter | ||
def code(self, v: str) -> None: | ||
self._set_property(CODE_PROP, validated_code(v), pop_if_none=False) | ||
|
||
@classmethod | ||
def get_schema_uri(cls) -> str: | ||
return SCHEMA_URI | ||
|
||
@classmethod | ||
def ext(cls, obj: pystac.Item, add_if_missing: bool = False) -> "GridExtension": | ||
"""Extends the given STAC Object with properties from the :stac-ext:`Grid | ||
Extension <grid>`. | ||
|
||
This extension can be applied to instances of :class:`~pystac.Item`. | ||
|
||
Raises: | ||
|
||
pystac.ExtensionTypeError : If an invalid object type is passed. | ||
""" | ||
if isinstance(obj, pystac.Item): | ||
cls.validate_has_extension(obj, add_if_missing) | ||
return cast(GridExtension, GridExtension(obj)) | ||
else: | ||
raise pystac.ExtensionTypeError( | ||
f"Grid Extension does not apply to type '{type(obj).__name__}'" | ||
) | ||
|
||
|
||
class GridExtensionHooks(ExtensionHooks): | ||
schema_uri: str = SCHEMA_URI | ||
prev_extension_ids: Set[str] = set() | ||
stac_object_types = {pystac.STACObjectType.ITEM} | ||
|
||
|
||
Grid_EXTENSION_HOOKS: ExtensionHooks = GridExtensionHooks() |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filed