我正在使用c#GDAL绑定尝试读取栅格值并执行相当简单的计算。总而言之,我想获取值的平均值,然后获取低于均值的值之和和高于均值的值之和。除了我尝试将栅格读取到缓冲区外,其他所有内容似乎都可以正常工作。结果是生成一个数组,其中每个值都是-3.402823E+38
数据是浮点数据,我希望在计算时保持这种方式。代码如下。
class Program
{
static void Main(string[] args)
{
Gdal.AllRegister();
Ogr.RegisterAll();
Dataset dataset = Gdal.Open(@"xxxxxxxxxxx.tif", Access.GA_ReadOnly);
Band band = dataset.GetRasterBand(1);
int width = band.XSize;
int height = band.YSize;
int size = width * height;
double min = 0.00;
double max = 0.00;
double mean = 0.00;
double stddev = 0.00;
var stats = band.GetStatistics(1, 0, out min, out max, out mean, out stddev);
//Console.WriteLine($"Statistics retrieved and returned a result of {stats}");
Console.WriteLine($"X : {width} Y : {height} SIZE: {size}");
Console.WriteLine($"MIN : {min} MAX : {max} MEAN : {mean} STDDEV : {stddev}");
DataType type = band.DataType;
Console.WriteLine($"Data Type : {type}");
float gtMean = 0; //cut
float ltMean = 0; //fill
float[] data = new float[size];
var dataArr = band.ReadRaster(0, 0, width, height, data, width, height, 0, 0);
int i, j;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
float value = data[i + j * width];
if (value > (float)mean)
{
gtMean += value;
}
if (value < (float)mean)
{
ltMean += value;
}
}
}
Console.WriteLine($"Sum of values above the mean {gtMean}");
Console.WriteLine($"Sum of values below the mean {ltMean}");
double pixelArea = 0.10763911106;
Console.WriteLine("Press Any Key To Exit....");
Console.ReadLine();
}
}