示例#1
0
    outX = np.array((X[:, band1] - X[:, band2])).astype(np.int16)
    return outX


#################################################################
# We can add keyword argument in the addFunction.
# This function is going to substract band2 from band 1
import time

t = time.time()
rM = RasterMath(raster)
rM.add_function(sub,
                out_image='/tmp/sub.tif',
                band1=1,
                band2=0,
                compress='high')

#####################
# Run the script

rM.run()
print(time.time() - t)
#######################
# Plot result

from osgeo import gdal
from matplotlib import pyplot as plt

src = gdal.Open('/tmp/sub.tif')
plt.imshow(src.ReadAsArray())
示例#2
0
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()
    
    rM.run(2)
#    rM.run()
    
    print(time.time()-t)

from osgeo import gdal
dst = gdal.Open('/tmp/x_flatten_True.tif')
arr = dst.GetRasterBand(1).ReadAsArray()
plt.imshow(np.ma.masked_where(arr == np.min(arr), arr))