Read and write raster files with GDAL in Python. Alexis Outdoors August, 2020.
Read and write raster files with GDAL in Python [link]Paper  abstract   bibtex   
This tutorial explains how to read raster data as an array and save arrays as a GeoTiff file using the GDAL library in Python. GDAL/OGR Python API: https://gdal.org/python/ Code: from osgeo import gdal import numpy as np import matplotlib.pyplot as plt # import ds = gdal.Open("dem.tif") gt = ds.GetGeoTransform() proj = ds.GetProjection() band = ds.GetRasterBand(1) array = band.ReadAsArray() plt.figure() plt.imshow(array) # manipulate binmask = np.where((array \&gt = np.mean(array)),1,0) plt.figure() plt.imshow(binmask) # export driver = gdal.GetDriverByName("GTiff") driver.Register() outds = driver.Create("binmask.tif", xsize = binmask.shape[1], ysize = binmask.shape[0], bands = 1, eType = gdal.GDT_Int16) outds.SetGeoTransform(gt) outds.SetProjection(proj) outband = outds.GetRasterBand(1) outband.WriteArray(binmask) outband.SetNoDataValue(np.nan) outband.FlushCache() # close your datasets and bands!!! outband = None outds = None
@misc{alexis_outdoors_read_2020,
	title = {Read and write raster files with {GDAL} in {Python}},
	url = {https://www.youtube.com/watch?v=p_BsFdV_LUk},
	abstract = {This tutorial explains how to read raster data as an array and save arrays as a GeoTiff file using the GDAL library in Python. 

GDAL/OGR Python API: https://gdal.org/python/

Code: 

from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt

\# import 
ds = gdal.Open("dem.tif")
gt = ds.GetGeoTransform()
proj = ds.GetProjection()

band = ds.GetRasterBand(1)
array = band.ReadAsArray()

plt.figure()
plt.imshow(array)

\# manipulate
binmask = np.where((array {\textbackslash}\&gt = np.mean(array)),1,0)
plt.figure()
plt.imshow(binmask)

\# export
driver = gdal.GetDriverByName("GTiff")
driver.Register()
outds = driver.Create("binmask.tif", xsize = binmask.shape[1],
                      ysize = binmask.shape[0], bands = 1, 
                      eType = gdal.GDT\_Int16)
outds.SetGeoTransform(gt)
outds.SetProjection(proj)
outband = outds.GetRasterBand(1)
outband.WriteArray(binmask)
outband.SetNoDataValue(np.nan)
outband.FlushCache()

\# close your datasets and bands!!!
outband = None
outds = None},
	urldate = {2020-12-22},
	author = {{Alexis Outdoors}},
	month = aug,
	year = {2020},
}

Downloads: 0