PIE 分块读写数据
手头的影像数据量比较大,尺寸是 20009*10165 的,直接创建数组会导致栈溢出,因此决定分块,思路也比较简单,如图:
只是把x分成多份,y不变,这样代码写起来也简单,只要做好最后一个块的宽度的判断就好了。
我这里是把一个float型的单波段影像做了一个阈值分割,输出到一个byte影像中,分块是以 3000*高度 为一个块,代码如下:
int BandCount = hyData.GetBandCount();
int ImgWidth = hyData.GetRasterXSize();
int ImgHeight = hyData.GetRasterYSize();
string SpatialRef = hyData.SpatialReference.ExportToPrettyWkt();
PixelDataType DataType = hyData.GetRasterBand(0).GetRasterDataType();
SaveFileDialog SaveData = new SaveFileDialog();
SaveData.Filter = "RasterFileData|*.tiff";
if (SaveData.ShowDialog() != DialogResult.OK) return;
int[] bandMap = new int[BandCount];
for (int i = 0; i < BandCount; i++)
{
bandMap[i] = i + 1;
}
IRasterDataset newRasterDataset = DatasetFactory.CreateRasterDataset(SaveData.FileName, ImgWidth, ImgHeight, BandCount, DataType, "GTiff", null);
newRasterDataset.SpatialReference = hyData.SpatialReference;
float[] data = new float[ImgHeight * 3000];
byte[] result = new byte[ImgHeight * 3000];
//读取数据到数组
int times = ImgWidth / 3000;
for (int i = 0; i < times + 1; i++)
{
if (i != times)
{
hyData.Read(i * 3000, 0, 3000, ImgHeight, data, 3000, ImgHeight, DataType, BandCount, bandMap);
}
else
{
hyData.Read(i * 3000, 0, ImgWidth % 3000, ImgHeight, data, ImgWidth % 3000, ImgHeight, DataType, BandCount, bandMap);
}
for (int j = 0; j < data.Length; j++)
{
if (data[j] > 0.05)
{
result[j] = 255;
}
else
{
result[j] = 0;
}
}
if (i != times)
{
newRasterDataset.Write(i * 3000, 0, 3000, ImgHeight, result, 3000, ImgHeight, PixelDataType.Byte, BandCount, bandMap);
}
else
{
newRasterDataset.Write(i * 3000, 0, ImgWidth % 3000, ImgHeight, result, ImgWidth % 3000, ImgHeight, PixelDataType.Byte, BandCount, bandMap);
}
}
double[] geoTrans = hyData.GetGeoTransform();
newRasterDataset.SetGeoTransform(geoTrans);
((IDisposable)newRasterDataset).Dispose();
MessageBox.Show("新建tiff成功");

鲁公网安备 37148202000241号