zoukankan      html  css  js  c++  java
  • 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;

    }

    }

    }

    }

    }

  • 相关阅读:
    学习些新东西
    浏览器内的web开发工具
    基于oracle开发的初步接触
    LAMP3 PHP安装
    svn for windows
    PHP替换掉字符串中的非字符
    搭个邮件服务器
    centos下安装mysql
    安装tomcat
    c#线程
  • 原文地址:https://www.cnblogs.com/AmatVictorialCuram/p/3435447.html
Copyright © 2011-2022 走看看