Coverage for py_tools_ds/dtypes/conversion.py: 26%

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

35 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 datetime 

30 

31import numpy as np 

32from osgeo import gdal 

33 

34# dictionary to translate Numpy data types (strings) into corresponding GDAL data types, 

35# e.g. dTypeDic_NumPy2GDAL(str(np.dtype(np.uint8))) 

36dTypeDic_NumPy2GDAL = {'bool': gdal.GDT_Byte, 

37 'bool_': gdal.GDT_Int32, 

38 'int': gdal.GDT_Int32, 

39 'int8': gdal.GDT_Int16, 

40 'uint8': gdal.GDT_Byte, 

41 'uint16': gdal.GDT_UInt16, 

42 'int16': gdal.GDT_Int16, 

43 'uint32': gdal.GDT_UInt32, 

44 'int32': gdal.GDT_Int32, 

45 'int64': gdal.GDT_Float64, 

46 'float': gdal.GDT_Float32, 

47 'float16': gdal.GDT_Float32, 

48 'float32': gdal.GDT_Float32, 

49 'float64': gdal.GDT_Float64 

50 } 

51 

52# dictionary to translate GDAL data types (strings) into corresponding numpy data types 

53dTypeDic_GDAL2Numpy = {gdal.GDT_Byte: np.uint8, 

54 gdal.GDT_UInt16: np.uint16, 

55 gdal.GDT_Int16: np.int16, 

56 gdal.GDT_UInt32: np.uint32, 

57 gdal.GDT_Int32: np.int32, 

58 gdal.GDT_Float32: np.float32, 

59 gdal.GDT_Float64: np.float64, 

60 } 

61 

62# dictionary to translate Numpy data types into GDAL compatible Numpy data types 

63dTypeDic_NumPy2GDALcompatible = \ 

64 dict(zip(dTypeDic_NumPy2GDAL.keys(), 

65 [dTypeDic_GDAL2Numpy[dTypeDic_NumPy2GDAL[str(np.dtype(NDT))]] for NDT in dTypeDic_NumPy2GDAL.keys()])) 

66 

67 

68def get_dtypeStr(val): 

69 is_numpy = 'numpy' in str(type(val)) 

70 DType = str(np.dtype(val)) if is_numpy else \ 

71 'int' if isinstance(val, int) else \ 

72 'float' if isinstance(val, float) else \ 

73 'str' if isinstance(val, str) else \ 

74 'complex' if isinstance(val, complex) else \ 

75 'date' if isinstance(val, datetime.datetime) else None 

76 assert DType, 'data type not understood' 

77 return DType 

78 

79 

80def convertGdalNumpyDataType(dType): 

81 """convertGdalNumpyDataType 

82 :param dType: GDALdataType string or numpy dataType 

83 :return: corresponding dataType 

84 """ 

85 # dictionary to translate GDAL data types (strings) in corresponding numpy data types 

86 dTypeDic = {"Byte": np.uint8, "UInt16": np.uint16, "Int16": np.int16, "UInt32": np.uint32, "Int32": np.int32, 

87 "Float32": np.float32, "Float64": np.float64, "GDT_UInt32": np.uint32} 

88 outdType = None 

89 

90 if dType in dTypeDic: 

91 outdType = dTypeDic[dType] 

92 elif dType in dTypeDic.values(): 

93 for i in dTypeDic.items(): 

94 if dType == i[1]: 

95 outdType = i[0] 

96 elif dType in [np.int8, np.int64, int]: 

97 outdType = "Int32" 

98 print(">>> Warning: %s is converted to GDAL_Type 'Int_32'\n" % dType) 

99 elif dType in [bool, np.bool_]: 

100 outdType = "Byte" 

101 print(">>> Warning: %s is converted to GDAL_Type 'Byte'\n" % dType) 

102 elif dType in [float]: 

103 outdType = "Float32" 

104 print(">>> Warning: %s is converted to GDAL_Type 'Float32'\n" % dType) 

105 elif dType in [np.float16]: 

106 outdType = "Float32" 

107 print(">>> Warning: %s is converted to GDAL_Type 'Float32'\n" % dType) 

108 else: 

109 raise Exception('GEOP.convertGdalNumpyDataType: Unexpected input data type %s.' % dType) 

110 return outdType