-
Notifications
You must be signed in to change notification settings - Fork 51
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
Handle reading multi-band datasetes #146
Draft
gjoseph92
wants to merge
1
commit into
main
Choose a base branch
from
multiband
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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,34 @@ | ||
from typing import Tuple, Union | ||
import re | ||
|
||
import numpy as np | ||
from rasterio.windows import Window | ||
|
||
State = Tuple[np.dtype, Union[int, float]] | ||
|
||
|
||
def nodata_for_window( | ||
ndim: int, window: Window, fill_value: Union[int, float], dtype: np.dtype | ||
): | ||
return np.full((ndim, window.height, window.width), fill_value, dtype) | ||
|
||
|
||
def exception_matches(e: Exception, patterns: Tuple[Exception, ...]) -> bool: | ||
""" | ||
Whether an exception matches one of the pattern exceptions | ||
|
||
Parameters | ||
---------- | ||
e: | ||
The exception to check | ||
patterns: | ||
Instances of an Exception type to catch, where ``str(exception_pattern)`` | ||
is a regex pattern to match against ``str(e)``. | ||
""" | ||
e_type = type(e) | ||
e_msg = str(e) | ||
for pattern in patterns: | ||
if issubclass(e_type, type(pattern)): | ||
if re.match(str(pattern), e_msg): | ||
return True | ||
return False |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,14 @@ | ||
from __future__ import annotations | ||
from typing import Optional, Protocol, Tuple, Type, TYPE_CHECKING, TypeVar, Union | ||
from typing import ( | ||
Optional, | ||
Protocol, | ||
Sequence, | ||
Tuple, | ||
Type, | ||
TYPE_CHECKING, | ||
TypeVar, | ||
Union, | ||
) | ||
|
||
import numpy as np | ||
|
||
|
@@ -30,6 +39,7 @@ def __init__( | |
self, | ||
*, | ||
url: str, | ||
bands: Optional[Sequence[int]], | ||
spec: RasterSpec, | ||
resampling: Resampling, | ||
dtype: np.dtype, | ||
|
@@ -45,6 +55,9 @@ def __init__( | |
---------- | ||
url: | ||
Fetch data from the asset at this URL. | ||
bands: | ||
List of (one-indexed!) band indices to read, or None for all bands. | ||
If None, the asset must have exactly one band. | ||
spec: | ||
Reproject data to match this georeferencing information. | ||
resampling: | ||
|
@@ -69,7 +82,6 @@ def __init__( | |
where ``str(exception_pattern)`` is a regex pattern to match against | ||
``str(raised_exception)``. | ||
""" | ||
# TODO colormaps? | ||
|
||
def read(self, window: Window) -> np.ndarray: | ||
""" | ||
|
@@ -87,7 +99,7 @@ def read(self, window: Window) -> np.ndarray: | |
|
||
Returns | ||
------- | ||
array: The window of data read | ||
array: The window of data read from all bands, as a 3D array | ||
""" | ||
... | ||
|
||
|
@@ -113,11 +125,16 @@ class FakeReader: | |
or inherent to the dask graph. | ||
""" | ||
|
||
def __init__(self, *, dtype: np.dtype, **kwargs) -> None: | ||
def __init__( | ||
self, *, bands: Optional[Sequence[int]], dtype: np.dtype, **kwargs | ||
) -> None: | ||
self.dtype = dtype | ||
self.ndim = len(bands) if bands is not None else 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oof, |
||
|
||
def read(self, window: Window, **kwargs) -> np.ndarray: | ||
return np.random.random((window.height, window.width)).astype(self.dtype) | ||
return np.random.random((self.ndim, window.height, window.width)).astype( | ||
self.dtype | ||
) | ||
|
||
def close(self) -> None: | ||
pass | ||
|
Oops, something went wrong.
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.
FYI, this is where we'd actually figure out band counts from STAC metadata