Skip to content

Commit

Permalink
Merge pull request #60 from rok4/fix/read-png-1band
Browse files Browse the repository at this point in the history
Correction de la lecture d'une tuile d'une pyramide PNG 1 canal
  • Loading branch information
Dolite authored Sep 8, 2023
2 parents 1559d1a + 2e2a202 commit f01405b
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[bumpversion]
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(b(?P<beta>\d+))?
serialize =
serialize =
{major}.{minor}.{patch}b{beta}
{major}.{minor}.{patch}

Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/build-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
name: Release ${{ github.ref_name }}
generateReleaseNotes: true
draft: false
prerelease: contains(${{ github.ref_name }},'b')
prerelease: ${{ contains(github.ref_name ,'b') }}

build_and_test:

Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
coverage report -m
- name: Build unit tests report
if: "matrix.os == 'ubuntu-20.04' && matrix.python-version == '3.8'"
if: "! contains(github.ref_name,'b') && matrix.os == 'ubuntu-20.04' && matrix.python-version == '3.8'"
run: |
source .venv/bin/activate
coverage html -d dist/tests/
Expand All @@ -93,7 +93,7 @@ jobs:
python3 -m build
- name: Build documentation
if: "matrix.os == 'ubuntu-20.04' && matrix.python-version == '3.8'"
if: "! contains(github.ref_name,'b') && matrix.os == 'ubuntu-20.04' && matrix.python-version == '3.8'"
run: |
source .venv/bin/activate
pip install -e .[doc]
Expand All @@ -112,7 +112,7 @@ jobs:
publish_artefacts:
name: Add built artefacts to release and PyPI
needs: [create_release, build_and_test]
if: "always()&&(needs.create_release.outputs.job_status=='success')&&(needs.build_and_test.outputs.job_status=='success')"
if: "always() && needs.create_release.outputs.job_status == 'success' && needs.build_and_test.outputs.job_status == 'success'"
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -158,7 +158,7 @@ jobs:
commit_documentation:
name: Add documentation and unit tests results into gh-pages branch
needs: build_and_test
if: "!(contains(${{ github.ref_name }},'b'))&&(needs.create_release.outputs.job_status=='success')&&(needs.build_and_test.outputs.job_status=='success')"
if: "always() && ! contains(github.ref_name,'b') && needs.create_release.outputs.job_status == 'success' && needs.build_and_test.outputs.job_status == 'success'"
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -208,7 +208,7 @@ jobs:
delete_version:
name: Remove release and tag if error occured
needs: build_and_test
if: "always()&&(needs.create_release.outputs.job_status=='success')&&(needs.build_and_test.outputs.job_status!='success')"
if: "always() && needs.create_release.outputs.job_status == 'success' && needs.build_and_test.outputs.job_status != 'success'"
runs-on: ubuntu-latest

steps:
Expand Down
10 changes: 10 additions & 0 deletions src/rok4/pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,11 @@ def get_tile_data_raster(self, level: str, column: int, row: int) -> numpy.ndarr
raise FormatError("JPEG", "binary tile", e)

data = numpy.asarray(img)
data.shape = (
level_object.tile_matrix.tile_size[0],
level_object.tile_matrix.tile_size[1],
self.__raster_specifications["channels"],
)

elif self.__format == "TIFF_RAW_UINT8":
data = numpy.frombuffer(binary_tile, dtype=numpy.dtype("uint8"))
Expand All @@ -1205,6 +1210,11 @@ def get_tile_data_raster(self, level: str, column: int, row: int) -> numpy.ndarr
raise FormatError("PNG", "binary tile", e)

data = numpy.asarray(img)
data.shape = (
level_object.tile_matrix.tile_size[0],
level_object.tile_matrix.tile_size[1],
self.__raster_specifications["channels"],
)

elif self.__format == "TIFF_ZIP_UINT8":
try:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from unittest import mock
from unittest.mock import *

import pytest

from rok4.enums import PyramidType
from rok4.exceptions import *
from rok4.layer import Layer
Expand Down
19 changes: 9 additions & 10 deletions tests/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
from rok4.enums import SlabType, StorageType
from rok4.exceptions import *
from rok4.pyramid import *
from rok4.tile_matrix_set import TileMatrixSet
from rok4.utils import *


