GDAL中RasterIO函数(把文件读取为一个一维数组)和ReadBlock函数(读取栅格数据块)...

CPLErr GDALRasterBand::RasterIO

(

GDALRWFlag

eRWFlag,

int

nXOff,

int

nYOff,

int

nXSize,

int

nYSize,

void *

pData,

int

nBufXSize,

int

nBufYSize,

GDALDataType

eBufType,

int

nPixelSpace,

int

nLineSpace

)

Read/write a region of image data for this band.

This method allows reading a region of a GDALRasterBand into a buffer, or writing data from a buffer into a region of a GDALRasterBand. It automatically takes care of data type translation if the data type (eBufType) of the buffer is different than that of the GDALRasterBand. The method also takes care of image decimation / replication if the buffer size (nBufXSize x nBufYSize) is different than the size of the region being accessed (nXSize x nYSize).

The nPixelSpace and nLineSpace parameters allow reading into or writing from unusually organized buffers. This is primarily used for buffers containing more than one bands raster data in interleaved format.

Some formats may efficiently implement decimation into a buffer by reading from lower resolution overview images.

For highest performance full resolution data access, read and write on "block boundaries" as returned by GetBlockSize(), or use the ReadBlock() and WriteBlock() methods.

This method is the same as the C GDALRasterIO() function.

Parameters:

 

eRWFlag

Either GF_Read to read a region of data, or GF_Write to write a region of data.

 

nXOff

The pixel offset to the top left corner of the region of the band to be accessed. This would be zero to start from the left side.

 

nYOff

The line offset to the top left corner of the region of the band to be accessed. This would be zero to start from the top.

 

nXSize

The width of the region of the band to be accessed in pixels.

 

nYSize

The height of the region of the band to be accessed in lines.

 

pData

The buffer into which the data should be read, or from which it should be written. This buffer must contain at least nBufXSize * nBufYSize words of type eBufType. It is organized in left to right, top to bottom pixel order. Spacing is controlled by the nPixelSpace, and nLineSpace parameters.

 

nBufXSize

the width of the buffer image into which the desired region is to be read, or from which it is to be written.

 

nBufYSize

the height of the buffer image into which the desired region is to be read, or from which it is to be written.

 

eBufType

the type of the pixel values in the pData data buffer. The pixel values will automatically be translated to/from the GDALRasterBand data type as needed.

 

nPixelSpace

The byte offset from the start of one pixel value in pData to the start of the next pixel value within a scanline. If defaulted (0) the size of the datatype eBufType is used.

 

nLineSpace

The byte offset from the start of one scanline in pData to the start of the next. If defaulted (0) the size of the datatype eBufType * nBufXSize is used.

Returns:

CE_Failure if the access fails, otherwise CE_None.

CPLErr GDALRasterBand::ReadBlock

(

int

nXBlockOff,

int

nYBlockOff,

void *

pImage

)

Read a block of image data efficiently.

This method accesses a "natural" block from the raster band without resampling, or data type conversion. For a more generalized, but potentially less efficient access use RasterIO().

This method is the same as the C GDALReadBlock() function.

See the GetLockedBlockRef() method for a way of accessing internally cached block oriented data without an extra copy into an application buffer.

Parameters:

 

nXBlockOff

the horizontal block offset, with zero indicating the left most block, 1 the next block and so forth.

 

nYBlockOff

the vertical block offset, with zero indicating the left most block, 1 the next block and so forth.

 

pImage

the buffer into which the data will be read. The buffer must be large enough to hold GetBlockXSize()*GetBlockYSize() words of type GetRasterDataType().

Returns: CE_None on success or CE_Failure on an error.

The following code would efficiently compute a histogram of eight bit raster data. Note that the final block may be partial ... data beyond the edge of the underlying raster band in these edge blocks is of an undermined value.

CPLErr GetHistogram( GDALRasterBand *poBand, int *panHistogram )

{

int nXBlocks, nYBlocks, nXBlockSize, nYBlockSize;

int iXBlock, iYBlock;

GByte *pabyData;

memset( panHistogram, 0, sizeof(int) * 256 );

CPLAssert( poBand->GetRasterDataType() == GDT_Byte );

poBand->GetBlockSize( &nXBlockSize, &nYBlockSize );

nXBlocks = (poBand->GetXSize() + nXBlockSize - 1) / nXBlockSize;

nYBlocks = (poBand->GetYSize() + nYBlockSize - 1) / nYBlockSize;

pabyData = (GByte *) CPLMalloc(nXBlockSize * nYBlockSize);

for( iYBlock = 0; iYBlock < nYBlocks; iYBlock++ )

{

for( iXBlock = 0; iXBlock < nXBlocks; iXBlock++ )

{

int nXValid, nYValid;

poBand->ReadBlock( iXBlock, iYBlock, pabyData );

// Compute the portion of the block that is valid

// for partial edge blocks.

if( (iXBlock+1) * nXBlockSize > poBand->GetXSize() )

nXValid = poBand->GetXSize() - iXBlock * nXBlockSize;

else

nXValid = nXBlockSize;

if( (iYBlock+1) * nYBlockSize > poBand->GetYSize() )

nYValid = poBand->GetYSize() - iYBlock * nYBlockSize;

else

nYValid = nYBlockSize;

// Collect the histogram counts.

for( int iY = 0; iY < nYValid; iY++ )

{

for( int iX = 0; iX < nXValid; iX++ )

{

panHistogram[pabyData[iX + iY * nXBlockSize]] += 1;

}

}

}

}

}

转载于:https://www.cnblogs.com/AmatVictorialCuram/p/3435447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值