Read and write raster files with GDAL in Python. Alexis Outdoors August, 2020.
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 \> = 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
{"_id":"EFnS7rwBagqZgDFof","bibbaseid":"alexisoutdoors-readandwriterasterfileswithgdalinpython-2020","author_short":["Alexis Outdoors"],"bibdata":{"bibtype":"misc","type":"misc","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 \\&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":[{"firstnames":[],"propositions":[],"lastnames":["Alexis Outdoors"],"suffixes":[]}],"month":"August","year":"2020","bibtex":"@misc{alexis_outdoors_read_2020,\n\ttitle = {Read and write raster files with {GDAL} in {Python}},\n\turl = {https://www.youtube.com/watch?v=p_BsFdV_LUk},\n\tabstract = {This tutorial explains how to read raster data as an array and save arrays as a GeoTiff file using the GDAL library in Python. \n\nGDAL/OGR Python API: https://gdal.org/python/\n\nCode: \n\nfrom osgeo import gdal\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n\\# import \nds = gdal.Open(\"dem.tif\")\ngt = ds.GetGeoTransform()\nproj = ds.GetProjection()\n\nband = ds.GetRasterBand(1)\narray = band.ReadAsArray()\n\nplt.figure()\nplt.imshow(array)\n\n\\# manipulate\nbinmask = np.where((array {\\textbackslash}\\&gt = np.mean(array)),1,0)\nplt.figure()\nplt.imshow(binmask)\n\n\\# export\ndriver = gdal.GetDriverByName(\"GTiff\")\ndriver.Register()\noutds = driver.Create(\"binmask.tif\", xsize = binmask.shape[1],\n ysize = binmask.shape[0], bands = 1, \n eType = gdal.GDT\\_Int16)\noutds.SetGeoTransform(gt)\noutds.SetProjection(proj)\noutband = outds.GetRasterBand(1)\noutband.WriteArray(binmask)\noutband.SetNoDataValue(np.nan)\noutband.FlushCache()\n\n\\# close your datasets and bands!!!\noutband = None\noutds = None},\n\turldate = {2020-12-22},\n\tauthor = {{Alexis Outdoors}},\n\tmonth = aug,\n\tyear = {2020},\n}\n\n","author_short":["Alexis Outdoors"],"key":"alexis_outdoors_read_2020","id":"alexis_outdoors_read_2020","bibbaseid":"alexisoutdoors-readandwriterasterfileswithgdalinpython-2020","role":"author","urls":{"Paper":"https://www.youtube.com/watch?v=p_BsFdV_LUk"},"metadata":{"authorlinks":{}},"html":""},"bibtype":"misc","biburl":"https://bibbase.org/zotero/fortinm","dataSources":["TLSfqfvpu3sQJ89pA"],"keywords":[],"search_terms":["read","write","raster","files","gdal","python","alexis outdoors"],"title":"Read and write raster files with GDAL in Python","year":2020}