raster.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os.path as osp
  15. from typing import List, Tuple, Union, Optional
  16. import numpy as np
  17. try:
  18. from osgeo import gdal
  19. except:
  20. import gdal
  21. from paddlers.transforms.functions import to_uint8 as raster2uint8
  22. def _get_type(type_name: str) -> int:
  23. if type_name in ["bool", "uint8"]:
  24. gdal_type = gdal.GDT_Byte
  25. elif type_name in ["int8", "int16"]:
  26. gdal_type = gdal.GDT_Int16
  27. elif type_name == "uint16":
  28. gdal_type = gdal.GDT_UInt16
  29. elif type_name == "int32":
  30. gdal_type = gdal.GDT_Int32
  31. elif type_name == "uint32":
  32. gdal_type = gdal.GDT_UInt32
  33. elif type_name in ["int64", "uint64", "float16", "float32"]:
  34. gdal_type = gdal.GDT_Float32
  35. elif type_name == "float64":
  36. gdal_type = gdal.GDT_Float64
  37. elif type_name == "complex64":
  38. gdal_type = gdal.GDT_CFloat64
  39. else:
  40. raise TypeError("Non-suported data type {}.".format(type_name))
  41. return gdal_type
  42. class Raster:
  43. def __init__(self,
  44. path: str,
  45. gdal_obj: Optional[gdal.Dataset]=None,
  46. band_list: Union[List[int], Tuple[int], None]=None,
  47. to_uint8: bool=False) -> None:
  48. """
  49. Reader of raster files.
  50. Args:
  51. path (str): Path of raster file.
  52. gdal_obj (gdal.Dataset|None, optional): GDAL dataset. Defaults to None.
  53. band_list (list[int] | tuple[int] | None, optional): Select a set of
  54. bands (the band index starts from 1). If None, read all bands.
  55. Defaults to None.
  56. to_uint8 (bool, optional): Whether to convert data type to uint8.
  57. Defaults to False.
  58. """
  59. super(Raster, self).__init__()
  60. if path is not None:
  61. if osp.exists(path):
  62. self.path = path
  63. self.ext_type = path.split(".")[-1]
  64. if self.ext_type.lower() in ["npy", "npz"]:
  65. self._src_data = None
  66. else:
  67. try:
  68. # raster format support in GDAL:
  69. # https://www.osgeo.cn/gdal/drivers/raster/index.html
  70. self._src_data = gdal.Open(path)
  71. except:
  72. raise TypeError("Unsupported data format: {}".format(
  73. self.ext_type))
  74. else:
  75. raise ValueError("The path {0} does not exist.".format(path))
  76. else:
  77. if gdal_obj is not None:
  78. self._src_data = gdal_obj
  79. else:
  80. raise ValueError(
  81. "At least one of `path` and `gdal_obj` is not None.")
  82. self.to_uint8 = to_uint8
  83. self._getInfo()
  84. self.setBands(band_list)
  85. self._getType()
  86. def setBands(self, band_list: Union[List[int], Tuple[int], None]) -> None:
  87. """
  88. Set bands of data.
  89. Args:
  90. band_list (list[int] | tuple[int] | None, optional): Select a set of
  91. bands (the band index starts from 1). If None, read all bands.
  92. Defaults to None.
  93. """
  94. if band_list is not None:
  95. if len(band_list) > self.bands:
  96. raise ValueError(
  97. "The lenght of band_list must be less than {0}.".format(
  98. str(self.bands)))
  99. if max(band_list) > self.bands or min(band_list) < 1:
  100. raise ValueError("The range of band_list must within [1, {0}].".
  101. format(str(self.bands)))
  102. self.band_list = band_list
  103. def getArray(self,
  104. start_loc: Union[List[int], Tuple[int, int], None]=None,
  105. block_size: Union[List[int], Tuple[int, int]]=[512, 512]
  106. ) -> np.ndarray:
  107. """
  108. Fetch data in a ndarray.
  109. Args:
  110. start_loc (list[int] | tuple[int] | None, optional): Coordinates of the
  111. upper left corner of the block. None value means returning full image.
  112. block_size (list[int] | tuple[int], optional): Block size.
  113. Defaults to [512, 512].
  114. Returns:
  115. np.ndarray: data's ndarray.
  116. """
  117. if self._src_data is not None:
  118. if start_loc is None:
  119. return self._getArray()
  120. else:
  121. return self._getBlock(start_loc, block_size)
  122. else:
  123. print("Numpy doesn't support blocking temporarily.")
  124. return self._getNumpy()
  125. def _getInfo(self) -> None:
  126. if self._src_data is not None:
  127. self.width = self._src_data.RasterXSize
  128. self.height = self._src_data.RasterYSize
  129. self.bands = self._src_data.RasterCount
  130. self.geot = self._src_data.GetGeoTransform()
  131. self.proj = self._src_data.GetProjection()
  132. else:
  133. d_img = self._getNumpy()
  134. d_shape = d_img.shape
  135. if len(d_shape) == 3:
  136. self.height, self.width, self.bands = d_shape
  137. else:
  138. self.height, self.width = d_shape
  139. self.bands = 1
  140. self.geot = None
  141. self.proj = None
  142. def _getType(self) -> None:
  143. d_name = self.getArray([0, 0], [1, 1]).dtype.name
  144. self.datatype = _get_type(d_name)
  145. def _getNumpy(self):
  146. ima = np.load(self.path)
  147. if self.band_list is not None:
  148. band_array = []
  149. for b in self.band_list:
  150. band_i = ima[:, :, b - 1]
  151. band_array.append(band_i)
  152. ima = np.stack(band_array, axis=0)
  153. return ima
  154. def _getArray(self,
  155. window: Union[None, List[int], Tuple[int, int, int, int]]=None
  156. ) -> np.ndarray:
  157. if self._src_data is None:
  158. raise ValueError("The raster is None.")
  159. if window is not None:
  160. xoff, yoff, xsize, ysize = window
  161. if self.band_list is None:
  162. if window is None:
  163. ima = self._src_data.ReadAsArray()
  164. else:
  165. ima = self._src_data.ReadAsArray(xoff, yoff, xsize, ysize)
  166. else:
  167. band_array = []
  168. for b in self.band_list:
  169. if window is None:
  170. band_i = self._src_data.GetRasterBand(b).ReadAsArray()
  171. else:
  172. band_i = self._src_data.GetRasterBand(b).ReadAsArray(
  173. xoff, yoff, xsize, ysize)
  174. band_array.append(band_i)
  175. ima = np.stack(band_array, axis=0)
  176. if self.bands == 1:
  177. if len(ima.shape) == 3:
  178. ima = ima.squeeze(0)
  179. # the type is complex means this is a SAR data
  180. if isinstance(type(ima[0, 0]), complex):
  181. ima = abs(ima)
  182. else:
  183. ima = ima.transpose((1, 2, 0))
  184. if self.to_uint8 is True:
  185. ima = raster2uint8(ima)
  186. return ima
  187. def _getBlock(self,
  188. start_loc: Union[List[int], Tuple[int, int]],
  189. block_size: Union[List[int], Tuple[int, int]]=[512, 512]
  190. ) -> np.ndarray:
  191. if len(start_loc) != 2 or len(block_size) != 2:
  192. raise ValueError("The length start_loc/block_size must be 2.")
  193. xoff, yoff = start_loc
  194. xsize, ysize = block_size
  195. if (xoff < 0 or xoff > self.width) or (yoff < 0 or yoff > self.height):
  196. raise ValueError("start_loc must be within [0-{0}, 0-{1}].".format(
  197. str(self.width), str(self.height)))
  198. if xoff + xsize > self.width:
  199. xsize = self.width - xoff
  200. if yoff + ysize > self.height:
  201. ysize = self.height - yoff
  202. ima = self._getArray([int(xoff), int(yoff), int(xsize), int(ysize)])
  203. h, w = ima.shape[:2] if len(ima.shape) == 3 else ima.shape
  204. if self.bands != 1:
  205. tmp = np.zeros(
  206. (block_size[0], block_size[1], self.bands), dtype=ima.dtype)
  207. tmp[:h, :w, :] = ima
  208. else:
  209. tmp = np.zeros((block_size[0], block_size[1]), dtype=ima.dtype)
  210. tmp[:h, :w] = ima
  211. return tmp
  212. def save_geotiff(image: np.ndarray,
  213. save_path: str,
  214. proj: str,
  215. geotf: Tuple,
  216. use_type: Optional[int]=None,
  217. clear_ds: bool=True) -> None:
  218. if len(image.shape) == 2:
  219. height, width = image.shape
  220. channel = 1
  221. else:
  222. height, width, channel = image.shape
  223. if use_type is not None:
  224. data_type = use_type
  225. else:
  226. data_type = _get_type(image.dtype.name)
  227. driver = gdal.GetDriverByName("GTiff")
  228. dst_ds = driver.Create(save_path, width, height, channel, data_type)
  229. dst_ds.SetGeoTransform(geotf)
  230. dst_ds.SetProjection(proj)
  231. if channel > 1:
  232. for i in range(channel):
  233. band = dst_ds.GetRasterBand(i + 1)
  234. band.WriteArray(image[:, :, i])
  235. dst_ds.FlushCache()
  236. else:
  237. band = dst_ds.GetRasterBand(1)
  238. band.WriteArray(image)
  239. dst_ds.FlushCache()
  240. if clear_ds:
  241. dst_ds = None
  242. return dst_ds