def georeference_raster_tile(x, y, z, input_path, output_path, expandRgba): bounds = tile_edges(x, y, z) named_args = { "outputSRS": "EPSG:4326", "outputBounds": bounds, } if expandRgba: named_args["rgbExpand"] = "rgba" Translate( output_path, input_path, **named_args, )
def copy_vrt(in_fname, out_fname=None, bbox=None, verbose=True): """Create a VRT for (a subset of) a gdal-readable file bbox format: (left, bottom, right, top)""" from gdal import Translate if out_fname is None: out_fname = in_fname + ".vrt" # Using Translate... but would use Warp if every reprojecting if bbox: left, bottom, right, top = bbox projwin = (left, top, right, bottom ) # unclear why Translate does UL LR else: projwin = None if verbose: logger.info(f"Creating {out_fname}, subset bbox: {bbox}") Translate(out_fname, in_fname, projWin=projwin)
def _convert_to_tif( partial_coverage_tiles: List[PartialCoverageTile], wms_crs_code: str ) -> None: for tile in partial_coverage_tiles: if os.path.exists(tile.wms_path): logging.debug(f"Converting {tile.wms_path} to {tile.tif_path}") src_file = Open(tile.wms_path, GA_ReadOnly) Translate( tile.tif_path, src_file, format="GTiff", noData=None, outputSRS=wms_crs_code, # Translate expects bounds in format ulX, ulY, lrX, lrY so flip minY and maxY outputBounds=(tile.x_min, tile.y_max, tile.x_max, tile.y_min), ) if remove_intermediaries(): os.remove(tile.wms_path) else: logging.warn(f"Expected file {tile.wms_path} does not exist")
def resize_with_gdal(geo_dataset_path, resize_dim): dest_path = geo_dataset_path.parent / geo_dataset_path.name[:-1] geo_obj = gdal.Open(str(geo_dataset_path)) t_o = {"width": resize_dim[0], "height": resize_dim[1]} ds = Translate(str(dest_path), geo_obj, **t_o) ds = None
path_to_image.unlink() if __name__ == '__main__': dataset_name = "20210325175353219035" make_dataset(dataset_name) # not used in this stub but often useful for finding various files # find .env automagically by walking up directories until it's found, then # load up the .env entries as environment variables # refine_glc() # path_to_img = Path("..", "..", "data", "processed", "20210324225635924767", "images", "20210324225635924767_0.tiff") # my_img_array = gdal.Open(str(path_to_img)).ReadAsArray() # my_img_array = np.swapaxes(my_img_array, 0, 2).copy() # new_array = crop_centre(my_img_array, 256, 256) # print(my_img_array.shape, new_array.shape) # fig, axs = plt.subplots(ncols=2, nrows=1) # axs[0].imshow(my_img_array) # axs[1].imshow(new_array) # fig.show() path_to_img = Path("/home/matt/Dropbox/github/climate_hackathon/data/processed/20210325175353219035/masks/20210325175353219035_0.tiff") dest_path = path_to_img.parent / path_to_img.name[:-1] geo_obj = gdal.Open(str(path_to_img)) t_o = {"width": 256, "height": 256} ds = Translate(str(dest_path), geo_obj, **t_o) ds = None