@mock.patch("rok4.pyramid.get_data_str", side_effect=StorageError("FILE", "Not found"))
def test_wrong_file(mocked_get_data_str):
with pytest.raises(StorageError):
pyramid = Pyramid.from_descriptor("file:///pyramid.json")
Pyramid.from_descriptor("file:///pyramid.json")


@mock.patch(
Expand All @@ -23,7 +22,7 @@ def test_wrong_file(mocked_get_data_str):
)
def test_bad_json(mocked_get_data_str):
with pytest.raises(FormatError) as exc:
pyramid = Pyramid.from_descriptor("file:///pyramid.json")
Pyramid.from_descriptor("file:///pyramid.json")

assert (
str(exc.value)
Expand All @@ -35,7 +34,7 @@ def test_bad_json(mocked_get_data_str):
@mock.patch("rok4.pyramid.get_data_str", return_value='{"format": "TIFF_PBF_MVT","levels":[]}')
def test_missing_tms(mocked_get_data_str):
with pytest.raises(MissingAttributeError) as exc:
pyramid = Pyramid.from_descriptor("file:///pyramid.json")
Pyramid.from_descriptor("file:///pyramid.json")

assert str(exc.value) == "Missing attribute 'tile_matrix_set' in 'file:///pyramid.json'"
mocked_get_data_str.assert_called_once_with("file:///pyramid.json")
Expand All @@ -49,7 +48,7 @@ def test_missing_tms(mocked_get_data_str):
@mock.patch("rok4.pyramid.TileMatrixSet", side_effect=StorageError("FILE", "TMS not found"))
def test_wrong_tms(mocked_tms_constructor, mocked_get_data_str):
with pytest.raises(StorageError) as exc:
pyramid = Pyramid.from_descriptor("file:///pyramid.json")
Pyramid.from_descriptor("file:///pyramid.json")

assert str(exc.value) == "Issue occured using a FILE storage : TMS not found"
mocked_tms_constructor.assert_called_once_with("PM")
Expand All @@ -64,7 +63,7 @@ def test_wrong_tms(mocked_tms_constructor, mocked_get_data_str):
@mock.patch("rok4.pyramid.TileMatrixSet")
def test_raster_missing_raster_specifications(mocked_tms_class, mocked_get_data_str):
with pytest.raises(MissingAttributeError) as exc:
pyramid = Pyramid.from_descriptor("file:///pyramid.json")
Pyramid.from_descriptor("file:///pyramid.json")

assert str(exc.value) == "Missing attribute 'raster_specifications' in 'file:///pyramid.json'"
mocked_get_data_str.assert_called_once_with("file:///pyramid.json")
Expand All @@ -83,7 +82,7 @@ def test_wrong_level(mocked_tms_class, mocked_get_data_str):
mocked_tms_class.return_value = tms_instance

with pytest.raises(Exception) as exc:
pyramid = Pyramid.from_descriptor("file:///pyramid.json")
Pyramid.from_descriptor("file:///pyramid.json")

mocked_tms_class.assert_called_once_with("PM")
mocked_get_data_str.assert_called_once_with("file:///pyramid.json")
Expand All @@ -102,7 +101,7 @@ def test_wrong_level(mocked_tms_class, mocked_get_data_str):
@mock.patch("rok4.pyramid.TileMatrixSet", autospec=True)
def test_vector_missing_tables(mocked_tms_class, mocked_get_data_str):
with pytest.raises(MissingAttributeError) as exc:
pyramid = Pyramid.from_descriptor("file:///pyramid.json")
Pyramid.from_descriptor("file:///pyramid.json")

assert str(exc.value) == "Missing attribute levels[].'tables' in 'file:///pyramid.json'"
mocked_get_data_str.assert_called_once_with("file:///pyramid.json")
Expand All @@ -119,7 +118,7 @@ def test_raster_ok(mocked_put_data_str, mocked_tms_class, mocked_get_data_str):
tms_instance = MagicMock()
tms_instance.name = "PM"
tms_instance.srs = "EPSG:3857"
tms_instance.sr = sr_src = srs_to_spatialreference("EPSG:3857")
tms_instance.sr = srs_to_spatialreference("EPSG:3857")

tm_instance = MagicMock()
tm_instance.id = "0"
Expand Down Expand Up @@ -191,7 +190,7 @@ def test_vector_ok(mocked_tms_class, mocked_get_data_str):
except Exception as exc:
assert False, f"Pyramid creation raises an exception: {exc}"

with pytest.raises(Exception) as exc:
with pytest.raises(Exception):
pyramid.get_tile_data_raster("12", 5, 6)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import math
import random
from unittest import TestCase, mock
from unittest.mock import MagicMock, Mock, call, mock_open, patch
from unittest.mock import MagicMock, call, mock_open

import pytest

Expand Down
23 changes: 11 additions & 12 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import botocore.exceptions
import pytest
from rados import ObjectNotFound

from rok4.enums import StorageType
from rok4.exceptions import *
Expand Down Expand Up @@ -55,13 +54,13 @@ def test_get_path_from_infos():
@mock.patch.dict(os.environ, {}, clear=True)
def test_s3_missing_env():
with pytest.raises(MissingEnvironmentError):
data = get_data_str("s3://bucket/path/to/object")
get_data_str("s3://bucket/path/to/object")


@mock.patch.dict(os.environ, {}, clear=True)
def test_ceph_missing_env():
with pytest.raises(MissingEnvironmentError):
data = get_data_str("ceph://bucket/path/to/object")
get_data_str("ceph://bucket/path/to/object")


@mock.patch.dict(
Expand All @@ -71,25 +70,25 @@ def test_ceph_missing_env():
)
def test_s3_invalid_envs():
with pytest.raises(StorageError):
data = get_data_str("s3://bucket/path/to/object")
get_data_str("s3://bucket/path/to/object")


@mock.patch.dict(
os.environ, {"ROK4_S3_URL": "a", "ROK4_S3_SECRETKEY": "b", "ROK4_S3_KEY": "c"}, clear=True
)
@mock.patch("rok4.storage.boto3.client")
def test_s3_invalid_endpoint(mocked_s3_client):
s3_instance = MagicMock()
MagicMock()
mocked_s3_client.side_effect = Exception("Invalid URL")
with pytest.raises(StorageError):
data = get_data_str("s3://bucket/path/to/object")
get_data_str("s3://bucket/path/to/object")


@mock.patch.dict(os.environ, {}, clear=True)
@mock.patch("builtins.open", side_effect=FileNotFoundError("not_found"))
def test_file_read_error(mock_file):
with pytest.raises(FileNotFoundError):
data = get_data_str("file:///path/to/file.ext")
get_data_str("file:///path/to/file.ext")

mock_file.assert_called_with("/path/to/file.ext", "rb")

Expand Down Expand Up @@ -117,7 +116,7 @@ def test_s3_read_nok(mocked_s3_client):
s3_instance.get_object.side_effect = Exception("Bucket or object not found")
mocked_s3_client.return_value = s3_instance
with pytest.raises(StorageError):
data = get_data_str("s3://bucket/path/to/object")
get_data_str("s3://bucket/path/to/object")


@mock.patch.dict(
Expand Down Expand Up @@ -171,15 +170,15 @@ def test_http_read_error(mock_http):
requests_instance.content = "NULL"
requests_instance.status_code = 404
mock_http.return_value = requests_instance
data = get_data_str("http://path/to/file.ext")
get_data_str("http://path/to/file.ext")

mock_http.assert_called_with("http://path/to/file.ext", stream=True)


@mock.patch.dict(os.environ, {}, clear=True)
def test_http_read_range_error():
with pytest.raises(NotImplementedError):
data = get_data_binary("http://path/to/file.ext", (0, 100))
get_data_binary("http://path/to/file.ext", (0, 100))


@mock.patch.dict(os.environ, {}, clear=True)
Expand Down Expand Up @@ -925,7 +924,7 @@ def test_size_path_file_ok():

def test_size_file_nok():
with pytest.raises(StorageError):
size = size_path("file://tests/fixtures/TIFF_PBF_M")
size_path("file://tests/fixtures/TIFF_PBF_M")


@mock.patch.dict(
Expand All @@ -935,7 +934,7 @@ def test_size_file_nok():
)
def test_size_path_ceph_nok():
with pytest.raises(NotImplementedError):
size = size_path("ceph://pool/path")
size_path("ceph://pool/path")


@mock.patch.dict(
Expand Down
24 changes: 12 additions & 12 deletions tests/test_tile_matrix_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
@mock.patch.dict(os.environ, {}, clear=True)
def test_missing_env():
with pytest.raises(MissingEnvironmentError):
tms = TileMatrixSet("tms")
TileMatrixSet("tms")


@mock.patch.dict(os.environ, {"ROK4_TMS_DIRECTORY": "file:///path/to"}, clear=True)
@mock.patch("rok4.tile_matrix_set.get_data_str", side_effect=StorageError("FILE", "Not found"))
def test_wrong_file(mocked_get_data_str):
with pytest.raises(StorageError):
tms = TileMatrixSet("tms")
TileMatrixSet("tms")


@mock.patch.dict(os.environ, {"ROK4_TMS_DIRECTORY": "file:///path/to"}, clear=True)
Expand All @@ -27,8 +27,8 @@ def test_wrong_file(mocked_get_data_str):
return_value='"crs":"EPSG:3857","orderedAxes":["X","Y"],"id":"PM"}',
)
def test_bad_json(mocked_get_data_str):
with pytest.raises(FormatError) as exc:
tms = TileMatrixSet("tms")
with pytest.raises(FormatError):
TileMatrixSet("tms")
mocked_get_data_str.assert_called_once_with("file:///path/to/tms.json")


Expand All @@ -39,7 +39,7 @@ def test_bad_json(mocked_get_data_str):
)
def test_missing_id(mocked_get_data_str):
with pytest.raises(MissingAttributeError) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")
assert str(exc.value) == "Missing attribute 'id' in 'file:///path/to/tms.json'"
mocked_get_data_str.assert_called_once_with("file:///path/to/tms.json")

Expand All @@ -51,7 +51,7 @@ def test_missing_id(mocked_get_data_str):
)
def test_missing_crs(mocked_get_data_str):
with pytest.raises(MissingAttributeError) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")
assert str(exc.value) == "Missing attribute 'crs' in 'file:///path/to/tms.json'"
mocked_get_data_str.assert_called_once_with("file:///path/to/tms.json")

