Skip to content

Commit

Permalink
adding a CountMethod to default pixel selction and including tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan McCarthy committed Feb 16, 2024
1 parent 874f141 commit c629411
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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)

# 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

0 comments on commit c629411

Please sign in to comment.