|
@@ -16,14 +16,13 @@ import os.path as osp
|
|
from typing import List, Tuple, Union, Optional
|
|
from typing import List, Tuple, Union, Optional
|
|
|
|
|
|
import numpy as np
|
|
import numpy as np
|
|
-
|
|
|
|
-from paddlers.transforms.functions import to_uint8 as raster2uint8
|
|
|
|
-
|
|
|
|
try:
|
|
try:
|
|
from osgeo import gdal
|
|
from osgeo import gdal
|
|
except:
|
|
except:
|
|
import gdal
|
|
import gdal
|
|
|
|
|
|
|
|
+from paddlers.transforms.functions import to_uint8 as raster2uint8
|
|
|
|
+
|
|
|
|
|
|
def _get_type(type_name: str) -> int:
|
|
def _get_type(type_name: str) -> int:
|
|
if type_name in ["bool", "uint8"]:
|
|
if type_name in ["bool", "uint8"]:
|
|
@@ -53,7 +52,9 @@ class Raster:
|
|
gdal_obj: Optional[gdal.Dataset]=None,
|
|
gdal_obj: Optional[gdal.Dataset]=None,
|
|
band_list: Union[List[int], Tuple[int], None]=None,
|
|
band_list: Union[List[int], Tuple[int], None]=None,
|
|
to_uint8: bool=False) -> None:
|
|
to_uint8: bool=False) -> None:
|
|
- """ Class of read raster.
|
|
|
|
|
|
+ """
|
|
|
|
+ Class of read raster.
|
|
|
|
+
|
|
Args:
|
|
Args:
|
|
path (Optional[str]): The path of raster.
|
|
path (Optional[str]): The path of raster.
|
|
gdal_obj (Optional[Any], optional): The object of GDAL. Defaults to None.
|
|
gdal_obj (Optional[Any], optional): The object of GDAL. Defaults to None.
|
|
@@ -75,22 +76,25 @@ class Raster:
|
|
# https://www.osgeo.cn/gdal/drivers/raster/index.html
|
|
# https://www.osgeo.cn/gdal/drivers/raster/index.html
|
|
self._src_data = gdal.Open(path)
|
|
self._src_data = gdal.Open(path)
|
|
except:
|
|
except:
|
|
- raise TypeError(
|
|
|
|
- "Unsupported data format: `{}`".format(self.ext_type))
|
|
|
|
|
|
+ raise TypeError("Unsupported data format: `{}`".format(
|
|
|
|
+ self.ext_type))
|
|
else:
|
|
else:
|
|
raise ValueError("The path {0} not exists.".format(path))
|
|
raise ValueError("The path {0} not exists.".format(path))
|
|
else:
|
|
else:
|
|
if gdal_obj is not None:
|
|
if gdal_obj is not None:
|
|
self._src_data = gdal_obj
|
|
self._src_data = gdal_obj
|
|
else:
|
|
else:
|
|
- raise ValueError("At least one of `path` and `gdal_obj` is not None.")
|
|
|
|
|
|
+ raise ValueError(
|
|
|
|
+ "At least one of `path` and `gdal_obj` is not None.")
|
|
self.to_uint8 = to_uint8
|
|
self.to_uint8 = to_uint8
|
|
self._getInfo()
|
|
self._getInfo()
|
|
self.setBands(band_list)
|
|
self.setBands(band_list)
|
|
self._getType()
|
|
self._getType()
|
|
|
|
|
|
def setBands(self, band_list: Union[List[int], Tuple[int], None]) -> None:
|
|
def setBands(self, band_list: Union[List[int], Tuple[int], None]) -> None:
|
|
- """ Set band of data.
|
|
|
|
|
|
+ """
|
|
|
|
+ Set band of data.
|
|
|
|
+
|
|
Args:
|
|
Args:
|
|
band_list (Union[List[int], Tuple[int], None]):
|
|
band_list (Union[List[int], Tuple[int], None]):
|
|
band list (start with 1) or None (all of bands).
|
|
band list (start with 1) or None (all of bands).
|
|
@@ -105,16 +109,19 @@ class Raster:
|
|
format(str(self.bands)))
|
|
format(str(self.bands)))
|
|
self.band_list = band_list
|
|
self.band_list = band_list
|
|
|
|
|
|
- def getArray(
|
|
|
|
- self,
|
|
|
|
- start_loc: Union[List[int], Tuple[int, int], None]=None,
|
|
|
|
- block_size: Union[List[int], Tuple[int, int]]=[512, 512]) -> np.ndarray:
|
|
|
|
- """ Get ndarray data
|
|
|
|
|
|
+ def getArray(self,
|
|
|
|
+ start_loc: Union[List[int], Tuple[int, int], None]=None,
|
|
|
|
+ block_size: Union[List[int], Tuple[int, int]]=[512, 512]
|
|
|
|
+ ) -> np.ndarray:
|
|
|
|
+ """
|
|
|
|
+ Get ndarray data
|
|
|
|
+
|
|
Args:
|
|
Args:
|
|
start_loc (Union[List[int], Tuple[int], None], optional):
|
|
start_loc (Union[List[int], Tuple[int], None], optional):
|
|
Coordinates of the upper left corner of the block, if None means return full image.
|
|
Coordinates of the upper left corner of the block, if None means return full image.
|
|
block_size (Union[List[int], Tuple[int]], optional):
|
|
block_size (Union[List[int], Tuple[int]], optional):
|
|
Block size. Defaults to [512, 512].
|
|
Block size. Defaults to [512, 512].
|
|
|
|
+
|
|
Returns:
|
|
Returns:
|
|
np.ndarray: data's ndarray.
|
|
np.ndarray: data's ndarray.
|
|
"""
|
|
"""
|
|
@@ -144,7 +151,7 @@ class Raster:
|
|
self.bands = 1
|
|
self.bands = 1
|
|
self.geot = None
|
|
self.geot = None
|
|
self.proj = None
|
|
self.proj = None
|
|
-
|
|
|
|
|
|
+
|
|
def _getType(self) -> None:
|
|
def _getType(self) -> None:
|
|
d_name = self.getArray([0, 0], [1, 1]).dtype.name
|
|
d_name = self.getArray([0, 0], [1, 1]).dtype.name
|
|
self.datatype = _get_type(d_name)
|
|
self.datatype = _get_type(d_name)
|
|
@@ -159,9 +166,9 @@ class Raster:
|
|
ima = np.stack(band_array, axis=0)
|
|
ima = np.stack(band_array, axis=0)
|
|
return ima
|
|
return ima
|
|
|
|
|
|
- def _getArray(
|
|
|
|
- self,
|
|
|
|
- window: Union[None, List[int], Tuple[int, int, int, int]]=None) -> np.ndarray:
|
|
|
|
|
|
+ def _getArray(self,
|
|
|
|
+ window: Union[None, List[int], Tuple[int, int, int, int]]=None
|
|
|
|
+ ) -> np.ndarray:
|
|
if self._src_data is None:
|
|
if self._src_data is None:
|
|
raise ValueError("The raster is None.")
|
|
raise ValueError("The raster is None.")
|
|
if window is not None:
|
|
if window is not None:
|
|
@@ -193,10 +200,10 @@ class Raster:
|
|
ima = raster2uint8(ima)
|
|
ima = raster2uint8(ima)
|
|
return ima
|
|
return ima
|
|
|
|
|
|
- def _getBlock(
|
|
|
|
- self,
|
|
|
|
- start_loc: Union[List[int], Tuple[int, int]],
|
|
|
|
- block_size: Union[List[int], Tuple[int, int]]=[512, 512]) -> np.ndarray:
|
|
|
|
|
|
+ def _getBlock(self,
|
|
|
|
+ start_loc: Union[List[int], Tuple[int, int]],
|
|
|
|
+ block_size: Union[List[int], Tuple[int, int]]=[512, 512]
|
|
|
|
+ ) -> np.ndarray:
|
|
if len(start_loc) != 2 or len(block_size) != 2:
|
|
if len(start_loc) != 2 or len(block_size) != 2:
|
|
raise ValueError("The length start_loc/block_size must be 2.")
|
|
raise ValueError("The length start_loc/block_size must be 2.")
|
|
xoff, yoff = start_loc
|
|
xoff, yoff = start_loc
|
|
@@ -220,9 +227,9 @@ class Raster:
|
|
return tmp
|
|
return tmp
|
|
|
|
|
|
|
|
|
|
-def save_geotiff(image: np.ndarray,
|
|
|
|
- save_path: str,
|
|
|
|
- proj: str,
|
|
|
|
|
|
+def save_geotiff(image: np.ndarray,
|
|
|
|
+ save_path: str,
|
|
|
|
+ proj: str,
|
|
geotf: Tuple,
|
|
geotf: Tuple,
|
|
use_type: Optional[int]=None,
|
|
use_type: Optional[int]=None,
|
|
clear_ds: bool=True) -> None:
|
|
clear_ds: bool=True) -> None:
|