def sub_n_reproj(input_mat, kwargs_sub, sub_window, output_crs): # Get the georeference and bounding parameters of subset image kwargs_sub.update({ 'height': sub_window.height, 'width': sub_window.width, 'transform': rasterio.windows.transform(sub_window, kwargs_sub['transform'])}) # Generate subset rasterio dataset input_ds_subset = MemoryFile().open(**kwargs_sub) input_mat = np.expand_dims(input_mat, axis=0) input_ds_subset.write(input_mat) # Reproject a dataset transform_reproj, width_reproj, height_reproj = \ calculate_default_transform(input_ds_subset.crs, output_crs, input_ds_subset.width, input_ds_subset.height, *input_ds_subset.bounds) kwargs_reproj = input_ds_subset.meta.copy() kwargs_reproj.update({ 'crs': output_crs, 'transform': transform_reproj, 'width': width_reproj, 'height': height_reproj }) output_ds = MemoryFile().open(**kwargs_reproj) reproject(source=rasterio.band(input_ds_subset, 1), destination=rasterio.band(output_ds, 1), src_transform=input_ds_subset.transform, src_crs=input_ds_subset.crs, dst_transform=transform_reproj, dst_crs=output_crs, resampling=Resampling.nearest) return output_ds
def read_tif_channels(tif, channel_index): if not isinstance(channel_index, list): channel_index = list(channel_index) if channel_index[ 0] == 0: # rasterio indexes channels starting from 1 not 0... channel_index = list(np.array(channel_index) + 1) profile = tif.profile profile.update({'count': len(channel_index)}) memory_file = MemoryFile().open(**profile) memory_file.write(tif.read(channel_index)) return memory_file
def mosaic(directory): file_paths = glob(directory + "/*.tif") tif_list = [] for path in file_paths: tif = rasterio.open(path) tif_list.append(resample_tif(tif, tif.transform[0] / rs.ground_truth.data.resolution)) mosaic_tif, mosaic_transform = merge(tif_list) profile = tif_list[0].profile profile.update({'width': mosaic_tif.shape[-1], 'height': mosaic_tif.shape[-2], 'transform': mosaic_transform}) memory_file = MemoryFile().open(**profile) memory_file.write(mosaic_tif) return memory_file
def resample_tif(tif, scale_factor, mode=Resampling.bilinear): data = tif.read(out_shape=(tif.count, int(tif.height * scale_factor), int(tif.width * scale_factor)), resampling=mode) # scale image transform transform = tif.transform * tif.transform.scale( (tif.width / data.shape[-1]), (tif.height / data.shape[-2])) profile = tif.profile profile.update({ 'width': int(tif.width * scale_factor), 'height': int(tif.height * scale_factor), 'transform': transform }) memory_file = MemoryFile().open(**profile) memory_file.write(data) return memory_file
kwargs = smap_sm_1km_ds.meta.copy() kwargs.update({ 'height': window.height, 'width': window.width, 'count': 1, 'transform': rasterio.windows.transform(window, smap_sm_1km_ds.transform) }) smap_sm_1km_subset = MemoryFile().open(**kwargs) mat_sub = smap_sm_1km_ds.read(1, window=window) mat_sub = np.expand_dims(mat_sub, axis=0) smap_sm_1km_subset.write(mat_sub) # Define the projection as WGS84 # transform = from_origin(shp_dan_extent[0], shp_dan_extent[3], cellsize_1km, cellsize_1km) # reproj_data = MemoryFile().open('GTiff', width=smap_sm_1km.shape[1], height=smap_sm_1km.shape[0], # dtype=str(smap_sm_1km.dtype), count=1, crs='EPSG:4326', transform=transform) # reproj_data.write(smap_sm_1km, 1) # reproj_out = rasterio.open('/Users/binfang/Downloads/raster.tif', 'w', 'GTiff', # width=(lon_1km_dan[-1] - lon_1km_dan[0])//cellsize_1km, # height=(lat_1km_dan[0] - lat_1km_dan[-1])//cellsize_1km, # dtype=str(smap_sm_1km.dtype), count=1, crs='EPSG:4326', transform=transform) # reproj_out.write(smap_sm_1km, 1) # reproj_out.close()