Simplest way to save array into raster file in Python
<p>With one 2-d array in the shape of (100, 100), I want to save it into raster file in <code>.tiff</code> format.  </p>

<p>I can use <code>gdal</code> package to read <code>tiff</code> files which are already exist. But I still can't find a simple way to <strong>transform the 2-d array into tiff file</strong>.  </p>

<p>Using <code>plt.imsave("xx.tif",array)</code> or  </p>

<pre><code>def array_to_raster(array):
    """Array &gt; Raster
    Save a raster from a C order array.
    :param array: ndarray
     """
    dst_filename = 'xxx.tiff'
    x_pixels = 100  # number of pixels in x
    y_pixels = 100  # number of pixels in y
    driver = gdal.GetDriverByName('GTiff')
    dataset = driver.Create(
           dst_filename,
           x_pixels,
           y_pixels,
           1,
           gdal.GDT_Float32, )
    dataset.GetRasterBand(1).WriteArray(array)
    dataset.FlushCache()  # Write to disk.
    return dataset, dataset.GetRasterBand(1)  
</code></pre>

<p>They all failed to achieve my target. The second method was adapted from <a href="https://gis.stackexchange.com/questions/58517/python-gdal-save-array-as-raster-with-projection-from-other-file">here</a> which can transform an array into a <code>geotiff</code> with a projection.  </p>

<p>Is there some simple way to save array into <code>.tiff</code>, so I can call it by import the <code>tiff</code> file next time.  </p>

<p>Any advices would be appreciate.</p>