Processing DEMs with GDAL in Python. Alexis Outdoors September, 2020. 00000
Paper abstract bibtex In this tutorial I explain various approaches to calculating terrain attributes such as the slope, aspect and hillshade of a digital elevation model (DEM) in Python. The different options are: 1) Run gdaldem with os.system() or subprocess.call() https://gdal.org/programs/gdaldem.html 2) Use the GDAL/OGR Python API and DEMProcessing() https://gdal.org/python/ 3) Use richdem.TerrainAttribute() https://richdem.readthedocs.io/en/lat... Code: from osgeo import gdal import subprocess import os import richdem as rd import matplotlib.pyplot as plt # using os, subprocess cmd = "gdaldem slope -compute_edges dem.tif slope1.tif" # replace slope with aspect or hillshade os.system(cmd) subprocess.check_call(cmd.split()) slp1 = gdal.Open("slope1.tif") slp1Array = slp1.GetRasterBand(1).ReadAsArray() # using DEMProcessing dem = gdal.Open("dem.tif") slp2 = gdal.DEMProcessing("slope2.tif", dem, "slope", computeEdges=True) # replace "slope" with "aspect" or "hillshade" slp2Array = slp2.GetRasterBand(1).ReadAsArray() # close your datasets! slp1 = slp2 = dem = None # using richdem dem = rd.LoadGDAL("dem.tif") slp3 = rd.TerrainAttribute(dem, attrib="slope_degrees") # replace "slope_degrees" with "slope_riserun", "aspect" ... rd.SaveGDAL("slope3.tif", slp3) # visualize (example) plt.figure() plt.imshow(slp2Array) plt.colorbar() plt.show()
@misc{alexis_outdoors_processing_2020,
title = {Processing {DEMs} with {GDAL} in {Python}},
url = {https://www.youtube.com/watch?v=5dDZeEXws9Q},
abstract = {In this tutorial I explain various approaches to calculating terrain attributes such as the slope, aspect and hillshade of a digital elevation model (DEM) in Python. The different options are:
1) Run gdaldem with os.system() or subprocess.call()
https://gdal.org/programs/gdaldem.html
2) Use the GDAL/OGR Python API and DEMProcessing()
https://gdal.org/python/
3) Use richdem.TerrainAttribute()
https://richdem.readthedocs.io/en/lat...
Code:
from osgeo import gdal
import subprocess
import os
import richdem as rd
import matplotlib.pyplot as plt
\# using os, subprocess
cmd = "gdaldem slope -compute\_edges dem.tif slope1.tif" \# replace slope with aspect or hillshade
os.system(cmd)
subprocess.check\_call(cmd.split())
slp1 = gdal.Open("slope1.tif")
slp1Array = slp1.GetRasterBand(1).ReadAsArray()
\# using DEMProcessing
dem = gdal.Open("dem.tif")
slp2 = gdal.DEMProcessing("slope2.tif", dem, "slope", computeEdges=True) \# replace "slope" with "aspect" or "hillshade"
slp2Array = slp2.GetRasterBand(1).ReadAsArray()
\# close your datasets!
slp1 = slp2 = dem = None
\# using richdem
dem = rd.LoadGDAL("dem.tif")
slp3 = rd.TerrainAttribute(dem, attrib="slope\_degrees") \# replace "slope\_degrees" with "slope\_riserun", "aspect" ...
rd.SaveGDAL("slope3.tif", slp3)
\# visualize (example)
plt.figure()
plt.imshow(slp2Array)
plt.colorbar()
plt.show()},
urldate = {2020-12-22},
author = {{Alexis Outdoors}},
month = sep,
year = {2020},
note = {00000},
}
Downloads: 0
{"_id":"875MLy4CwPHY5CbCK","bibbaseid":"alexisoutdoors-processingdemswithgdalinpython-2020","author_short":["Alexis Outdoors"],"bibdata":{"bibtype":"misc","type":"misc","title":"Processing DEMs with GDAL in Python","url":"https://www.youtube.com/watch?v=5dDZeEXws9Q","abstract":"In this tutorial I explain various approaches to calculating terrain attributes such as the slope, aspect and hillshade of a digital elevation model (DEM) in Python. The different options are: 1) Run gdaldem with os.system() or subprocess.call() https://gdal.org/programs/gdaldem.html 2) Use the GDAL/OGR Python API and DEMProcessing() https://gdal.org/python/ 3) Use richdem.TerrainAttribute() https://richdem.readthedocs.io/en/lat... Code: from osgeo import gdal import subprocess import os import richdem as rd import matplotlib.pyplot as plt # using os, subprocess cmd = \"gdaldem slope -compute_edges dem.tif slope1.tif\" # replace slope with aspect or hillshade os.system(cmd) subprocess.check_call(cmd.split()) slp1 = gdal.Open(\"slope1.tif\") slp1Array = slp1.GetRasterBand(1).ReadAsArray() # using DEMProcessing dem = gdal.Open(\"dem.tif\") slp2 = gdal.DEMProcessing(\"slope2.tif\", dem, \"slope\", computeEdges=True) # replace \"slope\" with \"aspect\" or \"hillshade\" slp2Array = slp2.GetRasterBand(1).ReadAsArray() # close your datasets! slp1 = slp2 = dem = None # using richdem dem = rd.LoadGDAL(\"dem.tif\") slp3 = rd.TerrainAttribute(dem, attrib=\"slope_degrees\") # replace \"slope_degrees\" with \"slope_riserun\", \"aspect\" ... rd.SaveGDAL(\"slope3.tif\", slp3) # visualize (example) plt.figure() plt.imshow(slp2Array) plt.colorbar() plt.show()","urldate":"2020-12-22","author":[{"firstnames":[],"propositions":[],"lastnames":["Alexis Outdoors"],"suffixes":[]}],"month":"September","year":"2020","note":"00000","bibtex":"@misc{alexis_outdoors_processing_2020,\n\ttitle = {Processing {DEMs} with {GDAL} in {Python}},\n\turl = {https://www.youtube.com/watch?v=5dDZeEXws9Q},\n\tabstract = {In this tutorial I explain various approaches to calculating terrain attributes such as the slope, aspect and hillshade of a digital elevation model (DEM) in Python. The different options are:\n1) Run gdaldem with os.system() or subprocess.call() \nhttps://gdal.org/programs/gdaldem.html\n2) Use the GDAL/OGR Python API and DEMProcessing()\nhttps://gdal.org/python/\n3) Use richdem.TerrainAttribute()\nhttps://richdem.readthedocs.io/en/lat...\n\nCode:\n\nfrom osgeo import gdal\nimport subprocess\nimport os\nimport richdem as rd\nimport matplotlib.pyplot as plt\n\n\\# using os, subprocess\ncmd = \"gdaldem slope -compute\\_edges dem.tif slope1.tif\" \\# replace slope with aspect or hillshade\nos.system(cmd)\nsubprocess.check\\_call(cmd.split())\nslp1 = gdal.Open(\"slope1.tif\")\nslp1Array = slp1.GetRasterBand(1).ReadAsArray()\n\n\\# using DEMProcessing\ndem = gdal.Open(\"dem.tif\")\nslp2 = gdal.DEMProcessing(\"slope2.tif\", dem, \"slope\", computeEdges=True) \\# replace \"slope\" with \"aspect\" or \"hillshade\"\nslp2Array = slp2.GetRasterBand(1).ReadAsArray()\n\n\\# close your datasets!\nslp1 = slp2 = dem = None\n\n\\# using richdem\ndem = rd.LoadGDAL(\"dem.tif\")\nslp3 = rd.TerrainAttribute(dem, attrib=\"slope\\_degrees\") \\# replace \"slope\\_degrees\" with \"slope\\_riserun\", \"aspect\" ...\nrd.SaveGDAL(\"slope3.tif\", slp3) \n\n\\# visualize (example)\nplt.figure()\nplt.imshow(slp2Array)\nplt.colorbar()\nplt.show()},\n\turldate = {2020-12-22},\n\tauthor = {{Alexis Outdoors}},\n\tmonth = sep,\n\tyear = {2020},\n\tnote = {00000},\n}\n\n","author_short":["Alexis Outdoors"],"key":"alexis_outdoors_processing_2020","id":"alexis_outdoors_processing_2020","bibbaseid":"alexisoutdoors-processingdemswithgdalinpython-2020","role":"author","urls":{"Paper":"https://www.youtube.com/watch?v=5dDZeEXws9Q"},"metadata":{"authorlinks":{}},"html":""},"bibtype":"misc","biburl":"https://bibbase.org/zotero/fortinm","dataSources":["TLSfqfvpu3sQJ89pA"],"keywords":[],"search_terms":["processing","dems","gdal","python","alexis outdoors"],"title":"Processing DEMs with GDAL in Python","year":2020}