def write_splitted_images(self, target_dir, filename, filter_fun=lambda x:True, idxs_to_be_kept=None): """Write all splitted images as tif file. Parameters ---------- target_dir: str The directory to save the splitted images. filename: str The prefix od the filename. The index number will be followed by the output filename you defined, e.g. <filename>_idx_idxh_idxw;. filter_fun: function, optional, default: lambda x:True Filter specific spllitted images and not save it. The input of the function is a splitted image. If the function output is True, the splitted image will be saved. idxs_to_be_kept: list, optional list of indexs of splitted image to save as file. """ df_attribute = self.get_geo_attribute(return_geo_transform=True) splitted_images = self.get_splitted_images() idxs_to_be_kept = range(len(df_attribute)) if idxs_to_be_kept is None else idxs_to_be_kept for idx, row in df_attribute.iterrows(): if idx in idxs_to_be_kept: target_img = splitted_images[idx].copy() idx_str = "_" + ("%3i"%idx).replace(" ", "0") idx_h_str = "_" + ("%3i"%row['idx_h']).replace(" ", "0") idx_w_str = "_" + ("%3i"%row['idx_w']).replace(" ", "0") path = os.path.join(target_dir, filename + idx_str + idx_h_str + idx_w_str + ".tif") gt = row['geo_transform'] if filter_fun(target_img): tgp.write_raster(path, target_img, gt, self.proj, self.gdaldtype, self.no_data_value)
def to_file(self, fp): """Save the file. It is recommended to save the file using tif format, that is use '.tif' as its extension. Parameters ---------- fp: str File path. """ tgp.write_raster(fp, self.data, self.geo_transform, self.projection, self.gdaldtype, self.no_data_value, self.metadata)
def write_combined_tif(self, X, dst_tif_path, gdaldtype=None, no_data_value=None): """Combine the model predict result on splitted images and write as tif file. Parameters ---------- X: array_like The splitted image prediction result. should have the same shape with the SplittedImage.get_splitted_images. dst_tif_path: str The location to save the tif file. gdaldtype: int, optional The type of the cell defined in gdal which will affect the information to be stored when saving the file. This can be generate from `gdal.GDT_XXX` such as `gdal.GDT_Int32` equals 5 and `gdal.GDT_Float32` equals 6. no_data_value: int or float, optional Define which value to replace nan in numpy array when saving a raster file. """ X_combined_bands = self.get_combined_image(X) gdaldtype = gdaldtype if gdaldtype is not None else self.gdaldtype no_data_value = no_data_value if no_data_value is not None else self.no_data_value tgp.write_raster(dst_tif_path, X_combined_bands, geo_transform=self.src_gt, projection=self.proj, gdaldtype=gdaldtype, no_data_value=no_data_value)