如何用python编写/创建GeoTIFF RGB图像文件?

如何用python编写/创建GeoTIFF RGB图像文件?,python,numpy,rgb,gdal,geotiff,Python,Numpy,Rgb,Gdal,Geotiff,我有5个nx,ny形状的numpy数组 lons.shape = (nx,ny) lats.shape = (nx,ny) reds.shape = (nx,ny) greens.shape = (nx,ny) blues.shape = (nx,ny) 红色、绿色和蓝色数组包含范围为0–255的值,而lat/lon数组是纬度/经度像素坐标 我的问题是如何将这些数据写入geotiff 我最终希望使用basemap绘制图像 这是我到目前为止的代码,但是我得到了一个巨大的GeoTIFF文件(~50

我有5个nx,ny形状的numpy数组

lons.shape = (nx,ny)
lats.shape = (nx,ny)
reds.shape = (nx,ny)
greens.shape = (nx,ny)
blues.shape = (nx,ny)
红色、绿色和蓝色数组包含范围为0–255的值,而lat/lon数组是纬度/经度像素坐标

我的问题是如何将这些数据写入geotiff

我最终希望使用basemap绘制图像

这是我到目前为止的代码,但是我得到了一个巨大的GeoTIFF文件(~500MB),它是空白的(只有一个黑色图像)。还要注意,nx,ny=81205416

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

我认为问题在于,当您创建数据集时,您将其传递给GDT_Float32。对于像素范围为0-255的标准图像,需要GDT_字节。浮点值通常要求值介于0-1之间

我获取了您的代码并随机生成了一些数据,以便测试您的API的其余部分。然后我在太浩湖周围创建了一些虚拟坐标

这是代码

#!/usr/bin/env python
from osgeo import gdal
from osgeo import osr
import numpy as np
import os, sys

#  Initialize the Image Size
image_size = (400,400)

#  Choose some Geographic Transform (Around Lake Tahoe)
lat = [39,38.5]
lon = [-120,-119.5]

#  Create Each Channel
r_pixels = np.zeros((image_size), dtype=np.uint8)
g_pixels = np.zeros((image_size), dtype=np.uint8)
b_pixels = np.zeros((image_size), dtype=np.uint8)

#  Set the Pixel Data (Create some boxes)
for x in range(0,image_size[0]):
    for y in range(0,image_size[1]):
        if x < image_size[0]/2 and y < image_size[1]/2:
            r_pixels[y,x] = 255
        elif x >= image_size[0]/2 and y < image_size[1]/2:
            g_pixels[y,x] = 255
        elif x < image_size[0]/2 and y >= image_size[1]/2:
            b_pixels[y,x] = 255
        else:
            r_pixels[y,x] = 255
            g_pixels[y,x] = 255
            b_pixels[y,x] = 255

# set geotransform
nx = image_size[0]
ny = image_size[1]
xmin, ymin, xmax, ymax = [min(lon), min(lat), max(lon), max(lat)]
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_Byte)

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_pixels)   # write r-band to the raster
dst_ds.GetRasterBand(2).WriteArray(g_pixels)   # write g-band to the raster
dst_ds.GetRasterBand(3).WriteArray(b_pixels)   # write b-band to the raster
dst_ds.FlushCache()                     # write to disk
dst_ds = None
#/usr/bin/env python
从osgeo导入gdal
从osgeo导入osr
将numpy作为np导入
导入操作系统,系统
#初始化图像大小
图像大小=(400400)
#选择一些地理变换(围绕太浩湖)
纬度=[39,38.5]
lon=[-120,-119.5]
#创建每个通道
r_像素=np.0((图像大小),dtype=np.uint8)
g_像素=np.0((图像大小),dtype=np.uint8)
b_像素=np.0((图像大小),数据类型=np.uint8)
#设置像素数据(创建一些框)
对于范围内的x(0,图像大小[0]):
对于范围内的y(0,图像大小[1]):
如果x=图像大小[0]/2和y<图像大小[1]/2:
g_像素[y,x]=255
如果x=image\u size[1]/2:
b_像素[y,x]=255
其他:
r_像素[y,x]=255
g_像素[y,x]=255
b_像素[y,x]=255
#集合变换
nx=图像大小[0]
ny=图像大小[1]
xmin,ymin,xmax,ymax=[min(lon),min(lat),max(lon),max(lat)]
xres=(xmax-xmin)/float(nx)
年=(ymax-ymin)/浮动(纽约)
geotransform=(xmin,xres,0,ymax,0,-yres)
#创建3波段光栅文件
dst_ds=gdal.GetDriverByName('GTiff').Create('myGeoTIFF.tif',ny,nx,3,gdal.GDT_字节)
dst_ds.SetGeoTransform(geotransform)#指定坐标
srs=osr.SpatialReference()#建立编码
srs.进口Romepsg(3857)#WGS84横向/纵向
dst_ds.SetProjection(srs.ExportToWkt())#将坐标导出到文件
dst_ds.GetRasterBand(1).WriteArray(r_像素)#将r波段写入光栅
dst_ds.GetRasterBand(2).WriteArray(g_像素)#将g波段写入光栅
dst_ds.GetRasterBand(3).WriteArray(b_像素)#将b波段写入光栅
dst_ds.FlushCache()#写入磁盘
dst_ds=无
这是输出。(注意:目标是生成颜色,而不是地形!)

这是QGIS中的图像,用于验证投影


你可以先通过谷歌搜索
geotiff-python
@cel,我已经添加了我目前正在使用的代码。希望这能更清楚地说明我的错误所在。回答得很好。在上面的代码中,我刚刚用GDT_Float32替换了GDT_字节(正如您所建议的),它工作得非常好。谢谢我正在尝试一些非常类似的东西,但是当我保存
GTiff
时,它是黑白的,你知道为什么吗?这是我的代码:如何将tiff放在QGIS上的地图上方?在QGIS 2.18中,您应该能够直接添加图层->添加图层->添加光栅图层(或Ctrl+Shift+R),必须更改
srs.ImportFromEPSG(3857)
srs.ImportFromEPSG(4326)
才能工作。