Python+GDAL 影像重采样

脚本的目的是将影像重采样到与参考影像同一大小,配合上篇博文使用更佳。


# -*- coding: utf-8 -*-
"""
Created on Sun Jul 21 11:22:59 2019

@author: Minty
"""
from osgeo import gdal, gdalconst
#import os
import sys

args = sys.argv
if len(args)!=4:
    print("Invalid parameters.")
    print("Please input:")
    print("Input path of raster to be resampled")
    print("Input path of reference raster")
    print("Output path of resampled raster")
    sys.exit(1)

print ('参数个数为:', len(args), '个参数。')
print ('参数列表:', str(args))

inputfile = args[1]
referencefile = args[2]
outputfile = args[3]


inputras = gdal.Open(inputfile,gdal.GA_ReadOnly)
inputProj = inputras.GetProjection()
inputTrans = inputras.GetGeoTransform()

reference = gdal.Open(referencefile,gdal.GA_ReadOnly)
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
bandreference = reference.GetRasterBand(1)    
x = reference.RasterXSize 
y = reference.RasterYSize
nbands = reference.RasterCount

driver= gdal.GetDriverByName('GTiff')
output = driver.Create(outputfile,x,y,nbands,bandreference.DataType)
output.SetGeoTransform(referenceTrans)
output.SetProjection(referenceProj)

gdal.ReprojectImage(inputras,output,inputProj,referenceProj,gdalconst.GRA_Bilinear)

del output

 

你可能感兴趣的:(GDAL,遥感图像)