Repository: dlr-eoc/ukis-pysat

Issue:
Array dimorder not considered anymore when creating Image from array
When creating an Image instance from an array, the array dimorder is not considered anymore. 

Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51?src=pr&el=h1) Report
> Merging [#51](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51?src=pr&el=desc) into [master](https://codecov.io/gh/dlr-eoc/ukis-pysat/commit/39b43d24edb8be6f3eb9738b9f4f8d8bdd6cd8e8&el=desc) will **decrease** coverage by `0.11%`.
> The diff coverage is `95.45%`.

[![Impacted file tree graph](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51/graphs/tree.svg?width=650&height=150&src=pr&token=6LYPGRGMXV)](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #51      +/-   ##
==========================================
- Coverage   93.65%   93.54%   -0.12%     
==========================================
  Files           8        8              
  Lines         725      728       +3     
==========================================
+ Hits          679      681       +2     
- Misses         46       47       +1     
```

| Flag | Coverage Δ | |
|---|---|---|
| #unittests | `93.54% <95.45%> (-0.12%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [ukis\_pysat/raster.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51/diff?src=pr&el=tree#diff-dWtpc19weXNhdC9yYXN0ZXIucHk=) | `92.63% <95.23%> (-0.49%)` | :arrow_down: |
| [tests/test\_raster.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yYXN0ZXIucHk=) | `99.21% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51?src=pr&el=footer). Last update [39b43d2...4a97077](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/51?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index c3f20aa..d15c395 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -27,7 +27,8 @@ Fixed
 
 Changed
 *******
-- ``raster.Image()``: update of init signature to be less confusing #41
+- ``raster.Image()``: update of init signature to be less confusing #41 #50
+
 
 [0.4.0]  (2020-06-05)
 ----------------------
diff --git a/ukis_pysat/raster.py b/ukis_pysat/raster.py
index 59483a0..e0ac79c 100644
--- a/ukis_pysat/raster.py
+++ b/ukis_pysat/raster.py
@@ -40,40 +40,43 @@ class Image:
             self.dimorder = dimorder
         else:
             raise TypeError("dimorder for bands or channels must be either 'first' or 'last'.")
-        try:
+
+        if isinstance(data, rasterio.io.DatasetReader):
+            self.dataset = data
+            self.crs = self.dataset.crs
+            self.transform = self.dataset.transform
+            self.__arr = self.dataset.read()
+
+        elif isinstance(data, str):
             self.dataset = rasterio.open(data)
             self.crs = self.dataset.crs
             self.transform = self.dataset.transform
             self.__arr = self.dataset.read()
-        except (TypeError, AttributeError):
-            if isinstance(data, rasterio.io.DatasetReader):
-                self.dataset = data
-                self.crs = self.dataset.crs
-                self.transform = self.dataset.transform
-                self.__arr = self.dataset.read()
-            elif isinstance(data, np.ndarray):
-                if crs is None:
-                    raise TypeError("if dataset is of type np.ndarray crs must not be None")
-                if transform is None:
-                    raise TypeError("if dataset is of type np.ndarray transform must not be None")
-                meta = {"dtype": data.dtype, "count": data.shape[0], "crs": crs, "driver": "GTiff"}
+
+        elif isinstance(data, np.ndarray):
+            if crs is None:
+                raise TypeError("if dataset is of type np.ndarray crs must not be None")
+            if transform is None:
+                raise TypeError("if dataset is of type np.ndarray transform must not be None")
+            if dimorder == "first":
                 self.__arr = data
-                self.transform = transform
-                self.dataset = self.__update_dataset(meta).open()
-                self.crs = self.dataset.crs
-                self.transform = self.dataset.transform
             else:
-                raise TypeError("dataset must be of type str, rasterio.io.DatasetReader or np.ndarray")
+                self.__arr = reshape_as_raster(data)
+            meta = {"dtype": self.__arr.dtype, "count": self.__arr.shape[0], "crs": crs, "driver": "GTiff"}
+            self.transform = transform
+            self.dataset = self.__update_dataset(meta).open()
+            self.crs = self.dataset.crs
+            self.transform = self.dataset.transform
+        else:
+            raise TypeError("dataset must be of type rasterio.io.DatasetReader, str or np.ndarray")
 
     @property
     def arr(self):
         """array property"""
         if self.dimorder == "first":
             return self.__arr
-        elif self.dimorder == "last":
-            return reshape_as_image(self.__arr)
         else:
-            raise AttributeError("dimorder for bands or channels must be either 'first' or 'last'.")
+            return reshape_as_image(self.__arr)
 
     def get_valid_data_bbox(self, nodata=0):
         """bounding box covering the input array's valid data pixels.
@@ -90,7 +93,7 @@ class Image:
         :param bbox: bounding box of type tuple or Shapely Polygon
         :param crop: bool, see rasterio.mask. Optional, (default: True)
         :param pad: pads image, should only be used when bbox.bounds extent img.bounds, optional (default: False)
-        :param mode: str, how to pad, see rasterio.pad. Optional (default: 'constant') 
+        :param mode: str, how to pad, see rasterio.pad. Optional (default: 'constant')
         :param constant_values: nodata value, padding should be filled with, optional (default: 0)
         """
         # TODO https://github.com/mapbox/rasterio/issues/995


Test patch:
diff --git a/tests/test_raster.py b/tests/test_raster.py
index ea32ae0..b09158a 100644
--- a/tests/test_raster.py
+++ b/tests/test_raster.py
@@ -96,7 +96,7 @@ class DataTest(unittest.TestCase):
         self.assertEqual(img_first.arr.shape, (1, 385, 502))
         img_first.close()
 
-        img_last = Image(np.ones((1, 385, 502)), dimorder="last", crs=self.img.crs, transform=self.img.transform)
+        img_last = Image(np.ones((385, 502, 1)), dimorder="last", crs=self.img.crs, transform=self.img.transform)
         self.assertEqual(img_last.arr.shape, (385, 502, 1))
         img_last.close()