我正在尝试使用GDAL和Python
将ENVI文件读取为数组图片信息如下:
Driver: ENVI/ENVI .hdr Labelled
Files: IMG-VV-ALPSRP248213250-P1.1__D_pwr_geo_sigma
IMG-VV-ALPSRP248213250-P1.1__D_pwr_geo_sigma.hdr
Size is 1659, 2775
Coordinate System is:
PROJCS["UTM Zone 16, Northern Hemisphere",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
TOWGS84[0,0,0,0,0,0,0],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9108"]],
AUTHORITY["EPSG","4326"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",-87],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["Meter",1]]
Origin = (332125.000000000000000,2017650.000000000000000)
Pixel Size = (25.000000000000000,-25.000000000000000)
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 332125.000, 2017650.000) ( 88d35'16.06"W, 18d14'29.96"N)
Lower Left ( 332125.000, 1948275.000) ( 88d34'55.98"W, 17d36'53.41"N)
Upper Right ( 373600.000, 2017650.000) ( 88d11'44.12"W, 18d14'40.22"N)
Lower Right ( 373600.000, 1948275.000) ( 88d11'29.00"W, 17d37' 3.30"N)
Center ( 352862.500, 1982962.500) ( 88d23'21.23"W, 17d55'47.10"N)
Band 1 Block=1659x1 Type=Float32, ColorInterp=Undefined
我的代码如下:
driver = gdal.GetDriverByName('ENVI')
driver.Register()
#Mind the suffix (It is an ENVI file)
file = 'C:/img.1__A_pwr_geo_sigma
raster = gdal.Open(file,gdal.GA_ReadOnly)
raster_array = raster.ReadAsArray()
print raster_array
输出:
>>>array([[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
...,
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan]], dtype=float32)
数组中的所有值都是NaN,虽然我知道图像中有浮点32位值(使用ENVI软件检查)
我在这里做错了什么?或者后缀是否有问题?
我也尝试使用gdal_translate将ENVI格式转换为Geotiff,但GeoTiff也生成相同的数组。
答案 0 :(得分:2)
好消息,并非所有价值观都是纳米。 GDAL驱动程序(v.1.9.1)运行良好,有效数据在依赖于GDAL(例如QGIS)的软件中可见。
nan
值用于表示NoData。 (通常,大多数人对NoData使用一些有限的“虚拟”值,例如1e + 20)。
import numpy as np
from osgeo import gdal
fname = r'C:\path\to\envi_data\IMG-HH-ALPSRP136260330-H1.1__A_pwr_geo_sigma'
ds = gdal.Open(fname, gdal.GA_ReadOnly)
band = ds.GetRasterBand(1)
print band.GetNoDataValue()
# None ## normally this would have a finite value, e.g. 1e+20
ar = band.ReadAsArray()
print np.isnan(ar).all()
# False
print '%.1f%% masked' % (np.isnan(ar).sum() * 100.0 / ar.size)
# 43.0% masked
在Python / NumPy中表示缺少值的数组的最佳方法是使用masked array:
mar = np.ma.masked_array(ar, np.isnan(ar))
print mar.min(), np.median(mar), mar.mean(), mar.std(), mar.max()
# 0.000715672 0.148093774915 0.0921848740388 0.0700106260235 5.0