Year:2021
ArcMap
Python
at National Taiwan University
Merge rasters by GDAL
at National Taiwan University
Year:2021
ArcMap
Python
Description
This is short python script for mergin multiple raster together.
If we need to merge multiple rasters together we normally use Mosaic To New Raster tool.
This function doesn't work well with a large number of datasets or if the final GeoTIFF is too large.
Therefore, it's better to use a python script to do the hard work for us.
eg. In Taiwan, the DEM raster comes in multiple grid files together with a shapefile which includes the grid filenames
First I used the selected area shapefile to clip the grid shapefile.
Second, I exported an attribute table, where each polygon has a grid field.
In the last step, I built this small script which is based on the osgeo.gdal library.
from osgeo import gdal
import pandas as pd
import os
#read csv table
files = pd.read_csv("table_from_shapefile.csv")
#take only grid column as it contains the name of files
filenames = files["grids"]
#make a list of filenames
demnames = []
for names in filenames:
name = "Grid/"+str(names)+"DEM.tif"
#check if file exists, if so, append to list
if os.path.exists(name) == True:
demnames.append(name)
#copy demnames
files_to_mosaic = demnames
g = gdal.Warp("output.tif", files_to_mosaic, format="GTiff",
options=["COMPRESS=LZW", "TILED=YES"])
# Close file and flush to disk
g = None