# Initialize rasterMath with raster # ------------------------------------ # Set return_3d to True to have full block size (not one pixel per row) # Create raster mask to only keep pixel inside polygons. image_mask_from_vector(vector, raster, '/tmp/mask.tif', invert=True) import time t0 = time.time() for return_3d in [True, False]: rM = RasterMath(raster, in_image_mask='/tmp/mask.tif', return_3d=return_3d) rM.custom_block_size(128, 128) # block of 128x128 x = rM.get_block() # Returns with only 1 dimension returnFlatten = lambda x: x[..., 0] # Returns 3x the original last dimension addOneBand = lambda x: np.repeat(x, 3, axis=x.ndim - 1) # Add functions to rasterMath rM.add_function(addOneBand, '/tmp/x_repeat_{}.tif'.format(str(return_3d))) rM.add_function(returnFlatten, '/tmp/x_flatten_{}.tif'.format(str(return_3d))) rM.run() print(time.time() - t0)
############################################################################## # Initialize rasterMath with raster # ------------------------------------ # Set return_3d to True to have full block size (not one pixel per row) # Create raster mask to only keep pixel inside polygons. image_mask_from_vector(vector,raster,'/tmp/mask.tif',invert=True) import time for return_3d in [True,False]: rM = RasterMath(raster,in_image_mask='/tmp/mask.tif',return_3d=return_3d) rM.custom_block_size(10,10) # block of 128x128 # print(rM.get_random_block().shape) x = rM.get_block() # Returns with only 1 dimension returnFlatten = lambda x : x[...,0] # Returns 3x the original last dimension addOneBand = lambda x : np.repeat(x,3,axis=x.ndim-1) # Add functions to rasterMath rM.add_function(addOneBand,'/tmp/x_repeat_{}.tif'.format(str(return_3d))) rM.add_function(returnFlatten,'/tmp/x_flatten_{}.tif'.format(str(return_3d))) t=time.time()
# ------------------------------------ # Set return3d to True to have full block size (not one pixel per row) rM = RasterMath(raster, return_3d=True) print(rM.get_random_block().shape) ############################################################################## # Comparing different block size (%, fixed, full block) # ------------------------------------------------------- ####################### # You can define block by percentage of the whole width/height rM.custom_block_size(1 / 2, 1 / 2) print(rM.get_random_block().shape) ####################### # Or by fixed window rM.custom_block_size(50, 100) # width divided every 50 pixel and height every 100 print(rM.get_random_block().shape) ######################## # To have the full image (one block) rM.custom_block_size(-1, -1) # to have the full image ########################