[test] add raster helpers test cases
Browse files
samgis/io/raster_helpers.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
"""helpers for computer vision duties"""
|
| 2 |
import numpy as np
|
| 3 |
-
from numpy import ndarray
|
| 4 |
|
| 5 |
from samgis import app_logger
|
| 6 |
-
from samgis.utilities.type_hints import
|
| 7 |
|
| 8 |
|
| 9 |
def get_nextzen_terrain_rgb_formula(red: ndarray, green: ndarray, blue: ndarray) -> ndarray:
|
|
@@ -34,8 +34,8 @@ def get_mapbox__terrain_rgb_formula(red: ndarray, green: ndarray, blue: ndarray)
|
|
| 34 |
|
| 35 |
|
| 36 |
providers_terrain_rgb_formulas = {
|
| 37 |
-
|
| 38 |
-
|
| 39 |
}
|
| 40 |
|
| 41 |
|
|
@@ -84,6 +84,7 @@ def get_rgb_prediction_image(raster_cropped: ndarray, slope_cellsize: int, inver
|
|
| 84 |
|
| 85 |
try:
|
| 86 |
slope, curvature = get_slope_curvature(raster_cropped, slope_cellsize=slope_cellsize)
|
|
|
|
| 87 |
channel0 = raster_cropped
|
| 88 |
channel1 = normalize_array_list(
|
| 89 |
[raster_cropped, slope, curvature], CHANNEL_EXAGGERATIONS_LIST, title=f"channel1_normlist")
|
|
@@ -126,7 +127,8 @@ def get_rgb_image(arr_channel0: ndarray, arr_channel1: ndarray, arr_channel2: nd
|
|
| 126 |
data_rgb[:, :, 2] = normalize_array(
|
| 127 |
arr_channel2.astype(float), high=1, norm_type="float", title=f"RGB:channel2") * 192
|
| 128 |
if invert_image:
|
| 129 |
-
|
|
|
|
| 130 |
return data_rgb
|
| 131 |
except ValueError as ve_get_rgb_image:
|
| 132 |
msg = f"ve_get_rgb_image:{ve_get_rgb_image}."
|
|
@@ -208,6 +210,7 @@ def normalize_array(arr: ndarray, high: int = 255, norm_type: str = "float", inv
|
|
| 208 |
ndarray: normalized numpy array
|
| 209 |
|
| 210 |
"""
|
|
|
|
| 211 |
|
| 212 |
h_min_arr = np.nanmin(arr)
|
| 213 |
h_arr_max = np.nanmax(arr)
|
|
@@ -217,7 +220,7 @@ def normalize_array(arr: ndarray, high: int = 255, norm_type: str = "float", inv
|
|
| 217 |
f"normalize_array:: '{title}',h_min_arr:{h_min_arr},h_arr_max:{h_arr_max},h_diff:{h_diff}, dtype:{arr.dtype}.")
|
| 218 |
except Exception as e_h_diff:
|
| 219 |
app_logger.error(f"e_h_diff:{e_h_diff}.")
|
| 220 |
-
raise e_h_diff
|
| 221 |
|
| 222 |
if check_empty_array(arr, high) or check_empty_array(arr, h_diff):
|
| 223 |
msg_ve = f"normalize_array::empty array '{title}',h_min_arr:{h_min_arr},h_arr_max:{h_arr_max},h_diff:{h_diff}, dtype:{arr.dtype}."
|
|
|
|
| 1 |
"""helpers for computer vision duties"""
|
| 2 |
import numpy as np
|
| 3 |
+
from numpy import ndarray, bitwise_not
|
| 4 |
|
| 5 |
from samgis import app_logger
|
| 6 |
+
from samgis.utilities.type_hints import XYZTerrainProvidersNames
|
| 7 |
|
| 8 |
|
| 9 |
def get_nextzen_terrain_rgb_formula(red: ndarray, green: ndarray, blue: ndarray) -> ndarray:
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
providers_terrain_rgb_formulas = {
|
| 37 |
+
XYZTerrainProvidersNames.MAPBOX_TERRAIN_TILES_NAME: get_mapbox__terrain_rgb_formula,
|
| 38 |
+
XYZTerrainProvidersNames.NEXTZEN_TERRAIN_TILES_NAME: get_nextzen_terrain_rgb_formula
|
| 39 |
}
|
| 40 |
|
| 41 |
|
|
|
|
| 84 |
|
| 85 |
try:
|
| 86 |
slope, curvature = get_slope_curvature(raster_cropped, slope_cellsize=slope_cellsize)
|
| 87 |
+
|
| 88 |
channel0 = raster_cropped
|
| 89 |
channel1 = normalize_array_list(
|
| 90 |
[raster_cropped, slope, curvature], CHANNEL_EXAGGERATIONS_LIST, title=f"channel1_normlist")
|
|
|
|
| 127 |
data_rgb[:, :, 2] = normalize_array(
|
| 128 |
arr_channel2.astype(float), high=1, norm_type="float", title=f"RGB:channel2") * 192
|
| 129 |
if invert_image:
|
| 130 |
+
app_logger.debug(f"data_rgb:{type(data_rgb)}, {data_rgb.dtype}.")
|
| 131 |
+
data_rgb = bitwise_not(data_rgb)
|
| 132 |
return data_rgb
|
| 133 |
except ValueError as ve_get_rgb_image:
|
| 134 |
msg = f"ve_get_rgb_image:{ve_get_rgb_image}."
|
|
|
|
| 210 |
ndarray: normalized numpy array
|
| 211 |
|
| 212 |
"""
|
| 213 |
+
np.seterr("raise")
|
| 214 |
|
| 215 |
h_min_arr = np.nanmin(arr)
|
| 216 |
h_arr_max = np.nanmax(arr)
|
|
|
|
| 220 |
f"normalize_array:: '{title}',h_min_arr:{h_min_arr},h_arr_max:{h_arr_max},h_diff:{h_diff}, dtype:{arr.dtype}.")
|
| 221 |
except Exception as e_h_diff:
|
| 222 |
app_logger.error(f"e_h_diff:{e_h_diff}.")
|
| 223 |
+
raise ValueError(e_h_diff)
|
| 224 |
|
| 225 |
if check_empty_array(arr, high) or check_empty_array(arr, h_diff):
|
| 226 |
msg_ve = f"normalize_array::empty array '{title}',h_min_arr:{h_min_arr},h_arr_max:{h_arr_max},h_diff:{h_diff}, dtype:{arr.dtype}."
|
samgis/io/wrappers_helpers.py
CHANGED
|
@@ -5,7 +5,7 @@ from xyzservices import providers, TileProvider
|
|
| 5 |
from samgis import app_logger
|
| 6 |
from samgis.io.coordinates_pixel_conversion import get_latlng_to_pixel_coordinates
|
| 7 |
from samgis.utilities.constants import COMPLETE_URL_TILES_MAPBOX, COMPLETE_URL_TILES_NEXTZEN, CUSTOM_RESPONSE_MESSAGES
|
| 8 |
-
from samgis.utilities.type_hints import ApiRequestBody, ContentTypes,
|
| 9 |
from samgis.utilities.utilities import base64_decode
|
| 10 |
|
| 11 |
|
|
@@ -153,12 +153,12 @@ def get_parsed_request_body(event: Dict or str) -> ApiRequestBody:
|
|
| 153 |
|
| 154 |
|
| 155 |
mapbox_terrain_rgb = TileProvider(
|
| 156 |
-
name=
|
| 157 |
url=COMPLETE_URL_TILES_MAPBOX,
|
| 158 |
attribution=""
|
| 159 |
)
|
| 160 |
nextzen_terrain_rgb = TileProvider(
|
| 161 |
-
name=
|
| 162 |
url=COMPLETE_URL_TILES_NEXTZEN,
|
| 163 |
attribution=""
|
| 164 |
)
|
|
@@ -167,11 +167,11 @@ nextzen_terrain_rgb = TileProvider(
|
|
| 167 |
def get_url_tile(source_type: str):
|
| 168 |
try:
|
| 169 |
match source_type.lower():
|
| 170 |
-
case
|
| 171 |
-
return providers.query_name(
|
| 172 |
-
case
|
| 173 |
return mapbox_terrain_rgb
|
| 174 |
-
case
|
| 175 |
app_logger.info("nextzen_terrain_rgb:", nextzen_terrain_rgb)
|
| 176 |
return nextzen_terrain_rgb
|
| 177 |
|
|
@@ -184,4 +184,4 @@ def get_url_tile(source_type: str):
|
|
| 184 |
|
| 185 |
|
| 186 |
def check_source_type_is_terrain(source: str | TileProvider):
|
| 187 |
-
return isinstance(source, TileProvider) and source.name in list(
|
|
|
|
| 5 |
from samgis import app_logger
|
| 6 |
from samgis.io.coordinates_pixel_conversion import get_latlng_to_pixel_coordinates
|
| 7 |
from samgis.utilities.constants import COMPLETE_URL_TILES_MAPBOX, COMPLETE_URL_TILES_NEXTZEN, CUSTOM_RESPONSE_MESSAGES
|
| 8 |
+
from samgis.utilities.type_hints import ApiRequestBody, ContentTypes, XYZTerrainProvidersNames, XYZDefaultProvidersNames
|
| 9 |
from samgis.utilities.utilities import base64_decode
|
| 10 |
|
| 11 |
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
mapbox_terrain_rgb = TileProvider(
|
| 156 |
+
name=XYZTerrainProvidersNames.MAPBOX_TERRAIN_TILES_NAME,
|
| 157 |
url=COMPLETE_URL_TILES_MAPBOX,
|
| 158 |
attribution=""
|
| 159 |
)
|
| 160 |
nextzen_terrain_rgb = TileProvider(
|
| 161 |
+
name=XYZTerrainProvidersNames.NEXTZEN_TERRAIN_TILES_NAME,
|
| 162 |
url=COMPLETE_URL_TILES_NEXTZEN,
|
| 163 |
attribution=""
|
| 164 |
)
|
|
|
|
| 167 |
def get_url_tile(source_type: str):
|
| 168 |
try:
|
| 169 |
match source_type.lower():
|
| 170 |
+
case XYZDefaultProvidersNames.DEFAULT_TILES_NAME_SHORT:
|
| 171 |
+
return providers.query_name(XYZDefaultProvidersNames.DEFAULT_TILES_NAME)
|
| 172 |
+
case XYZTerrainProvidersNames.MAPBOX_TERRAIN_TILES_NAME:
|
| 173 |
return mapbox_terrain_rgb
|
| 174 |
+
case XYZTerrainProvidersNames.NEXTZEN_TERRAIN_TILES_NAME:
|
| 175 |
app_logger.info("nextzen_terrain_rgb:", nextzen_terrain_rgb)
|
| 176 |
return nextzen_terrain_rgb
|
| 177 |
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
def check_source_type_is_terrain(source: str | TileProvider):
|
| 187 |
+
return isinstance(source, TileProvider) and source.name in list(XYZTerrainProvidersNames)
|
samgis/prediction_api/predictors.py
CHANGED
|
@@ -7,10 +7,9 @@ from samgis.io.raster_helpers import get_raster_terrain_rgb_like, get_rgb_predic
|
|
| 7 |
from samgis.io.tms2geotiff import download_extent
|
| 8 |
from samgis.io.wrappers_helpers import check_source_type_is_terrain
|
| 9 |
from samgis.prediction_api.sam_onnx import SegmentAnythingONNX
|
| 10 |
-
from samgis.utilities.constants import
|
| 11 |
-
DEFAULT_INPUT_SHAPE
|
| 12 |
-
from samgis.utilities.type_hints import llist_float, dict_str_int, list_dict, tuple_ndarr_int, PIL_Image
|
| 13 |
-
TmsTerrainProvidersNames
|
| 14 |
|
| 15 |
models_dict = {"fastsam": {"instance": None}}
|
| 16 |
|
|
|
|
| 7 |
from samgis.io.tms2geotiff import download_extent
|
| 8 |
from samgis.io.wrappers_helpers import check_source_type_is_terrain
|
| 9 |
from samgis.prediction_api.sam_onnx import SegmentAnythingONNX
|
| 10 |
+
from samgis.utilities.constants import (
|
| 11 |
+
MODEL_ENCODER_NAME, MODEL_DECODER_NAME, DEFAULT_URL_TILES, SLOPE_CELLSIZE, DEFAULT_INPUT_SHAPE)
|
| 12 |
+
from samgis.utilities.type_hints import llist_float, dict_str_int, list_dict, tuple_ndarr_int, PIL_Image
|
|
|
|
| 13 |
|
| 14 |
models_dict = {"fastsam": {"instance": None}}
|
| 15 |
|
samgis/utilities/type_hints.py
CHANGED
|
@@ -23,13 +23,13 @@ PIL_Image = Image
|
|
| 23 |
tuple_ndarray_transform = tuple[ndarray, Affine]
|
| 24 |
|
| 25 |
|
| 26 |
-
class
|
| 27 |
"""Default xyz provider names"""
|
| 28 |
DEFAULT_TILES_NAME_SHORT = "openstreetmap"
|
| 29 |
DEFAULT_TILES_NAME = "openstreetmap.mapnik"
|
| 30 |
|
| 31 |
|
| 32 |
-
class
|
| 33 |
"""Custom xyz provider names for digital elevation models"""
|
| 34 |
MAPBOX_TERRAIN_TILES_NAME = "mapbox.terrain-rgb"
|
| 35 |
NEXTZEN_TERRAIN_TILES_NAME = "nextzen.terrarium"
|
|
|
|
| 23 |
tuple_ndarray_transform = tuple[ndarray, Affine]
|
| 24 |
|
| 25 |
|
| 26 |
+
class XYZDefaultProvidersNames(StrEnum):
|
| 27 |
"""Default xyz provider names"""
|
| 28 |
DEFAULT_TILES_NAME_SHORT = "openstreetmap"
|
| 29 |
DEFAULT_TILES_NAME = "openstreetmap.mapnik"
|
| 30 |
|
| 31 |
|
| 32 |
+
class XYZTerrainProvidersNames(StrEnum):
|
| 33 |
"""Custom xyz provider names for digital elevation models"""
|
| 34 |
MAPBOX_TERRAIN_TILES_NAME = "mapbox.terrain-rgb"
|
| 35 |
NEXTZEN_TERRAIN_TILES_NAME = "nextzen.terrarium"
|
tests/io/test_raster_helpers.py
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import unittest
|
| 3 |
+
from unittest.mock import patch
|
| 4 |
+
|
| 5 |
+
from samgis.io import raster_helpers
|
| 6 |
+
from samgis.utilities.utilities import hash_calculate
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_three_channels(size=5, param1=1000, param2=3, param3=-88):
|
| 10 |
+
arr_base = np.arange(size*size).reshape(size, size) / size**2
|
| 11 |
+
channel_0 = arr_base * param1
|
| 12 |
+
channel_1 = arr_base * param2
|
| 13 |
+
channel_2 = arr_base * param3
|
| 14 |
+
return channel_0, channel_1, channel_2
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def helper_bell(size=10, param1=0.1, param2=2):
|
| 18 |
+
x = np.linspace(-size, size, num=size**2)
|
| 19 |
+
y = np.linspace(-size, size, num=size**2)
|
| 20 |
+
x, y = np.meshgrid(x, y)
|
| 21 |
+
return np.exp(-param1 * x ** param2 - param1 * y ** param2)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
arr_5x5x5 = np.arange(125).reshape((5, 5, 5)) / 25
|
| 25 |
+
arr = np.arange(25).resize((5, 5))
|
| 26 |
+
channel0, channel1, channel2 = get_three_channels()
|
| 27 |
+
z = helper_bell()
|
| 28 |
+
slope_z_cellsize3, curvature_z_cellsize3 = raster_helpers.get_slope_curvature(z, slope_cellsize=3)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class Test(unittest.TestCase):
|
| 32 |
+
|
| 33 |
+
def test_get_rgb_prediction_image_real(self):
|
| 34 |
+
output = raster_helpers.get_rgb_prediction_image(z, slope_cellsize=61, invert_image=True)
|
| 35 |
+
hash_output = hash_calculate(output)
|
| 36 |
+
assert hash_output == b'QpQ9yxgCLw9cf3klNFKNFXIDHaSkuiZxkbpeQApR8pA='
|
| 37 |
+
output = raster_helpers.get_rgb_prediction_image(z, slope_cellsize=61, invert_image=False)
|
| 38 |
+
hash_output = hash_calculate(output)
|
| 39 |
+
assert hash_output == b'Y+iXO9w/sKzNVOw2rBh2JrVGJUFRqaa8/0F9hpevmLs='
|
| 40 |
+
|
| 41 |
+
@patch.object(raster_helpers, "get_slope_curvature")
|
| 42 |
+
@patch.object(raster_helpers, "normalize_array_list")
|
| 43 |
+
@patch.object(raster_helpers, "get_rgb_image")
|
| 44 |
+
def test_get_rgb_prediction_image_mocked(self, get_rgb_image_mocked, normalize_array_list, get_slope_curvature):
|
| 45 |
+
local_arr = np.array(z * 100, dtype=np.uint8)
|
| 46 |
+
|
| 47 |
+
get_slope_curvature.return_value = slope_z_cellsize3, curvature_z_cellsize3
|
| 48 |
+
normalize_array_list.side_effect = None
|
| 49 |
+
get_rgb_image_mocked.return_value = np.bitwise_not(local_arr)
|
| 50 |
+
output = raster_helpers.get_rgb_prediction_image(local_arr, slope_cellsize=61, invert_image=True)
|
| 51 |
+
hash_output = hash_calculate(output)
|
| 52 |
+
assert hash_output == b'BPIyVH64RgVunj42EuQAx4/v59Va8ZAjcMnuiGNqTT0='
|
| 53 |
+
get_rgb_image_mocked.return_value = local_arr
|
| 54 |
+
output = raster_helpers.get_rgb_prediction_image(local_arr, slope_cellsize=61, invert_image=False)
|
| 55 |
+
hash_output = hash_calculate(output)
|
| 56 |
+
assert hash_output == b'XX54sdLQQUrhkUHT6ikQZYSloMYDSfh/AGITDq6jnRM='
|
| 57 |
+
|
| 58 |
+
@patch.object(raster_helpers, "get_slope_curvature")
|
| 59 |
+
def test_get_rgb_prediction_image_value_error(self, get_slope_curvature):
|
| 60 |
+
msg = "this is a value error"
|
| 61 |
+
get_slope_curvature.side_effect = ValueError(msg)
|
| 62 |
+
|
| 63 |
+
with self.assertRaises(ValueError):
|
| 64 |
+
try:
|
| 65 |
+
raster_helpers.get_rgb_prediction_image(arr, slope_cellsize=3)
|
| 66 |
+
except ValueError as ve:
|
| 67 |
+
self.assertEqual(str(ve), msg)
|
| 68 |
+
raise ve
|
| 69 |
+
|
| 70 |
+
def test_get_rgb_image(self):
|
| 71 |
+
output = raster_helpers.get_rgb_image(channel0, channel1, channel2, invert_image=True)
|
| 72 |
+
hash_output = hash_calculate(output)
|
| 73 |
+
assert hash_output == b'YVnRWla5Ptfet6reSfM+OEIsGytLkeso6X+CRs34YHk='
|
| 74 |
+
output = raster_helpers.get_rgb_image(channel0, channel1, channel2, invert_image=False)
|
| 75 |
+
hash_output = hash_calculate(output)
|
| 76 |
+
assert hash_output == b'LC/kIZGUZULSrwwSXCeP1My2spTZdW9D7LH+tltwERs='
|
| 77 |
+
|
| 78 |
+
def test_get_rgb_image_value_error_1(self):
|
| 79 |
+
with self.assertRaises(ValueError):
|
| 80 |
+
try:
|
| 81 |
+
raster_helpers.get_rgb_image(arr_5x5x5, arr_5x5x5, arr_5x5x5, invert_image=True)
|
| 82 |
+
except ValueError as ve:
|
| 83 |
+
self.assertEqual(f"arr_size, wrong type:{type(arr_5x5x5)} or arr_size:{arr_5x5x5.shape}.", str(ve))
|
| 84 |
+
raise ve
|
| 85 |
+
|
| 86 |
+
def test_get_rgb_image_value_error2(self):
|
| 87 |
+
arr_0 = np.arange(25).reshape((5, 5))
|
| 88 |
+
arr_1 = np.arange(4).reshape((2, 2))
|
| 89 |
+
with self.assertRaises(ValueError):
|
| 90 |
+
try:
|
| 91 |
+
raster_helpers.get_rgb_image(arr_0, arr_1, channel2, invert_image=True)
|
| 92 |
+
except ValueError as ve:
|
| 93 |
+
self.assertEqual('could not broadcast input array from shape (2,2) into shape (5,5)', str(ve))
|
| 94 |
+
raise ve
|
| 95 |
+
|
| 96 |
+
def test_get_slope_curvature(self):
|
| 97 |
+
slope_output, curvature_output = raster_helpers.get_slope_curvature(z, slope_cellsize=3)
|
| 98 |
+
hash_curvature = hash_calculate(curvature_output)
|
| 99 |
+
hash_slope = hash_calculate(slope_output)
|
| 100 |
+
assert hash_curvature == b'LAL9JFOjJP9D6X4X3fVCpnitx9VPM9drS5YMHwMZ3iE='
|
| 101 |
+
assert hash_slope == b'IYf6x4G0lmR47j6HRS5kUYWdtmimhLz2nak8py75nwc='
|
| 102 |
+
|
| 103 |
+
def test_get_slope_curvature_value_error(self):
|
| 104 |
+
from samgis.io import raster_helpers
|
| 105 |
+
|
| 106 |
+
with self.assertRaises(ValueError):
|
| 107 |
+
try:
|
| 108 |
+
raster_helpers.get_slope_curvature(np.array(1), slope_cellsize=3)
|
| 109 |
+
except ValueError as ve:
|
| 110 |
+
self.assertEqual('not enough values to unpack (expected 2, got 0)', str(ve))
|
| 111 |
+
raise ve
|
| 112 |
+
|
| 113 |
+
def test_calculate_slope(self):
|
| 114 |
+
slope_output = raster_helpers.calculate_slope(z, cell_size=3)
|
| 115 |
+
hash_output = hash_calculate(slope_output)
|
| 116 |
+
assert hash_output == b'IYf6x4G0lmR47j6HRS5kUYWdtmimhLz2nak8py75nwc='
|
| 117 |
+
|
| 118 |
+
def test_calculate_slope_value_error(self):
|
| 119 |
+
with self.assertRaises(ValueError):
|
| 120 |
+
try:
|
| 121 |
+
raster_helpers.calculate_slope(np.array(1), cell_size=3)
|
| 122 |
+
except ValueError as ve:
|
| 123 |
+
self.assertEqual('not enough values to unpack (expected 2, got 0)', str(ve))
|
| 124 |
+
raise ve
|
| 125 |
+
|
| 126 |
+
def test_normalize_array(self):
|
| 127 |
+
def check_ndarrays_almost_equal(cls, arr1, arr2, places, check_type="float", check_ndiff=1):
|
| 128 |
+
count_abs_diff = 0
|
| 129 |
+
for list00, list01 in zip(arr1.tolist(), arr2.tolist()):
|
| 130 |
+
for el00, el01 in zip(list00, list01):
|
| 131 |
+
ndiff = abs(el00 - el01)
|
| 132 |
+
if el00 != el01:
|
| 133 |
+
count_abs_diff += 1
|
| 134 |
+
if check_type == "float":
|
| 135 |
+
cls.assertAlmostEqual(el00, el01, places=places)
|
| 136 |
+
cls.assertTrue(ndiff < check_ndiff)
|
| 137 |
+
print("count_abs_diff:", count_abs_diff)
|
| 138 |
+
|
| 139 |
+
normalized_array = raster_helpers.normalize_array(z)
|
| 140 |
+
hash_output = hash_calculate(normalized_array)
|
| 141 |
+
assert hash_output == b'MPkQwiiQa5NxL7LDvCS9V143YUEJT/Qh1aNEKc/Ehvo='
|
| 142 |
+
|
| 143 |
+
mult_variable = 3.423
|
| 144 |
+
test_array_input = np.arange(256).reshape((16, 16))
|
| 145 |
+
test_array_output = raster_helpers.normalize_array(test_array_input * mult_variable)
|
| 146 |
+
check_ndarrays_almost_equal(self, test_array_output, test_array_input, places=8)
|
| 147 |
+
|
| 148 |
+
test_array_output1 = raster_helpers.normalize_array(test_array_input * mult_variable, high=128, norm_type="int")
|
| 149 |
+
o = np.arange(256).reshape((16, 16)) / 2
|
| 150 |
+
expected_array_output1 = o.astype(int)
|
| 151 |
+
check_ndarrays_almost_equal(
|
| 152 |
+
self, test_array_output1, expected_array_output1, places=2, check_type="int", check_ndiff=2)
|
| 153 |
+
|
| 154 |
+
@patch.object(np, "nanmin")
|
| 155 |
+
@patch.object(np, "nanmax")
|
| 156 |
+
def test_normalize_array_floating_point_error_mocked(self, nanmax_mocked, nanmin_mocked):
|
| 157 |
+
nanmax_mocked.return_value = 100
|
| 158 |
+
nanmin_mocked.return_value = 100
|
| 159 |
+
|
| 160 |
+
with self.assertRaises(ValueError):
|
| 161 |
+
try:
|
| 162 |
+
raster_helpers.normalize_array(
|
| 163 |
+
np.arange(25).reshape((5, 5))
|
| 164 |
+
)
|
| 165 |
+
except ValueError as ve:
|
| 166 |
+
self.assertEqual(
|
| 167 |
+
"normalize_array:::h_arr_max:100,h_min_arr:100,fe:divide by zero encountered in divide.",
|
| 168 |
+
str(ve)
|
| 169 |
+
)
|
| 170 |
+
raise ve
|
| 171 |
+
|
| 172 |
+
@patch.object(np, "nanmin")
|
| 173 |
+
@patch.object(np, "nanmax")
|
| 174 |
+
def test_normalize_array_exception_error_mocked(self, nanmax_mocked, nanmin_mocked):
|
| 175 |
+
nanmax_mocked.return_value = 100
|
| 176 |
+
nanmin_mocked.return_value = np.NaN
|
| 177 |
+
|
| 178 |
+
with self.assertRaises(ValueError):
|
| 179 |
+
try:
|
| 180 |
+
raster_helpers.normalize_array(
|
| 181 |
+
np.arange(25).reshape((5, 5))
|
| 182 |
+
)
|
| 183 |
+
except ValueError as ve:
|
| 184 |
+
self.assertEqual("cannot convert float NaN to integer", str(ve))
|
| 185 |
+
raise ve
|
| 186 |
+
|
| 187 |
+
def test_normalize_array_value_error(self):
|
| 188 |
+
with self.assertRaises(ValueError):
|
| 189 |
+
try:
|
| 190 |
+
raster_helpers.normalize_array(
|
| 191 |
+
np.zeros((5, 5))
|
| 192 |
+
)
|
| 193 |
+
except ValueError as ve:
|
| 194 |
+
self.assertEqual(
|
| 195 |
+
"normalize_array::empty array '',h_min_arr:0.0,h_arr_max:0.0,h_diff:0.0, " 'dtype:float64.',
|
| 196 |
+
str(ve)
|
| 197 |
+
)
|
| 198 |
+
raise ve
|
| 199 |
+
|
| 200 |
+
def test_normalize_array_list(self):
|
| 201 |
+
normalized_array = raster_helpers.normalize_array_list([channel0, channel1, channel2])
|
| 202 |
+
hash_output = hash_calculate(normalized_array)
|
| 203 |
+
assert hash_output == b'+6IbhIpyb3vPElTgqqPkQdIR0umf4uFP2c7t5IaBVvI='
|
| 204 |
+
|
| 205 |
+
test_norm_list_output2 = raster_helpers.normalize_array_list(
|
| 206 |
+
[channel0, channel1, channel2], exaggerations_list=[2.0, 3.0, 5.0])
|
| 207 |
+
hash_variable2 = hash_calculate(test_norm_list_output2)
|
| 208 |
+
assert hash_variable2 == b'yYCYWCKO3i8NYsWk/wgYOzSRRLSLUprEs7mChJkdL+A='
|
| 209 |
+
|
| 210 |
+
def test_normalize_array_list_value_error(self):
|
| 211 |
+
with self.assertRaises(ValueError):
|
| 212 |
+
try:
|
| 213 |
+
raster_helpers.normalize_array_list([])
|
| 214 |
+
except ValueError as ve:
|
| 215 |
+
self.assertEqual("input list can't be empty:[].", str(ve))
|
| 216 |
+
raise ve
|
| 217 |
+
|
| 218 |
+
def test_check_empty_array(self):
|
| 219 |
+
a = np.zeros((10, 10))
|
| 220 |
+
b = np.ones((10, 10))
|
| 221 |
+
c = np.ones((10, 10)) * 2
|
| 222 |
+
d = np.zeros((10, 10))
|
| 223 |
+
d[1, 1] = np.nan
|
| 224 |
+
e = np.ones((10, 10)) * 3
|
| 225 |
+
e[1, 1] = np.nan
|
| 226 |
+
|
| 227 |
+
self.assertTrue(raster_helpers.check_empty_array(a, 999))
|
| 228 |
+
self.assertTrue(raster_helpers.check_empty_array(b, 0))
|
| 229 |
+
self.assertTrue(raster_helpers.check_empty_array(c, 2))
|
| 230 |
+
self.assertTrue(raster_helpers.check_empty_array(d, 0))
|
| 231 |
+
self.assertTrue(raster_helpers.check_empty_array(e, 3))
|
| 232 |
+
self.assertFalse(raster_helpers.check_empty_array(z, 3))
|
| 233 |
+
|
| 234 |
+
def test_get_nextzen_terrain_rgb_formula(self):
|
| 235 |
+
output = raster_helpers.get_nextzen_terrain_rgb_formula(channel0, channel1, channel2)
|
| 236 |
+
hash_output = hash_calculate(output)
|
| 237 |
+
assert hash_output == b'3KJ81YKmQRdccRZARbByfwo1iMVLj8xxz9mfsWki/qA='
|
| 238 |
+
|
| 239 |
+
def test_get_mapbox__terrain_rgb_formula(self):
|
| 240 |
+
output = raster_helpers.get_mapbox__terrain_rgb_formula(channel0, channel1, channel2)
|
| 241 |
+
hash_output = hash_calculate(output)
|
| 242 |
+
assert hash_output == b'RU7CcoKoR3Fkh5LE+m48DHRVUy/vGq6UgfOFUMXx07M='
|
| 243 |
+
|
| 244 |
+
def test_get_raster_terrain_rgb_like(self):
|
| 245 |
+
from samgis.utilities.type_hints import XYZTerrainProvidersNames
|
| 246 |
+
|
| 247 |
+
arr_input = raster_helpers.get_rgb_image(channel0, channel1, channel2, invert_image=True)
|
| 248 |
+
output_nextzen = raster_helpers.get_raster_terrain_rgb_like(
|
| 249 |
+
arr_input, XYZTerrainProvidersNames.NEXTZEN_TERRAIN_TILES_NAME)
|
| 250 |
+
hash_nextzen = hash_calculate(output_nextzen)
|
| 251 |
+
assert hash_nextzen == b'+o2OTJliJkkBoqiAIGnhJ4s0xoLQ4MxHOvevLhNxysE='
|
| 252 |
+
output_mapbox = raster_helpers.get_raster_terrain_rgb_like(
|
| 253 |
+
arr_input, XYZTerrainProvidersNames.MAPBOX_TERRAIN_TILES_NAME)
|
| 254 |
+
hash_mapbox = hash_calculate(output_mapbox)
|
| 255 |
+
assert hash_mapbox == b'zWmekyKrpnmHnuDACnveCJl+o4GuhtHJmGlRDVwsce4='
|