Splitting raster data into equal pieces with GDAL in Python. Alexis Outdoors November, 2020. 00000
Paper abstract bibtex In this tutorial, I will guide you through the creation of a Python script that uses GDAL to split raster data into several equal parts. The key to this task is to find out the xmin, ymin, xmax, and ymax coordinates of the smaller tiles and then use gdal.Warp or gdal.Translate to adjust the output boundaries of the input raster data. Be aware that the output tiles might have a slightly different resolution. See 13:14 for ways to deal with this. GDAL/OGR Python API: https://gdal.org/python/ Code: from osgeo import gdal dem = gdal.Open("dem.tif") gt = dem.GetGeoTransform() # get coordinates of upper left corner xmin = gt[0] ymax = gt[3] res = gt[1] # determine total length of raster xlen = res * dem.RasterXSize ylen = res * dem.RasterYSize # number of tiles in x and y direction xdiv = 2 ydiv = 2 # size of a single tile xsize = xlen/xdiv ysize = ylen/ydiv # create lists of x and y coordinates xsteps = [xmin + xsize * i for i in range(xdiv+1)] ysteps = [ymax - ysize * i for i in range(ydiv+1)] # loop over min and max x and y coordinates for i in range(xdiv): for j in range(ydiv): xmin = xsteps[i] xmax = xsteps[i+1] ymax = ysteps[j] ymin = ysteps[j+1] print("xmin: "+str(xmin)) print("xmax: "+str(xmax)) print("ymin: "+str(ymin)) print("ymax: "+str(ymax)) print("\n") # use gdal warp gdal.Warp("dem"+str(i)+str(j)+".tif", dem, outputBounds = (xmin, ymin, xmax, ymax), dstNodata = -9999) # or gdal translate to subset the input raster gdal.Translate("dem_translate"+str(i)+str(j)+".tif", dem, projWin = (xmin, ymax, xmax, ymin), xRes = res, yRes = -res) # close the open dataset!!! dem = None
@misc{alexis_outdoors_splitting_2020,
title = {Splitting raster data into equal pieces with {GDAL} in {Python}},
url = {https://www.youtube.com/watch?v=H5uQ85VXttg},
abstract = {In this tutorial, I will guide you through the creation of a Python script that uses GDAL to split raster data into several equal parts. The key to this task is to find out the xmin, ymin, xmax, and ymax coordinates of the smaller tiles and then use gdal.Warp or gdal.Translate to adjust the output boundaries of the input raster data. Be aware that the output tiles might have a slightly different resolution. See 13:14 for ways to deal with this.
GDAL/OGR Python API: https://gdal.org/python/
Code:
from osgeo import gdal
dem = gdal.Open("dem.tif")
gt = dem.GetGeoTransform()
\# get coordinates of upper left corner
xmin = gt[0]
ymax = gt[3]
res = gt[1]
\# determine total length of raster
xlen = res * dem.RasterXSize
ylen = res * dem.RasterYSize
\# number of tiles in x and y direction
xdiv = 2
ydiv = 2
\# size of a single tile
xsize = xlen/xdiv
ysize = ylen/ydiv
\# create lists of x and y coordinates
xsteps = [xmin + xsize * i for i in range(xdiv+1)]
ysteps = [ymax - ysize * i for i in range(ydiv+1)]
\# loop over min and max x and y coordinates
for i in range(xdiv):
for j in range(ydiv):
xmin = xsteps[i]
xmax = xsteps[i+1]
ymax = ysteps[j]
ymin = ysteps[j+1]
print("xmin: "+str(xmin))
print("xmax: "+str(xmax))
print("ymin: "+str(ymin))
print("ymax: "+str(ymax))
print("{\textbackslash}n")
\# use gdal warp
gdal.Warp("dem"+str(i)+str(j)+".tif", dem,
outputBounds = (xmin, ymin, xmax, ymax), dstNodata = -9999)
\# or gdal translate to subset the input raster
gdal.Translate("dem\_translate"+str(i)+str(j)+".tif", dem, projWin = (xmin, ymax, xmax, ymin), xRes = res, yRes = -res)
\# close the open dataset!!!
dem = None},
urldate = {2020-12-22},
author = {{Alexis Outdoors}},
month = nov,
year = {2020},
note = {00000},
}
Downloads: 0
{"_id":"5sDmhSuTfnfjecjhZ","bibbaseid":"alexisoutdoors-splittingrasterdataintoequalpieceswithgdalinpython-2020","author_short":["Alexis Outdoors"],"bibdata":{"bibtype":"misc","type":"misc","title":"Splitting raster data into equal pieces with GDAL in Python","url":"https://www.youtube.com/watch?v=H5uQ85VXttg","abstract":"In this tutorial, I will guide you through the creation of a Python script that uses GDAL to split raster data into several equal parts. The key to this task is to find out the xmin, ymin, xmax, and ymax coordinates of the smaller tiles and then use gdal.Warp or gdal.Translate to adjust the output boundaries of the input raster data. Be aware that the output tiles might have a slightly different resolution. See 13:14 for ways to deal with this. GDAL/OGR Python API: https://gdal.org/python/ Code: from osgeo import gdal dem = gdal.Open(\"dem.tif\") gt = dem.GetGeoTransform() # get coordinates of upper left corner xmin = gt[0] ymax = gt[3] res = gt[1] # determine total length of raster xlen = res * dem.RasterXSize ylen = res * dem.RasterYSize # number of tiles in x and y direction xdiv = 2 ydiv = 2 # size of a single tile xsize = xlen/xdiv ysize = ylen/ydiv # create lists of x and y coordinates xsteps = [xmin + xsize * i for i in range(xdiv+1)] ysteps = [ymax - ysize * i for i in range(ydiv+1)] # loop over min and max x and y coordinates for i in range(xdiv): for j in range(ydiv): xmin = xsteps[i] xmax = xsteps[i+1] ymax = ysteps[j] ymin = ysteps[j+1] print(\"xmin: \"+str(xmin)) print(\"xmax: \"+str(xmax)) print(\"ymin: \"+str(ymin)) print(\"ymax: \"+str(ymax)) print(\"\\n\") # use gdal warp gdal.Warp(\"dem\"+str(i)+str(j)+\".tif\", dem, outputBounds = (xmin, ymin, xmax, ymax), dstNodata = -9999) # or gdal translate to subset the input raster gdal.Translate(\"dem_translate\"+str(i)+str(j)+\".tif\", dem, projWin = (xmin, ymax, xmax, ymin), xRes = res, yRes = -res) # close the open dataset!!! dem = None","urldate":"2020-12-22","author":[{"firstnames":[],"propositions":[],"lastnames":["Alexis Outdoors"],"suffixes":[]}],"month":"November","year":"2020","note":"00000","bibtex":"@misc{alexis_outdoors_splitting_2020,\n\ttitle = {Splitting raster data into equal pieces with {GDAL} in {Python}},\n\turl = {https://www.youtube.com/watch?v=H5uQ85VXttg},\n\tabstract = {In this tutorial, I will guide you through the creation of a Python script that uses GDAL to split raster data into several equal parts. The key to this task is to find out the xmin, ymin, xmax, and ymax coordinates of the smaller tiles and then use gdal.Warp or gdal.Translate to adjust the output boundaries of the input raster data. Be aware that the output tiles might have a slightly different resolution. See 13:14 for ways to deal with this. \n\nGDAL/OGR Python API: https://gdal.org/python/\n\nCode:\n\nfrom osgeo import gdal\n\ndem = gdal.Open(\"dem.tif\")\ngt = dem.GetGeoTransform()\n\n\\# get coordinates of upper left corner\nxmin = gt[0]\nymax = gt[3]\nres = gt[1]\n\n\\# determine total length of raster\nxlen = res * dem.RasterXSize\nylen = res * dem.RasterYSize\n\n\\# number of tiles in x and y direction\nxdiv = 2\nydiv = 2\n\n\\# size of a single tile\nxsize = xlen/xdiv\nysize = ylen/ydiv\n\n\\# create lists of x and y coordinates\nxsteps = [xmin + xsize * i for i in range(xdiv+1)]\nysteps = [ymax - ysize * i for i in range(ydiv+1)]\n\n\\# loop over min and max x and y coordinates\nfor i in range(xdiv):\n for j in range(ydiv):\n xmin = xsteps[i]\n xmax = xsteps[i+1]\n ymax = ysteps[j]\n ymin = ysteps[j+1]\n \n print(\"xmin: \"+str(xmin))\n print(\"xmax: \"+str(xmax))\n print(\"ymin: \"+str(ymin))\n print(\"ymax: \"+str(ymax))\n print(\"{\\textbackslash}n\")\n \n \\# use gdal warp\n gdal.Warp(\"dem\"+str(i)+str(j)+\".tif\", dem, \n outputBounds = (xmin, ymin, xmax, ymax), dstNodata = -9999)\n \\# or gdal translate to subset the input raster\n gdal.Translate(\"dem\\_translate\"+str(i)+str(j)+\".tif\", dem, projWin = (xmin, ymax, xmax, ymin), xRes = res, yRes = -res)\n \n\\# close the open dataset!!!\ndem = None},\n\turldate = {2020-12-22},\n\tauthor = {{Alexis Outdoors}},\n\tmonth = nov,\n\tyear = {2020},\n\tnote = {00000},\n}\n\n","author_short":["Alexis Outdoors"],"key":"alexis_outdoors_splitting_2020","id":"alexis_outdoors_splitting_2020","bibbaseid":"alexisoutdoors-splittingrasterdataintoequalpieceswithgdalinpython-2020","role":"author","urls":{"Paper":"https://www.youtube.com/watch?v=H5uQ85VXttg"},"metadata":{"authorlinks":{}},"html":""},"bibtype":"misc","biburl":"https://bibbase.org/zotero/fortinm","dataSources":["TLSfqfvpu3sQJ89pA"],"keywords":[],"search_terms":["splitting","raster","data","equal","pieces","gdal","python","alexis outdoors"],"title":"Splitting raster data into equal pieces with GDAL in Python","year":2020}