-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Adding mask to open_rasterio #2618
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
Changes from all commits
cb9de31
8f1fd70
5aa0d4c
66d52c8
8b1a263
68bdc7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -23,7 +23,7 @@ | |||||||
class RasterioArrayWrapper(BackendArray): | ||||||||
"""A wrapper around rasterio dataset objects""" | ||||||||
|
||||||||
def __init__(self, manager, lock, vrt_params=None): | ||||||||
def __init__(self, manager, lock, vrt_params=None, masked=False): | ||||||||
from rasterio.vrt import WarpedVRT | ||||||||
self.manager = manager | ||||||||
self.lock = lock | ||||||||
|
@@ -39,6 +39,7 @@ def __init__(self, manager, lock, vrt_params=None): | |||||||
if not np.all(np.asarray(dtypes) == dtypes[0]): | ||||||||
raise ValueError('All bands should have the same dtype') | ||||||||
self._dtype = np.dtype(dtypes[0]) | ||||||||
self.masked = masked | ||||||||
|
||||||||
@property | ||||||||
def dtype(self): | ||||||||
|
@@ -119,7 +120,9 @@ def _getitem(self, key): | |||||||
riods = self.manager.acquire(needs_lock=False) | ||||||||
if self.vrt_params is not None: | ||||||||
riods = WarpedVRT(riods, **self.vrt_params) | ||||||||
out = riods.read(band_key, window=window) | ||||||||
out = riods.read(band_key, window=window, masked=self.masked) | ||||||||
if self.masked: | ||||||||
out = np.ma.filled(out, np.nan) | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
if squeeze_axis: | ||||||||
out = np.squeeze(out, axis=squeeze_axis) | ||||||||
|
@@ -162,7 +165,7 @@ def default(s): | |||||||
|
||||||||
|
||||||||
def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, | ||||||||
lock=None): | ||||||||
lock=None, masked=False): | ||||||||
"""Open a file with rasterio (experimental). | ||||||||
|
||||||||
This should work with any file that rasterio can open (most often: | ||||||||
|
@@ -205,6 +208,8 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, | |||||||
:py:func:`dask.array.from_array`. By default, a global lock is | ||||||||
used to avoid issues with concurrent access to the same file when using | ||||||||
dask's multithreaded backend. | ||||||||
masked : bool, optional | ||||||||
If True, uses nodatavals to set values to NaN. Defaults to False. | ||||||||
|
||||||||
Returns | ||||||||
------- | ||||||||
|
@@ -275,6 +280,7 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, | |||||||
|
||||||||
# Attributes | ||||||||
attrs = dict() | ||||||||
encoding = dict() | ||||||||
# Affine transformation matrix (always available) | ||||||||
# This describes coefficients mapping pixel coordinates to CRS | ||||||||
# For serialization store as tuple of 6 floats, the last row being | ||||||||
|
@@ -298,9 +304,13 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, | |||||||
attrs['is_tiled'] = np.uint8(riods.is_tiled) | ||||||||
if hasattr(riods, 'nodatavals'): | ||||||||
# The nodata values for the raster bands | ||||||||
attrs['nodatavals'] = tuple( | ||||||||
nodatavals = tuple( | ||||||||
np.nan if nodataval is None else nodataval | ||||||||
for nodataval in riods.nodatavals) | ||||||||
if masked: | ||||||||
encoding['nodatavals'] = nodatavals | ||||||||
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. I thing setting the encoding should inline with the other if statements and should be something like: if mask and riods.nodata is not None:
encoding['_FillValue'] = riods.nodata 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. |
||||||||
else: | ||||||||
attrs['nodatavals'] = nodatavals | ||||||||
|
||||||||
# Parse extra metadata from tags, if supported | ||||||||
parsers = {'ENVI': _parse_envi} | ||||||||
|
@@ -319,15 +329,15 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, | |||||||
attrs[k] = v | ||||||||
|
||||||||
data = indexing.LazilyOuterIndexedArray( | ||||||||
RasterioArrayWrapper(manager, lock, vrt_params)) | ||||||||
RasterioArrayWrapper(manager, lock, vrt_params, masked=masked)) | ||||||||
|
||||||||
# this lets you write arrays loaded with rasterio | ||||||||
data = indexing.CopyOnWriteArray(data) | ||||||||
if cache and chunks is None: | ||||||||
data = indexing.MemoryCachedArray(data) | ||||||||
|
||||||||
result = DataArray(data=data, dims=('band', 'y', 'x'), | ||||||||
coords=coords, attrs=attrs) | ||||||||
coords=coords, attrs=attrs, encoding=encoding) | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
if chunks is not None: | ||||||||
from dask.base import tokenize | ||||||||
|
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.