Coverage for py_tools_ds/numeric/array.py: 76%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

25 statements  

1# -*- coding: utf-8 -*- 

2 

3# py_tools_ds - A collection of geospatial data analysis tools that simplify standard 

4# operations when handling geospatial raster and vector data as well as projections. 

5# 

6# Copyright (C) 2016-2021 

7# - Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de) 

8# - Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences Potsdam, 

9# Germany (https://www.gfz-potsdam.de/) 

10# 

11# This software was developed within the context of the GeoMultiSens project funded 

12# by the German Federal Ministry of Education and Research 

13# (project grant code: 01 IS 14 010 A-C). 

14# 

15# Licensed under the Apache License, Version 2.0 (the "License"); 

16# you may not use this file except in compliance with the License. 

17# You may obtain a copy of the License at 

18# 

19# http://www.apache.org/licenses/LICENSE-2.0 

20# 

21# Unless required by applicable law or agreed to in writing, software 

22# distributed under the License is distributed on an "AS IS" BASIS, 

23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

24# See the License for the specific language governing permissions and 

25# limitations under the License. 

26 

27__author__ = "Daniel Scheffler" 

28 

29import numpy as np 

30from typing import List, Iterable # noqa F401 # flake8 issue 

31 

32 

33def get_outFillZeroSaturated(dtype) -> tuple: 

34 """Return proper 'fill-', 'zero-' and 'saturated' values with respect to the given data type. 

35 

36 :param dtype: data type 

37 """ 

38 dtype = str(np.dtype(dtype)) 

39 assert dtype in ['int8', 'uint8', 'int16', 'uint16', 'float32'], \ 

40 "get_outFillZeroSaturated: Unknown dType: '%s'." % dtype 

41 dict_outFill = {'int8': -128, 'uint8': 0, 'int16': -9999, 'uint16': 9999, 'float32': -9999.} 

42 dict_outZero = {'int8': 0, 'uint8': 1, 'int16': 0, 'uint16': 1, 'float32': 0.} 

43 dict_outSaturated = {'int8': 127, 'uint8': 255, 'int16': 32767, 'uint16': 65535, 'float32': 65535.} 

44 

45 return dict_outFill[dtype], dict_outZero[dtype], dict_outSaturated[dtype] 

46 

47 

48def get_array_tilebounds(array_shape, tile_shape): 

49 # type: (Iterable, Iterable) -> List[List[tuple]] 

50 """Calculate row/col bounds for image tiles according to the given parameters. 

51 

52 :param array_shape: dimensions of array to be tiled: (rows, columns, bands) or (rows, columns) 

53 :param tile_shape: dimensions of target tile: (rows, columns, bands) or (rows, columns) 

54 """ 

55 rows, cols = array_shape[:2] 

56 tgt_rows, tgt_cols = tile_shape[:2] 

57 tgt_rows, tgt_cols = tgt_rows or rows, tgt_cols or cols # return all rows/cols in case tile_shape contains None 

58 

59 row_bounds = [0] 

60 while row_bounds[-1] + tgt_rows < rows: 

61 row_bounds.append(row_bounds[-1] + tgt_rows - 1) 

62 row_bounds.append(row_bounds[-2] + tgt_rows) 

63 else: 

64 row_bounds.append(rows - 1) 

65 

66 col_bounds = [0] 

67 while col_bounds[-1] + tgt_cols < cols: 

68 col_bounds.append(col_bounds[-1] + tgt_cols - 1) 

69 col_bounds.append(col_bounds[-2] + tgt_cols) 

70 else: 

71 col_bounds.append(cols - 1) 

72 

73 return [[tuple([row_bounds[r], row_bounds[r + 1]]), tuple([col_bounds[c], col_bounds[c + 1]])] 

74 for r in range(0, len(row_bounds), 2) for c in range(0, len(col_bounds), 2)]