gdal 空間參考(投影)操作 - 柵格

獲取已有柵格文件的空間信息(geo_transform, projection)

import gdal, osr

rasterfn = 'rasterfn.tif'
newRasterfn = 'newRasterfn.tif'

raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()  # geotransform
projection= raster.GetProjectionRef()  # projection
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = array.shape[1]
rows = array.shape[0]
array = raster.ReadAsArray(1)

創建新文件,並賦值已有空間參考信息

driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Byte)
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outband = outRaster.GetRasterBand(1)
outband.WriteArray(array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromWkt(projection)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outband.FlushCache()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章