我正在编写一个脚本,该脚本接收包含测量位置的CSV文件,并根据这些测量值创建概率图。我正在使用GDAL将概率写入GeoTIFF,但是,我注意到没有任何数据正确写入GeoTIFF。生成热图时,我使用numpy将数据存储在X by Y数组中,然后将该数组写入GeoTIFF。我测试的当前数据集的尺寸为260 x 262米,得到的GeoTIFF具有正确的尺寸并且经过适当的地理参考。热图数据的像素值应介于-51到0之间,但是,当我将GeoTIFF加载到QGIS中时,QGIS的值只有-51到-31。
GeoTIFF应存储为32位浮点数据和无损压缩,因此它不是数据范围或压缩问题。
我已附上以下相关代码:
import sys
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
# USING utm 0.4.0 from https://pypi.python.org/pypi/utm
import utm
import os
import argparse
import fileinput
from osgeo import gdal
import osr
import math
# finalLon and finalLat are 1-D arrays containing the UTM coordinates for each measurement
tiffXSize = int(max(finalLon)) - int(min(finalLon)) + maxRange * 2
tiffYSize = int(max(finalLat)) - int(min(finalLat)) + maxRange * 2
pixelSize = 1
heatMapArea = np.zeros((tiffXSize, tiffYSize)) # y, x
refLat = min(finalLat) - maxRange
maxLat = max(finalLat) + maxRange
refLon = max(finalLon) + maxRange
minLon = min(finalLon) - maxRange
# some code to set values in the heatMapArea array
outputFileName = '%s/RUN_06d_COL_%06d.tiff' % (output_path, run_num, num_col)
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(
outputFileName,
tiffYSize,
tiffXSize,
1,
gdal.GDT_Float32, ['COMPRESS=LZW'])
spatialReference = osr.SpatialReference()
spatialReference.SetUTM(zonenum, zone >= 'N')
spatialReference.SetWellKnownGeogCS('WGS84')
wkt = spatialReference.ExportToWkt()
retval = dataset.SetProjection(wkt)
dataset.SetGeoTransform((
refLat, # 0
1, # 1
0, # 2
refLon, # 3
0, # 4
-1))
band = dataset.GetRasterBand(1)
band.SetNoDataValue(100)
print(tiffXSize)
print(tiffYSize)
print(np.amin(heatMapArea))
print(np.amax(heatMapArea))
print(np.mean(heatMapArea))
print(np.std(heatMapArea))
print((heatMapArea > -30).sum())
band.WriteArray(heatMapArea)
band.SetStatistics(np.amin(heatMapArea), np.amax(heatMapArea), np.mean(heatMapArea), np.std(heatMapArea))
dataset.FlushCache()
dataset = None
答案 0 :(得分:1)
问题在于QGIS如何计算GeoTIFF的最大值和最小值。如果您进入GeoTIFF图层的属性,并打开“样式”选项卡,则在“带渲染”下,可以选择如何加载最小/最大值。默认情况下,QGIS使用2%到98%的计数作为最小值和最大值,但是,为此,我们需要最小值/最大值。