SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Writing raster dataset
  2. def write_tif(path_tif, array, geotransform, geoprojection):
  3.    
  4.     # Checking the dimension of array.
  5.     dim_array = array.shape
  6.     if len(dim_array) > 2:
  7.         depth = dim_array[2]
  8.     else:
  9.         depth = 1
  10.    
  11.     # Initializing TIF dataset driver
  12.     driver = gdal.GetDriverByName("GTiff")
  13.    
  14.     # Creating actual dataset from TIF drivers.
  15.     outdata = driver.Create(
  16.         path_tif, size[0], size[1], depth, gdal.GDT_Float32)
  17.    
  18.  
  19.     # sets same geotransform as input
  20.     outdata.SetGeoTransform(geotransform)
  21.    
  22.     # sets same projection as input
  23.     outdata.SetProjection(geoprojection)  
  24.    
  25.     # Writing data to array
  26.     for i in range(depth):
  27.         try:
  28.             arr = array[:, :, i]
  29.         except:
  30.             arr = array[:, :]
  31.         outdata.GetRasterBand(i+1).WriteArray(arr)
  32.    
  33.     # If you want these values transparent
  34.     outdata.GetRasterBand(1).SetNoDataValue(-9999)
  35.    
  36.     # saves to disk!!
  37.     outdata.FlushCache()
  38.    
  39.     # Releasing memory
  40.     outdata = None
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top