我有形狀爲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