Reproject, resample and clip raster data with GDAL in Python. Alexis Outdoors September, 2020. 00000
Reproject, resample and clip raster data with GDAL in Python [link]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