def __py_resample( self, resolution: Union[Tuple[float, float], List[float], float] ) -> "Raster": """ Resample raster using nearest neighbor Parameters ------- resolution: tuple, list spatial resolution target Returns ------- Raster Resampled """ warnings.warn("this function will be removed in v1.0", DeprecationWarning) if isinstance(resolution, (float, int)): resampled_x_resolution = float(resolution) resampled_y_resolution = float(resolution) else: resampled_x_resolution = resolution[0] resampled_y_resolution = resolution[1] resampled_rows = round(self.y_extent / resampled_y_resolution) resampled_cols = round(self.x_extent / resampled_x_resolution) resampled_shape: Tuple[int, ...] = (resampled_rows, resampled_cols, self.layers) if self.layers == 1: resampled_shape = (resampled_rows, resampled_cols) resampled_array = np.zeros( resampled_rows * resampled_cols * self.layers, dtype=self.dtype ).reshape(resampled_shape) resampled_affine = Affine.translation(self.x_min, self.y_min) * Affine.scale( resampled_x_resolution, -resampled_y_resolution ) for row in range(resampled_rows): for col in range(resampled_cols): x, y = rowcol2xy((row, col), resampled_affine) resampled_array[row, col] = self.xy_value( x + (resampled_x_resolution / 2), y + (resampled_y_resolution / 2) ) return Raster( resampled_array, (resampled_x_resolution, resampled_y_resolution), self.x_min, self.y_max, epsg=self.epsg, )
def __py_resize(self, height: int, width: int) -> "Raster": """ Resize raster using nearest neighbor Parameters ------- height: int raster height width: int raster width Returns ------- Raster Resampled """ warnings.warn("this function will be removed in v1.0", DeprecationWarning) resized_y_resolution = self.y_extent / height resized_x_resolution = self.x_extent / width resized_affine = Affine.translation(self.x_min, self.y_min) * Affine.scale( resized_x_resolution, -resized_y_resolution ) resized_shape: Tuple[int, ...] = (height, width, self.layers) if self.layers == 1: resized_shape = (height, width) resized_array = np.zeros( height * width * self.layers, dtype=self.dtype ).reshape(resized_shape) for row in range(height): for col in range(width): x, y = rowcol2xy((row, col), resized_affine) resized_array[row, col] = self.xy_value( x + (resized_x_resolution / 2), y + (resized_y_resolution / 2) ) return Raster( resized_array, (resized_x_resolution, resized_y_resolution), self.x_min, self.y_max, epsg=self.epsg, )
def rowcol2xy( self, row: int, col: int, offset: str = "center" ) -> Tuple[float, float]: """Convert image coordinate (row, col) to real world coordinate Parameters ---------- row : int col : int offset : str Returns ------- Tuple[float, float] X,Y coordinate in real world """ return rowcol2xy((row, col), self.transform, offset=offset)
def rowcol2xy(self, row: int, col: int) -> Tuple[float, float]: return rowcol2xy((row, col), self.transform)