Expand All @@ -63,7 +63,7 @@ def test_missing_crs(mocked_get_data_str):
)
def test_wrong_crs(mocked_get_data_str):
with pytest.raises(Exception) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")
assert (
str(exc.value)
== "Wrong attribute 'crs' ('epsg:123456') in 'file:///path/to/tms.json', not recognize by OSR. Trace : PROJ: proj_create_from_database: crs not found"
Expand All @@ -78,7 +78,7 @@ def test_wrong_crs(mocked_get_data_str):
)
def test_wrong_axes_order(mocked_get_data_str):
with pytest.raises(Exception) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")
assert (
str(exc.value)
== "TMS 'file:///path/to/tms.json' own invalid axes order : only X/Y or Lon/Lat are handled"
Expand All @@ -93,7 +93,7 @@ def test_wrong_axes_order(mocked_get_data_str):
)
def test_missing_levels(mocked_get_data_str):
with pytest.raises(MissingAttributeError) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")
assert str(exc.value) == "Missing attribute 'tileMatrices' in 'file:///path/to/tms.json'"
mocked_get_data_str.assert_called_once_with("file:///path/to/tms.json")

Expand All @@ -105,7 +105,7 @@ def test_missing_levels(mocked_get_data_str):
)
def test_no_levels(mocked_get_data_str):
with pytest.raises(Exception) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")
assert str(exc.value) == "TMS 'file:///path/to/tms.json' has no level"
mocked_get_data_str.assert_called_once_with("file:///path/to/tms.json")

Expand All @@ -117,7 +117,7 @@ def test_no_levels(mocked_get_data_str):
)
def test_wrong_level(mocked_get_data_str):
with pytest.raises(MissingAttributeError) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")
assert str(exc.value) == "Missing attribute tileMatrices[].'id' in 'file:///path/to/tms.json'"
mocked_get_data_str.assert_called_once_with("file:///path/to/tms.json")

Expand All @@ -129,7 +129,7 @@ def test_wrong_level(mocked_get_data_str):
)
def test_wrong_level_id(mocked_get_data_str):
with pytest.raises(Exception) as exc:
tms = TileMatrixSet("tms")
TileMatrixSet("tms")

assert (
str(exc.value)
Expand Down
Loading

0 comments on commit f01405b

Please sign in to comment.