Repository: cogeotiff/rio-tiler-mosaic

Issue:
Mean and Median should return the same data type ? 
A mean or a median value might not be the same datatype as the input tiles and thus it can lead to unexpected behaviour. 

IMO we should add an option to `cast` the output data to the same data type as the input tile array 

Comments:

Patch:
diff --git a/CHANGES.txt b/CHANGES.txt
index 3d9381a..4589e88 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,8 @@
+0.0.1dev2 (2019-07-18)
+--------------------
+- Force output data type to be the same as the input datatype 
+from Mean and Median method#4
+
 0.0.1dev1 (2019-07-18)
 --------------------
 
diff --git a/rio_tiler_mosaic/methods/defaults.py b/rio_tiler_mosaic/methods/defaults.py
index a5b6760..b5d0721 100644
--- a/rio_tiler_mosaic/methods/defaults.py
+++ b/rio_tiler_mosaic/methods/defaults.py
@@ -61,11 +61,18 @@ class LowestMethod(MosaicMethodBase):
 class MeanMethod(MosaicMethodBase):
     """Feed the mosaic tile with the Mean pixel value."""
 
+    def __init__(self, enforce_data_type=True):
+        """Overwrite base and init Mean method."""
+        super(MeanMethod, self).__init__()
+        self.enforce_data_type = enforce_data_type
+
     @property
     def data(self):
         """Return data and mask."""
         if self.tile is not None:
             tile = numpy.ma.mean(self.tile, axis=0)
+            if self.enforce_data_type:
+                tile = tile.astype(self.tile.dtype)
             return tile.data, ~tile.mask[0] * 255
         else:
             return None, None
@@ -82,11 +89,18 @@ class MeanMethod(MosaicMethodBase):
 class MedianMethod(MosaicMethodBase):
     """Feed the mosaic tile with the Median pixel value."""
 
+    def __init__(self, enforce_data_type=True):
+        """Overwrite base and init Median method."""
+        super(MedianMethod, self).__init__()
+        self.enforce_data_type = enforce_data_type
+
     @property
     def data(self):
         """Return data and mask."""
         if self.tile is not None:
             tile = numpy.ma.median(self.tile, axis=0)
+            if self.enforce_data_type:
+                tile = tile.astype(self.tile.dtype)
             return tile.data, ~tile.mask[0] * 255
         else:
             return None, None
diff --git a/setup.py b/setup.py
index c53ad3d..2e82b8b 100644
--- a/setup.py
+++ b/setup.py
@@ -16,7 +16,7 @@ with open("README.md") as f:
 
 setup(
     name="rio-tiler-mosaic",
-    version="0.0.1dev1",
+    version="0.0.1dev2",
     long_description=long_description,
     long_description_content_type="text/markdown",
     description=u"""A rio-tiler plugin to create mosaic tiles.""",


Test patch:
diff --git a/tests/test_mosaic.py b/tests/test_mosaic.py
index d15eb0f..5475008 100644
--- a/tests/test_mosaic.py
+++ b/tests/test_mosaic.py
@@ -91,6 +91,18 @@ def test_mosaic_tiler():
         assets, x, y, z, cogTiler, pixel_selection=defaults.MeanMethod()
     )
     assert m.all()
+    assert t[0][-1][-1] == 7822
+
+    # Test mean pixel selection
+    t, m = mosaic.mosaic_tiler(
+        assets,
+        x,
+        y,
+        z,
+        cogTiler,
+        pixel_selection=defaults.MeanMethod(enforce_data_type=False),
+    )
+    assert m.all()
     assert t[0][-1][-1] == 7822.5
 
     # Test median pixel selection
@@ -98,6 +110,18 @@ def test_mosaic_tiler():
         assets, x, y, z, cogTiler, pixel_selection=defaults.MedianMethod()
     )
     assert m.all()
+    assert t[0][-1][-1] == 7822
+
+    # Test median pixel selection
+    t, m = mosaic.mosaic_tiler(
+        assets,
+        x,
+        y,
+        z,
+        cogTiler,
+        pixel_selection=defaults.MedianMethod(enforce_data_type=False),
+    )
+    assert m.all()
     assert t[0][-1][-1] == 7822.5
 
     # Test invalid Pixel Selection class