Read or writes pixel values for this band. Blocks of data can be accessed by specifying the width, height and offset of the desired block. The same specification can be used to update parts of a raster by providing an array of values. Allowed input data type
(self, data=None, offset=None, size=None, shape=None, as_memoryview=False)
| 186 | return color |
| 187 | |
| 188 | def data(self, data=None, offset=None, size=None, shape=None, as_memoryview=False): |
| 189 | """ |
| 190 | Read or writes pixel values for this band. Blocks of data can |
| 191 | be accessed by specifying the width, height and offset of the |
| 192 | desired block. The same specification can be used to update |
| 193 | parts of a raster by providing an array of values. |
| 194 | |
| 195 | Allowed input data types are bytes, memoryview, list, tuple, and array. |
| 196 | """ |
| 197 | offset = offset or (0, 0) |
| 198 | size = size or (self.width - offset[0], self.height - offset[1]) |
| 199 | shape = shape or size |
| 200 | if any(x <= 0 for x in size): |
| 201 | raise ValueError("Offset too big for this raster.") |
| 202 | |
| 203 | if size[0] > self.width or size[1] > self.height: |
| 204 | raise ValueError("Size is larger than raster.") |
| 205 | |
| 206 | # Create ctypes type array generator |
| 207 | ctypes_array = GDAL_TO_CTYPES[self.datatype()] * (shape[0] * shape[1]) |
| 208 | |
| 209 | if data is None: |
| 210 | # Set read mode |
| 211 | access_flag = 0 |
| 212 | # Prepare empty ctypes array |
| 213 | data_array = ctypes_array() |
| 214 | else: |
| 215 | # Set write mode |
| 216 | access_flag = 1 |
| 217 | |
| 218 | # Instantiate ctypes array holding the input data |
| 219 | if isinstance(data, (bytes, memoryview)) or ( |
| 220 | numpy and isinstance(data, numpy.ndarray) |
| 221 | ): |
| 222 | data_array = ctypes_array.from_buffer_copy(data) |
| 223 | else: |
| 224 | data_array = ctypes_array(*data) |
| 225 | |
| 226 | # Access band |
| 227 | capi.band_io( |
| 228 | self._ptr, |
| 229 | access_flag, |
| 230 | offset[0], |
| 231 | offset[1], |
| 232 | size[0], |
| 233 | size[1], |
| 234 | byref(data_array), |
| 235 | shape[0], |
| 236 | shape[1], |
| 237 | self.datatype(), |
| 238 | 0, |
| 239 | 0, |
| 240 | ) |
| 241 | |
| 242 | # Return data as numpy array if possible, otherwise as list |
| 243 | if data is None: |
| 244 | if as_memoryview: |
| 245 | return memoryview(data_array) |
no test coverage detected