codecov-commenter: # [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/89?src=pr&el=h1) Report
> Merging [#89](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/89?src=pr&el=desc) into [master](https://codecov.io/gh/dlr-eoc/ukis-pysat/commit/a8bbb98b586bc2429a85dbf0084c2619ee437838?el=desc) will **increase** coverage by `0.05%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master      #89      +/-   ##
==========================================
+ Coverage   93.46%   93.52%   +0.05%     
==========================================
  Files           8        8              
  Lines         796      803       +7     
==========================================
+ Hits          744      751       +7     
  Misses         52       52              
```

| Flag | Coverage Δ | |
|---|---|---|
| #unittests | `93.52% <100.00%> (+0.05%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/89?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_raster.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/89/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yYXN0ZXIucHk=) | `99.40% <100.00%> (+0.01%)` | :arrow_up: |
| [ukis\_pysat/raster.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/89/diff?src=pr&el=tree#diff-dWtpc19weXNhdC9yYXN0ZXIucHk=) | `93.06% <100.00%> (+0.08%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/89?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/89?src=pr&el=footer). Last update [a8bbb98...e15f3f6](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/89?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index c04d503..e14adb0 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -16,12 +16,16 @@ something was changed
 [master]  (2020-**-**)
 ----------------------
 
+Added
+*****
+- ``raster``: possibility to init with 2D array #88
+
 [0.6.3]  (2020-09-02)
 ----------------------
 
 Fixed
 *****
-- ``file`` imported Pattern of module typing
+- ``file``: imported Pattern of module typing
 
 [0.6.2]  (2020-09-02)
 ----------------------
diff --git a/ukis_pysat/raster.py b/ukis_pysat/raster.py
index ea72b1b..9fcceaf 100644
--- a/ukis_pysat/raster.py
+++ b/ukis_pysat/raster.py
@@ -61,6 +61,10 @@ class Image:
                 self.__arr = data
             else:
                 self.__arr = reshape_as_raster(data)
+
+            if self.__arr.ndim == 2:
+                self.__arr = np.expand_dims(self.__arr, 0)  # always return 3D for consistency
+
             self.dataset = None
             self.__update_dataset(crs, transform, nodata=nodata)
         else:

Create ukis_pysat.raster-Image with 2D array
**Is your feature request related to a problem? Please describe.**

- `ukis_pysat.raster` only treats 3D arrays. Numerous extermal methods work on a 2D basis and using `np.squeeze()` or `np.expand_dims()` adds unnecessary code.

- building an `ukis_pysat.raster.Image()` from a 2D array will fail with a misleading error message:
  > rasterio._err.CPLE_FileIOError: Free disk space available is 68524220416 bytes, whereas 413266544908 are at least necessary.
  
  We could narrow this problem down to the fact, that bringing a 2D array into a 3D method is simply not working.

**Describe the solution you'd like**
Allow initalization of `ukis_pysat.raster.Image` with both 2D or 3D arrays. If I'm not mistaken, rasterio is limited to 3D arrays. @MWieland and @fwfichtner what is your opinion on this?

diff --git a/tests/test_raster.py b/tests/test_raster.py
index 28cb8b1..edf8d1e 100644
--- a/tests/test_raster.py
+++ b/tests/test_raster.py
@@ -23,9 +23,8 @@ class DataTest(unittest.TestCase):
         self.img.close()
 
     def test_init(self):
-        img = Image(self.img.dataset)
-        self.assertTrue(np.array_equal(self.img.arr, img.arr))
-        img.close()
+        with Image(self.img.dataset) as img:
+            self.assertTrue(np.array_equal(self.img.arr, img.arr))
 
     def test_context(self):
         with Image(TEST_FILE) as raster_file:
@@ -52,6 +51,12 @@ class DataTest(unittest.TestCase):
         self.assertTrue(np.array_equal(self.img.arr, img.arr))
         img.close()
 
+    def test_init_2dim(self):
+        with Image(np.ones(shape=(385, 502)), crs=self.img.dataset.crs, transform=self.img.dataset.transform) as img:
+            self.assertEqual(img.arr.ndim, 3)
+            self.assertEqual(len(img.arr), 1)
+            self.assertEqual(img.arr.shape, (1, 385, 502))
+
     def test_init_with_arry_fail_missing_crs(self):
         with self.assertRaises(TypeError):
             Image(data=self.img.arr, crs=self.img.dataset.transform)

pip install -e .[dev]
parse_log_pytest
requirements.txt
black sphinx sphinx_rtd_theme pandas dask[array] utm pytest
apt-get update apt-get install -y gcc
3.8
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
affine==2.4.0
alabaster==0.7.13
appdirs==1.4.4
attrs==25.3.0
babel==2.17.0
black==24.8.0
certifi==2025.1.31
chardet==3.0.4
charset-normalizer==3.4.1
click==7.1.2
click-plugins==1.1.1
cligj==0.7.2
dask==2.14.0
decopatch==1.4.10
docutils==0.19
exceptiongroup==1.2.2
Fiona==1.8.22
geographiclib==2.0
geojson==3.2.0
geomet==1.1.0
geopy==2.4.1
html2text==2024.2.26
idna==2.10
imagesize==1.4.1
importlib_metadata==8.5.0
iniconfig==2.1.0
Jinja2==3.1.6
landsatxplore==0.6
makefun==1.15.6
MarkupSafe==2.1.5
munch==4.0.0
mypy-extensions==1.0.0
numpy==1.18.5
packaging==24.2
pandas==1.0.5
pathspec==0.12.1
Pillow==7.1.2
platformdirs==4.3.6
pluggy==1.5.0
pyfields==1.1.5
Pygments==2.19.1
pylandsat==0.4
pyparsing==3.1.4
pyproj==2.6.1.post1
pytest==8.3.5
python-dateutil==2.8.2
pytz==2025.2
rasterio==1.1.4
requests==2.23.0
rio-mucho==1.0.0
rio-toa==0.3.0
sentinelsat==0.14
Shapely==1.7.1
six==1.17.0
snowballstemmer==2.2.0
snuggs==1.4.7
Sphinx==5.3.0
sphinx-rtd-theme==2.0.0
sphinxcontrib-applehelp==1.0.4
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.1
sphinxcontrib-jquery==4.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
tomli==2.2.1
toolz==1.0.0
tqdm==4.67.1
typing_extensions==4.13.0
-e git+https://github.com/dlr-eoc/ukis-pysat.git@a8bbb98b586bc2429a85dbf0084c2619ee437838#egg=ukis_pysat
urllib3==1.25.11
utm==0.5.0
valid8==5.1.2
zipp==3.20.2

name: ukis-pysat
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=24.2=py38h06a4308_0
  - python=3.8.20=he870216_0
  - readline=8.2=h5eee18b_0
  - setuptools=75.1.0=py38h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.44.0=py38h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - affine==2.4.0
      - alabaster==0.7.13
      - appdirs==1.4.4
      - attrs==25.3.0
      - babel==2.17.0
      - black==24.8.0
      - certifi==2025.1.31
      - chardet==3.0.4
      - charset-normalizer==3.4.1
      - click==7.1.2
      - click-plugins==1.1.1
      - cligj==0.7.2
      - dask==2.14.0
      - decopatch==1.4.10
      - docutils==0.19
      - exceptiongroup==1.2.2
      - fiona==1.8.22
      - geographiclib==2.0
      - geojson==3.2.0
      - geomet==1.1.0
      - geopy==2.4.1
      - html2text==2024.2.26
      - idna==2.10
      - imagesize==1.4.1
      - importlib-metadata==8.5.0
      - iniconfig==2.1.0
      - jinja2==3.1.6
      - landsatxplore==0.6
      - makefun==1.15.6
      - markupsafe==2.1.5
      - munch==4.0.0
      - mypy-extensions==1.0.0
      - numpy==1.18.5
      - packaging==24.2
      - pandas==1.0.5
      - pathspec==0.12.1
      - pillow==7.1.2
      - platformdirs==4.3.6
      - pluggy==1.5.0
      - pyfields==1.1.5
      - pygments==2.19.1
      - pylandsat==0.4
      - pyparsing==3.1.4
      - pyproj==2.6.1.post1
      - pytest==8.3.5
      - python-dateutil==2.8.2
      - pytz==2025.2
      - rasterio==1.1.4
      - requests==2.23.0
      - rio-mucho==1.0.0
      - rio-toa==0.3.0
      - sentinelsat==0.14
      - shapely==1.7.1
      - six==1.17.0
      - snowballstemmer==2.2.0
      - snuggs==1.4.7
      - sphinx==5.3.0
      - sphinx-rtd-theme==2.0.0
      - sphinxcontrib-applehelp==1.0.4
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.1
      - sphinxcontrib-jquery==4.1
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - tomli==2.2.1
      - toolz==1.0.0
      - tqdm==4.67.1
      - typing-extensions==4.13.0
      - urllib3==1.25.11
      - utm==0.5.0
      - valid8==5.1.2
      - zipp==3.20.2
prefix: /opt/conda/envs/ukis-pysat

tests/test_raster.py::DataTest::test_init_2dim
tests/test_raster.py::DataTest::test__lookup_bands tests/test_raster.py::DataTest::test_arr tests/test_raster.py::DataTest::test_arr_nodata tests/test_raster.py::DataTest::test_context tests/test_raster.py::DataTest::test_dimorder_error tests/test_raster.py::DataTest::test_dn2toa tests/test_raster.py::DataTest::test_get_dask_array tests/test_raster.py::DataTest::test_get_subset tests/test_raster.py::DataTest::test_get_tiles tests/test_raster.py::DataTest::test_get_valid_data_bbox tests/test_raster.py::DataTest::test_init tests/test_raster.py::DataTest::test_init_fail_invalid_dataset tests/test_raster.py::DataTest::test_init_fail_invalid_path tests/test_raster.py::DataTest::test_init_with_array tests/test_raster.py::DataTest::test_init_with_arry_fail_missing_crs tests/test_raster.py::DataTest::test_init_with_arry_fail_missing_crs_and_transform tests/test_raster.py::DataTest::test_init_with_arry_fail_missing_transform tests/test_raster.py::DataTest::test_mask_image tests/test_raster.py::DataTest::test_set_array tests/test_raster.py::DataTest::test_set_array_error tests/test_raster.py::DataTest::test_warp tests/test_raster.py::DataTest::test_write_to_file
CHANGELOG.rst ukis_pysat/raster.py
CHANGELOG.rst ukis_pysat/raster.py