Skip to content
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

adding a CountMethod to default pixel selction and including tests #676

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions rio_tiler/mosaic/methods/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,32 @@ def feed(self, array: Optional[numpy.ma.MaskedArray]):
mask = numpy.where(pidex, array.mask, self.mosaic.mask)
self.mosaic = numpy.ma.where(pidex, array, self.mosaic)
self.mosaic.mask = mask


@dataclass
class CountMethod(MosaicMethodBase):
"""Stack the arrays and return the valid pixel count."""

stack: List[numpy.ma.MaskedArray] = field(default_factory=list, init=False)

@property
def data(self) -> Optional[numpy.ma.MaskedArray]:
"""Return valid data count of the data stack."""
if self.stack:
data = numpy.ma.count(numpy.ma.stack(self.stack, axis=0), axis=0).astype(numpy.uint16)
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

# only need the counts from one band
if len(data.shape) > 2:
data = data[0]

# mask is always empty
mask = numpy.zeros(data.shape, dtype=bool)
array = numpy.ma.MaskedArray(data, mask)

return array

return None

def feed(self, array: Optional[numpy.ma.MaskedArray]):
"""Add array to the stack."""
self.stack.append(array)
10 changes: 10 additions & 0 deletions tests/test_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ class aClass(object):
assert t.dtype == "uint16"
assert m.dtype == "uint8"

# Test count pixel selection
(t, m), _ = mosaic.mosaic_reader(
assets, _read_tile, x, y, z, pixel_selection=defaults.CountMethod()
)
assert t.shape == (1, 256, 256)
assert m.shape == (256, 256)
assert m.all()
assert t.dtype == "uint16"
assert m.dtype == "uint8"


def mock_rasterio_open(asset):
"""Mock rasterio Open."""
Expand Down
Loading