我有形状为nx 5个numpy的阵列,NY如何在python中编写/创建GeoTIFF RGB图像文件?
lons.shape = (nx,ny)
lats.shape = (nx,ny)
reds.shape = (nx,ny)
greens.shape = (nx,ny)
blues.shape = (nx,ny)
的红色,绿色和蓝色阵列包含从0-255范围和纬度/经度阵列是纬度值/经度像素坐标。
我的问题是如何将这些数据写入geotiff?
我最终想用底图绘制图像。
这是我到目前为止的代码,但是我得到一个巨大的GeoTIFF文件(〜500MB),它出现空白(只是一个黑色的图像)。还要注意的是NX,NY = 8120,5416
from osgeo import gdal
from osgeo import osr
import numpy as np
import h5py
import os
os.environ['GDAL_DATA'] = "/Users/andyprata/Library/Enthought/Canopy_64bit/User/share/gdal"
# read in data
input_path = '/Users/andyprata/Desktop/modisRGB/'
with h5py.File(input_path+'red.h5', "r") as f:
red = f['red'].value
lon = f['lons'].value
lat = f['lats'].value
with h5py.File(input_path+'green.h5', "r") as f:
green = f['green'].value
with h5py.File(input_path+'blue.h5', "r") as f:
blue = f['blue'].value
# convert rgbs to uint8
r = red.astype('uint8')
g = green.astype('uint8')
b = blue.astype('uint8')
# set geotransform
nx = red.shape[0]
ny = red.shape[1]
xmin, ymin, xmax, ymax = [lon.min(), lat.min(), lon.max(), lat.max()]
xres = (xmax - xmin)/float(nx)
yres = (ymax - ymin)/float(ny)
geotransform = (xmin, xres, 0, ymax, 0, -yres)
# create the 3-band raster file
dst_ds = gdal.GetDriverByName('GTiff').Create('myGeoTIFF.tif', ny, nx, 3, gdal.GDT_Float32)
dst_ds.SetGeoTransform(geotransform) # specify coords
srs = osr.SpatialReference() # establish encoding
srs.ImportFromEPSG(3857) # WGS84 lat/long
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(r) # write r-band to the raster
dst_ds.GetRasterBand(2).WriteArray(g) # write g-band to the raster
dst_ds.GetRasterBand(3).WriteArray(b) # write b-band to the raster
dst_ds.FlushCache() # write to disk
dst_ds = None # save, close
您可能希望通过谷歌搜索'的GeoTIFF python' – cel
@cel我补充说,我用此刻的代码开始。希望能让我更清楚自己要出错的地方。 – Andy