Reproject, resample and clip raster data with GDAL in Python. Alexis Outdoors September, 2020. 00000
Paper abstract bibtex In this tutorial, I explain how to use gdalwarp in Python to reproject raster data to a different coordinate reference system, change the resolution (resample) and clip it to a shapefile. GDAL/OGR Python API: https://gdal.org/python/ Code: from osgeo import gdal import numpy as np import matplotlib.pyplot as plt ds = gdal.Open("dem.tif") # reproject dsReprj = gdal.Warp("demReprj.tif", ds, dstSRS = "EPSG:4326") # resample dsRes = gdal.Warp("demRes.tif", ds, xRes = 150, yRes = 150, resampleAlg = "bilinear") # clip # make sure your raster data and shapefile have the same projection! dsClip = gdal.Warp("demClip.tif", ds, cutlineDSName = "star.shp", cropToCutline = True, dstNodata = np.nan) # visualize array = dsClip.GetRasterBand(1).ReadAsArray() plt.figure() plt.imshow(array) plt.colorbar() plt.show() # close your datasets! ds = dsClip = dsRes = dsReprj = None
@misc{alexis_outdoors_reproject_2020,
title = {Reproject, resample and clip raster data with {GDAL} in {Python}},
url = {https://www.youtube.com/watch?v=1jHhQKJOQ5M},
abstract = {In this tutorial, I explain how to use gdalwarp in Python to reproject raster data to a different coordinate reference system, change the resolution (resample) and clip it to a shapefile.
GDAL/OGR Python API: https://gdal.org/python/
Code:
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt
ds = gdal.Open("dem.tif")
\# reproject
dsReprj = gdal.Warp("demReprj.tif", ds, dstSRS = "EPSG:4326")
\# resample
dsRes = gdal.Warp("demRes.tif", ds, xRes = 150, yRes = 150,
resampleAlg = "bilinear")
\# clip
\# make sure your raster data and shapefile have the same projection!
dsClip = gdal.Warp("demClip.tif", ds, cutlineDSName = "star.shp",
cropToCutline = True, dstNodata = np.nan)
\# visualize
array = dsClip.GetRasterBand(1).ReadAsArray()
plt.figure()
plt.imshow(array)
plt.colorbar()
plt.show()
\# close your datasets!
ds = dsClip = dsRes = dsReprj = None},
urldate = {2020-12-22},
author = {{Alexis Outdoors}},
month = sep,
year = {2020},
note = {00000},
}
Downloads: 0
{"_id":"DjXACwg72BgGvT3dY","bibbaseid":"alexisoutdoors-reprojectresampleandcliprasterdatawithgdalinpython-2020","author_short":["Alexis Outdoors"],"bibdata":{"bibtype":"misc","type":"misc","title":"Reproject, resample and clip raster data with GDAL in Python","url":"https://www.youtube.com/watch?v=1jHhQKJOQ5M","abstract":"In this tutorial, I explain how to use gdalwarp in Python to reproject raster data to a different coordinate reference system, change the resolution (resample) and clip it to a shapefile. GDAL/OGR Python API: https://gdal.org/python/ Code: from osgeo import gdal import numpy as np import matplotlib.pyplot as plt ds = gdal.Open(\"dem.tif\") # reproject dsReprj = gdal.Warp(\"demReprj.tif\", ds, dstSRS = \"EPSG:4326\") # resample dsRes = gdal.Warp(\"demRes.tif\", ds, xRes = 150, yRes = 150, resampleAlg = \"bilinear\") # clip # make sure your raster data and shapefile have the same projection! dsClip = gdal.Warp(\"demClip.tif\", ds, cutlineDSName = \"star.shp\", cropToCutline = True, dstNodata = np.nan) # visualize array = dsClip.GetRasterBand(1).ReadAsArray() plt.figure() plt.imshow(array) plt.colorbar() plt.show() # close your datasets! ds = dsClip = dsRes = dsReprj = None","urldate":"2020-12-22","author":[{"firstnames":[],"propositions":[],"lastnames":["Alexis Outdoors"],"suffixes":[]}],"month":"September","year":"2020","note":"00000","bibtex":"@misc{alexis_outdoors_reproject_2020,\n\ttitle = {Reproject, resample and clip raster data with {GDAL} in {Python}},\n\turl = {https://www.youtube.com/watch?v=1jHhQKJOQ5M},\n\tabstract = {In this tutorial, I explain how to use gdalwarp in Python to reproject raster data to a different coordinate reference system, change the resolution (resample) and clip it to a shapefile. \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\nds = gdal.Open(\"dem.tif\")\n\n\\# reproject\ndsReprj = gdal.Warp(\"demReprj.tif\", ds, dstSRS = \"EPSG:4326\")\n\\# resample\ndsRes = gdal.Warp(\"demRes.tif\", ds, xRes = 150, yRes = 150, \n resampleAlg = \"bilinear\")\n\\# clip \n\\# make sure your raster data and shapefile have the same projection!\ndsClip = gdal.Warp(\"demClip.tif\", ds, cutlineDSName = \"star.shp\",\n cropToCutline = True, dstNodata = np.nan)\n\n\\# visualize\narray = dsClip.GetRasterBand(1).ReadAsArray()\nplt.figure()\nplt.imshow(array)\nplt.colorbar()\nplt.show()\n\n\\# close your datasets!\nds = dsClip = dsRes = dsReprj = None},\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_reproject_2020","id":"alexis_outdoors_reproject_2020","bibbaseid":"alexisoutdoors-reprojectresampleandcliprasterdatawithgdalinpython-2020","role":"author","urls":{"Paper":"https://www.youtube.com/watch?v=1jHhQKJOQ5M"},"metadata":{"authorlinks":{}},"html":""},"bibtype":"misc","biburl":"https://bibbase.org/zotero/fortinm","dataSources":["TLSfqfvpu3sQJ89pA"],"keywords":[],"search_terms":["reproject","resample","clip","raster","data","gdal","python","alexis outdoors"],"title":"Reproject, resample and clip raster data with GDAL in Python","year":2020}