rsgispy 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rsgispy/__init__.py ADDED
@@ -0,0 +1,254 @@
1
+ """
2
+ rsgispy: Pure Python replacement for the RSGISLib library.
3
+ Provides remote sensing and GIS software tools and utilities.
4
+
5
+ Data Types for images:
6
+
7
+ * TYPE_UNDEFINED = 0
8
+ * TYPE_8INT = 1
9
+ * TYPE_16INT = 2
10
+ * TYPE_32INT = 3
11
+ * TYPE_64INT = 4
12
+ * TYPE_8UINT = 5
13
+ * TYPE_16UINT = 6
14
+ * TYPE_32UINT = 7
15
+ * TYPE_64UINT = 8
16
+ * TYPE_32FLOAT = 9
17
+ * TYPE_64FLOAT = 10
18
+ """
19
+
20
+ from typing import Union
21
+ import numpy as np
22
+ from rasterio.enums import Resampling
23
+
24
+ __version__ = "0.0.1"
25
+
26
+ TYPE_UNDEFINED = 0
27
+ TYPE_8INT = 1
28
+ TYPE_16INT = 2
29
+ TYPE_32INT = 3
30
+ TYPE_64INT = 4
31
+ TYPE_8UINT = 5
32
+ TYPE_16UINT = 6
33
+ TYPE_32UINT = 7
34
+ TYPE_64UINT = 8
35
+ TYPE_32FLOAT = 9
36
+ TYPE_64FLOAT = 10
37
+
38
+ INTERP_NEAREST_NEIGHBOUR = 0
39
+ INTERP_BILINEAR = 1
40
+ INTERP_CUBIC = 2
41
+ INTERP_CUBICSPLINE = 3
42
+ INTERP_LANCZOS = 4
43
+ INTERP_AVERAGE = 5
44
+ INTERP_MODE = 6
45
+ INTERP_MIN = 7
46
+ INTERP_MAX = 8
47
+ INTERP_MEDIAN = 9
48
+ INTERP_Q1 = 10
49
+ INTERP_Q3 = 11
50
+ INTERP_SUM = 12
51
+
52
+ IMG_STRETCH_NONE = 0
53
+ IMG_STRETCH_USER = 1
54
+ IMG_STRETCH_LINEAR = 2
55
+ IMG_STRETCH_STDEV = 3
56
+ IMG_STRETCH_CUMULATIVE = 4
57
+
58
+ _RSGIS_TO_RASTERIO_INTERP = {
59
+ INTERP_NEAREST_NEIGHBOUR: Resampling.nearest,
60
+ INTERP_BILINEAR: Resampling.bilinear,
61
+ INTERP_CUBIC: Resampling.cubic,
62
+ INTERP_CUBICSPLINE: Resampling.cubic_spline,
63
+ INTERP_LANCZOS: Resampling.lanczos,
64
+ INTERP_AVERAGE: Resampling.average,
65
+ INTERP_MODE: Resampling.mode,
66
+ INTERP_MIN: Resampling.min,
67
+ INTERP_MAX: Resampling.max,
68
+ INTERP_MEDIAN: Resampling.med,
69
+ INTERP_Q1: Resampling.q1,
70
+ INTERP_Q3: Resampling.q3,
71
+ INTERP_SUM: Resampling.sum,
72
+ }
73
+
74
+ _RSGIS_TO_RASTERIO_DTYPE = {
75
+ TYPE_8INT: "int8",
76
+ TYPE_16INT: "int16",
77
+ TYPE_32INT: "int32",
78
+ TYPE_64INT: "int64",
79
+ TYPE_8UINT: "uint8",
80
+ TYPE_16UINT: "uint16",
81
+ TYPE_32UINT: "uint32",
82
+ TYPE_64UINT: "uint64",
83
+ TYPE_32FLOAT: "float32",
84
+ TYPE_64FLOAT: "float64",
85
+ }
86
+
87
+ _RSGIS_TO_NUMPY_DTYPE = {
88
+ TYPE_8INT: np.int8,
89
+ TYPE_16INT: np.int16,
90
+ TYPE_32INT: np.int32,
91
+ TYPE_64INT: np.int64,
92
+ TYPE_8UINT: np.uint8,
93
+ TYPE_16UINT: np.uint16,
94
+ TYPE_32UINT: np.uint32,
95
+ TYPE_64UINT: np.uint64,
96
+ TYPE_32FLOAT: np.float32,
97
+ TYPE_64FLOAT: np.float64,
98
+ }
99
+
100
+ _RSGIS_TO_STR_DTYPE = {
101
+ TYPE_8INT: "8 bit INTEGER",
102
+ TYPE_16INT: "16 bit INTEGER",
103
+ TYPE_32INT: "32 bit INTEGER",
104
+ TYPE_64INT: "64 bit INTEGER",
105
+ TYPE_8UINT: "8 bit UNSIGNED INTEGER (BYTE)",
106
+ TYPE_16UINT: "16 bit UNSIGNED INTEGER",
107
+ TYPE_32UINT: "32 bit UNSIGNED INTEGER",
108
+ TYPE_64UINT: "64 bit UNSIGNED INTEGER",
109
+ TYPE_32FLOAT: "32 bit FLOAT",
110
+ TYPE_64FLOAT: "64 bit FLOAT",
111
+ }
112
+
113
+ _STR_TO_RSGIS_DTYPE = {
114
+ "int8": TYPE_8INT,
115
+ "int16": TYPE_16INT,
116
+ "int32": TYPE_32INT,
117
+ "int64": TYPE_64INT,
118
+ "byte": TYPE_8UINT,
119
+ "uint8": TYPE_8UINT,
120
+ "uint16": TYPE_16UINT,
121
+ "uint32": TYPE_32UINT,
122
+ "uint64": TYPE_64UINT,
123
+ "float32": TYPE_32FLOAT,
124
+ "float64": TYPE_64FLOAT,
125
+ }
126
+
127
+
128
+ class RSGISPyException(Exception):
129
+ """
130
+ A class representing an RSGISPy exception.
131
+ """
132
+
133
+ def __init__(self, value: str):
134
+ """
135
+ Init for the RSGISPyException class.
136
+ """
137
+ super().__init__(value)
138
+ self.value = value
139
+
140
+ def __str__(self) -> str:
141
+ """
142
+ Return a string representation of the exception.
143
+ """
144
+ return repr(self.value)
145
+
146
+
147
+ def get_rsgis_datatype(gdal_type: str) -> int:
148
+ """
149
+ Convert from a GDAL/rasterio data type string to an RSGIS data type int.
150
+
151
+ :param gdal_type: Data type name string (e.g., 'float32', 'uint8', 'byte').
152
+ :return: RSGIS data type constant (e.g., rsgispy.TYPE_32FLOAT).
153
+ """
154
+ key = gdal_type.lower().strip()
155
+ if key not in _STR_TO_RSGIS_DTYPE:
156
+ raise RSGISPyException(
157
+ f"The data type '{gdal_type}' is unknown / not supported."
158
+ )
159
+ return _STR_TO_RSGIS_DTYPE[key]
160
+
161
+
162
+ def get_rasterio_datatype(rsgislib_datatype: int) -> str:
163
+ """
164
+ Convert from an RSGIS data type int to a rasterio/GDAL dtype string.
165
+
166
+ :param rsgislib_datatype: RSGIS data type constant (e.g., rsgispy.TYPE_32FLOAT).
167
+ :return: Rasterio dtype string (e.g., 'float32').
168
+ """
169
+ if rsgislib_datatype not in _RSGIS_TO_RASTERIO_DTYPE:
170
+ raise RSGISPyException(
171
+ f"The data type '{rsgislib_datatype}' is unknown / not supported."
172
+ )
173
+ return _RSGIS_TO_RASTERIO_DTYPE[rsgislib_datatype]
174
+
175
+
176
+ def get_numpy_datatype(rsgislib_datatype: int):
177
+ """
178
+ Convert from an RSGIS data type int to a NumPy dtype.
179
+
180
+ :param rsgislib_datatype: RSGIS data type constant (e.g., rsgispy.TYPE_32FLOAT).
181
+ :return: NumPy dtype class (e.g., numpy.float32).
182
+ """
183
+ if rsgislib_datatype not in _RSGIS_TO_NUMPY_DTYPE:
184
+ raise RSGISPyException("Data type was not recognised.")
185
+ return _RSGIS_TO_NUMPY_DTYPE[rsgislib_datatype]
186
+
187
+
188
+ def get_rsgis_datatype_str(rsgislib_datatype: int) -> str:
189
+ """
190
+ Get a human-readable string representation of the RSGIS data type.
191
+
192
+ :param rsgislib_datatype: RSGIS data type constant.
193
+ :return: Human-readable string description.
194
+ """
195
+ if rsgislib_datatype not in _RSGIS_TO_STR_DTYPE:
196
+ raise RSGISPyException(
197
+ f"The data type '{rsgislib_datatype}' is unknown / not supported."
198
+ )
199
+ return _RSGIS_TO_STR_DTYPE[rsgislib_datatype]
200
+
201
+
202
+ def get_rasterio_interp_type(
203
+ rsgis_interp_method: Union[int, Resampling],
204
+ ) -> Resampling:
205
+ """
206
+ Convert an ``rsgispy.INTERP_*`` (or ``rsgislib.INTERP_*``) constant to a
207
+ ``rasterio.enums.Resampling`` enum member.
208
+
209
+ :param rsgis_interp_method: Interpolation constant or ``Resampling`` enum.
210
+ :return: Corresponding ``rasterio.enums.Resampling`` value.
211
+ """
212
+ if isinstance(rsgis_interp_method, Resampling):
213
+ return rsgis_interp_method
214
+ if rsgis_interp_method not in _RSGIS_TO_RASTERIO_INTERP:
215
+ raise RSGISPyException(
216
+ "Interpolation method was not recognised or known."
217
+ )
218
+ return _RSGIS_TO_RASTERIO_INTERP[rsgis_interp_method]
219
+
220
+
221
+ def is_notebook() -> bool:
222
+ """
223
+ Determine whether code is executing inside a Jupyter notebook environment.
224
+
225
+ :return: True if running within a Jupyter notebook or qtconsole, False otherwise.
226
+ """
227
+ try:
228
+ shell = get_ipython().__class__.__name__ # type: ignore[name-defined]
229
+ if shell == "ZMQInteractiveShell":
230
+ return True
231
+ return False
232
+ except NameError:
233
+ return False
234
+
235
+
236
+ def get_tqdm():
237
+ """
238
+ Return the appropriate tqdm progress bar callable for the current environment
239
+ (Jupyter notebook widget when inside a notebook and ipywidgets is available,
240
+ or standard terminal tqdm otherwise).
241
+
242
+ :return: tqdm class/callable.
243
+ """
244
+ if is_notebook():
245
+ try:
246
+ import ipywidgets # noqa: F401 # pylint: disable=unused-import,import-outside-toplevel
247
+ import tqdm.notebook # pylint: disable=import-outside-toplevel
248
+
249
+ return tqdm.notebook.tqdm
250
+ except ImportError:
251
+ pass
252
+ import tqdm # pylint: disable=import-outside-toplevel
253
+
254
+ return tqdm.tqdm