赞
踩
以某一个文件为参考(参考它的左上角点位置、像元大小),创建一个新的TIFF文件。
from osgeo import gdal, osr import os import numpy as np def test(data, dataRef): ds = gdal.Open(dataRef) gt_ref = ds.GetGeoTransform() print(gt_ref) prj_ref = ds.GetProjection() print(prj_ref) srs = osr.SpatialReference(wkt=prj_ref) if srs.IsProjected: print(srs.GetAttrValue('projcs')) print(srs.GetAttrValue('geogcs')) data = np.random.rand(100, 100) # Create a random 100x100 raster DEM_size = np.shape(data) # Get size of raster dstfile = os.path.join(os.path.dirname(dataRef), 'result.tiff') # Output file path # Make geotransform xmin, ymax = (gt_ref[0], gt_ref[3]) nrows, ncols = DEM_size xres = gt_ref[1] yres = np.abs(gt_ref[5]) geotransform = (xmin, xres, 0, ymax, 0, -yres) # Write output driver = gdal.GetDriverByName('Gtiff') dataset = driver.Create(dstfile, ncols, nrows, 1, gdal.GDT_Float32) dataset.SetGeoTransform(geotransform) srs = osr.SpatialReference(wkt=prj_ref) dataset.SetProjection(srs.ExportToWkt()) dataset.GetRasterBand(1).WriteArray(data) dataset = None if __name__ == "__main__": landsat8_dir = 'D:/now/landsat8/LC08_L1TP_044032_20181108_20181116_01_T1/LC08_L1TP_044032_20181108_20181116_01_T1_B1.TIF' test('', landsat8_dir)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。